Install and use MySQL in centos

Source: Internet
Author: User
Tags mysql host
I. Introduction I have been using Linux for a long time. I have never studied Linux since I have no hard tasks or systems. Recently, I have to use MySQL in Linux for my work. I thought I had experience using SQLServer in Windows and thought it would be easy to install MySql in Linux. I had a lot of detours when installing and using MySQL.

I. Introduction I have been using Linux for a long time. I have never studied Linux since I have no hard tasks or systems. Recently, I have to use MySQL in Linux for my work. I thought I had experience using SQL Server in Windows and thought it would be easy to install MySql in Linux. I had a lot of detours when installing and using MySQL.

I. Introduction I have been using Linux for a long time. I have never studied Linux since I have no hard tasks or systems. Recently, I have to use MySQL in Linux for my work. I thought that I had experience using SQL Server in Windows and thought it would be easy to install MySql in Linux. I had a lot of detours and encountered many problems when installing and using MySQL, after all, there is a big difference between Linux and Windows. I wrote this article to help beginners like me learn less and get started as soon as possible. The Linux environment in this article is Red Hat 9.0 and MySQL is 4.0.16.

Ii. Install Mysql

1. Download the MySQL Installation File

The following two files are required to install MySQL:

MySQL-server-4.0.16-0.i386.rpm

MySQL-client-4.0.16-0.i386.rpm

For: http://www.mysql.com/downloads/mysql-4.0.html, open this page, drop-down page to find the "Linux x86 RPM downloads" item, find the "Server" and "Client programs" item, download the above two rpm files.

2. Install MySQL

The rpm file is a software installation package developed by Red Hat. rpm frees Linux from complicated procedures when installing software packages.

The frequently used parameter of this command during installation is-ivh, where I indicates that the specified rmp package will be installed, and V indicates the detailed information during installation, h indicates that the "#" symbol appears during the installation to display the current installation process. This symbol will not stop until the installation is complete.

1) install the server

Run the following command in the directory with two rmp files:

[Root @ test1 local] # rpm-ivh MySQL-server-4.0.16-0.i386.rpm

The following information is displayed.

Warning: MySQL-server-4.0.16-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5

Preparing... ######################################## ### [100%] 1: mySQL-server ##################################### ###### [100%]... (Omitted)

/Usr/bin/mysqladmin-u root password 'new-password'

/Usr/bin/mysqladmin-u root-h test1 password 'new-password '...... (Omitted)

Starting mysqld daemon with databases from/var/lib/mysql

If the preceding information is displayed, the Server installation is complete. Run netstat to check whether the Mysql port is opened. If yes, the service is started and the installation is successful. The default Mysql port is 3306.

[Root @ test1 local] # netstat-nat

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State tcp

0 0 0.0.0.0: 3306 0.0.0.0: * LISTEN

The above shows that the MySQL service has been started.

2) install the client

Run the following command:

[Root @ test1 local] # rpm-ivh MySQL-client-4.0.16-0.i386.rpm

Warning: MySQL-client-4.0.16-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5

Preparing... ######################################## ### [100%] 1: mySQL-client ##################################### ###### [100%]

The installation is complete.

Use the following command to connect to mysql and test whether the connection is successful.

3. log on to MySQL

The command used to log on to MySQL is mysql. the syntax of mysql is as follows:

Mysql [-u username] [-h host] [-p [password] [dbname]

Username and password are the username and password of MySQL respectively. The initial management account of mysql is root, and there is no password. Note: This root user is not a Linux system user. The default MySQL user is root. Because there is no password at first, you only need to type mysql for the first time.

[Root @ test1 local] # mysql Welcome to the MySQL monitor. commands end with; or/g. your MySQL connection id is 1 to server version: 4.0.16-standard Type 'help; 'or'/H' for help. type '/C' to clear the buffer.

Mysql> the prompt "mysql>" appears. Congratulations! The installation is successful!

The logon format after the password is added is as follows:

Mysql-u root-p Enter password: (Enter the password) where-u is followed by the user name,-p requires a password, and press Enter to Enter the password.

Note: This mysql file is located in the/usr/bin directory. It is not a file with the Startup file/etc/init. d/mysql described later.

4. Several important MySQL Directories

After MySQL is installed, its database files, configuration files, and command files are not installed in the same directory as SQL Server by default. It is very important to understand these directories, especially for Linux beginners, the directory structure of Linux itself is complicated. If you cannot figure out the installation directory of MySQL, you won't be able to learn it in depth.

The following describes these directories.

1. Database directory/var/lib/mysql/

2. Configuration File/usr/share/mysql (mysql. server command and configuration file)

3. Related commands/usr/bin (mysqladmin mysqldump and other commands)

4. Start the script/etc/rc. d/init. d/(the directory for starting the script file mysql)

5. Change the logon Password

MySQL does not have a password by default. It is self-evident that the password is added after installation.

1. Command usr/bin/mysqladmin-u root password 'new-password'

Format: mysqladmin-u username-p old password New password

2. Example 1: Add a 123456 password to the root user.

Type the following command: [root @ test1 local] #/usr/bin/mysqladmin-u root password 123456

Note: because the root account does not have a password at the beginning, the old-p password can be omitted.

3. test whether the modification is successful

1) login without a password

[Root @ test1 local] # mysql

ERROR 1045: Access denied for user: 'root @ localhost' (Using password: NO) indicates that the password has been changed.

2) log on with the modified Password

[Root @ test1 local] # mysql-u root-p

Enter password: (Enter the password 123456 after modification)

Welcome to the MySQL monitor. Commands end with; or/g.

Your MySQL connection id is 4 to server version: 4.0.16-standard

Type 'help; 'or'/H' for help. type'/C' to clear the buffer.

Mysql> successful!

You can use the mysqladmin command to change the password or the database to change the password.

6. Start and Stop

1. After MySQL installation is started, run the following command to start mysql in the/etc/init. d directory.

[Root @ test1 init. d] #/etc/init. d/mysqld start

2. Stop/usr/bin/mysqladmin-u root-p shutdown

3. Automatic Start

1) Check whether mysql is in the Auto Start List

[Root @ test1 local] #/sbin/chkconfig-list

2) Add MySQL to the startup Service Group of your system.

[Root @ test1 local] #/sbin/chkconfig-add mysql

3) Delete MySQL from the startup Service Group.

[Root @ test1 local] #/sbin/chkconfig-del mysql

7. Change the MySQL directory

The default data file storage directory of MySQL is/var/lib/mysql.

To move the directory to/home/data, perform the following steps:

1. Create the data DIRECTORY cd/home mkdir data under the home Directory

2. Stop the MySQL service process: mysqladmin-u root-p shutdown

3. Move the entire/var/lib/mysql directory to/home/data mv/var/lib/mysql/home/data/

In this way, the MySQL data file is moved to/home/data/mysql.

4. Find the my. cnf configuration file.

If my. for the cnf configuration file, go to/usr/share/mysql/and find *. copy one of the cnf files to/etc/and change it to my. cnf.

The command is as follows:

[Root @ test1 mysql] # cp/usr/share/mysql/my-medium.cnf/etc/my. cnf

5. Edit the MySQL configuration file/etc/my. cnf.

To ensure that MySQL works properly, you must specify the location where the mysql. sock file is generated.

Change socket =/var/lib/mysql. sock to/home/mysql. sock.

The procedure is as follows:

Vi my. cnf

(Use the vi tool to edit the my. cnf file and find the following data to modify)

# The MySQL server [mysqld]

Port = 3306

# Socket =/var/lib/mysql. sock)

Socket =/home/data/mysql. sock (add this line)

6. Modify the MySQL STARTUP script/etc/rc. d/init. d/mysql

Finally, you need to modify the MySQL STARTUP script/etc/rc. d/init. d/mysql: change the path on the Right of datadir =/var/lib/mysql to your actual storage path: home/data/mysql.

[Root @ test1 etc] # vi/etc/rc. d/init. d/mysql

# Datadir =/var/lib/mysql (comment this row)

Datadir =/home/data/mysql (add this row)

7. Restart the MySQL service.

/Etc/rc. d/init. d/mysql start

Or use the reboot command to restart Linux.

If it works properly, it will succeed. Otherwise, check again against the previous seven steps.

VIII. Common MySQL operations

Note: Each Command in MySQL must end with a semicolon.

1. display the database

Mysql> show databases;

+ ---------- +

| Database |

+ ---------- +

| Mysql |

| Test |

+ ---------- +

2 rows in set (0.04 sec)

Mysql has just been installed with two databases: mysql and test. The mysql database is very important. It contains MySQL system information. We change the password and add new users. In fact, we use the relevant tables in this database for operations.

2. display tables in the database

Mysql> use mysql; (open the database. to operate on each database, open the database, similar to foxpro)

Database changed

Mysql> show tables;

+ ----------------- +

| Tables_in_mysql |

+ ----------------- +

| Columns_priv |

| Db |

| Func |

| Host |

| Tables_priv |

| User |

+ ----------------- +

6 rows in set (0.01 sec)

3. display the data table structure:

Describe table name;

4. display the records in the table:

Select * from table name;

For example, the user table records in the mysql database are displayed.

All users who can operate on MySQL users are in this table.

Select * from user;

5. database creation:

Create database name;

For example, create a database named aaa

Mysql> create databases aaa;

6. Create a table:

Use Database Name;

Create table Name (field setting list );

For example, if you create a table name in the newly created aaa database, the table has four fields: id (serial number, auto-increment), xm (name), xb (gender), and csny (date of birth ).

Use aaa;

Mysql> create table name (id int (3) auto_increment not null primary key, xm char (8), xb char (2), csny date );

You can use the describe command to view the created table structure.

Mysql> describe name;

+ ------- + --------- + ------ + ----- + --------- + ---------------- +

| Field | Type | Null | Key | Default | Extra |

+ ------- + --------- + ------ + ----- + --------- + ---------------- +

| Id | int (3) | PRI | NULL | auto_increment |

| Xm | char (8) | YES | NULL |

| Xb | char (2) | YES | NULL |

| Csny | date | YES | NULL |

+ ------- + --------- + ------ + ----- + --------- + ---------------- +

7. Add records

For example, add several related records.

Mysql> insert into name values ('', 'zhang san', 'mal', '2017-10-01 ');

Mysql> insert into name values ('', 'baiyun ', 'female', '2017-05-20 ');

The select command can be used to verify the result.

Mysql> select * from name;

+ ---- + ------ + ------------ +

| Id | xm | xb | csny |

+ ---- + ------ + ------------ +

| 1 | Zhang San | male |

| 2 | Baiyun | female | 1972-05-20 |

+ ---- + ------ + ------------ +

8. Modify records

For example, change the date of birth of John

Mysql> update name set csny = '2017-01-10 'where xm = 'zhang san ';

9. delete records

For example, delete the records of Michael Jacob.

Mysql> delete from name where xm = 'zhang san ';

10. Delete databases and tables

Drop database name;

Drop table name;

9. Add MySQL users

Format: grant select on database. * to username @ login host identified by "password"

Example 1: Add a user user_1 with a password of 123 so that he can log on to any host and have the permission to query, insert, modify, and delete all databases. First, use the root user to connect to MySQL, and then type the following command:

Mysql> grant select, insert, update, delete on *. * to user_1 @ "%" Identified by "123"; in Example 1, it is very dangerous to add users. If you know the user_1 password, then he can log on to your MySQL database on any computer on the Internet and do whatever he wants. For the solution, see Example 2.

Example 2: Add a user_2 password of 123 so that the user can only log on to localhost, you can also query, insert, modify, and delete the database aaa (localhost refers to the local host, that is, the host where the MySQL database is located), so that the user knows the password of user_2, he cannot directly access the database from the Internet, and can only operate the aaa database through the MYSQL host.

Mysql> grant select, insert, update, delete on aaa. * to user_2 @ localhost identified by "123 ";

If a new user cannot log on to MySQL,

Run the following command during logon: mysql-u user_1-p-h 192.168.113.50 (-h is followed by the IP address of the host to be logged on)

10. backup and recovery

1. Backup example: Back up the aaa library created in the previous example to the file back_aaa

[Root @ test1 root] # cd/home/data/mysql (go to the database directory, this example library has been transferred from val/lib/mysql to/home/data/mysql, see section 7 above)

[Root @ test1 mysql] # mysqldump-u root-p -- opt aaa> back_aaa

2. Restore [root @ test mysql] # mysql-u root-p ccc <back_aaa

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.