1. Download the Linux installation file
of MySQL The following two files are required to install MySQL under Linux:
MySQL-server-5.1.7-0.i386.rpm The
download address is: http://dev.mysql.com/downloads/mysql/5.1 .html, open this web page, pull down the web page to find the "Linux x86 generic RPM (statically linked against glibc 2.2.5) downloads" item, find the "Server" and "Client programs" items, and download the two rpm files you need.
2. Installing MySQL under Linux The
rpm file is a software installation package developed by Red Hat, and rpm allows Linux to avoid many complicated procedures when installing software packages. The commonly used parameter of this command during installation is –ivh, where i indicates that the specified rmp package will be installed, V indicates the detailed information during installation, and h indicates that the “#” symbol appears during the installation to display the current installation process. This symbol will continue until the installation is complete before stopping.
1) Install the server side
Run the following command in the directory with two rmp files:
[root@test1 local]# rpm -ivh MySQL-server-5.1.7-0.i386.rpm MySQL-client-5.1.7-0 .i386.rpm
displays the following information.
warning: MySQL-server-5.1.7-0.i386.rpm
signature: NOKEY, key ID 5072e1f5
Preparing... ########################################### [100% ]
1:MySQL-server ########################################### [ 100%]
. . . . . . (Shown omitted)
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h test1 password 'new-password'
. . . . . . (Omitted to show)
Starting mysqld daemon with databases from /var/lib/mysql
If the above information appears, the server installation is complete. To test whether the test is successful, run netstat to see if the Mysql port is open. If it is open, it means that the service has been started and the installation is successful. Mysql default 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 display shows that MySQL The service has been started.
2) Install the client
Run the following command:
[root@test1 local]# rpm -ivh MySQL-client-5.1.7-0.i386.rpm
warning: MySQL-client-5.1.7-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 it is successful.
3.
Log in to MySQL The command to log in 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 without a password. Note: this root user is not a Linux system user. The default user of MySQL is root. Since there is no password initially, 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>
"mysql>" prompt appears, congratulations You, the installation was successful!
The login format after adding a password is as follows:
mysql -u root -p
Enter password: (input password)
where -u is followed by the user name, -p requires a password, and enter the password at the input password after pressing Enter.
Note: This mysql file is in the /usr/bin directory, and is not the same file as the startup file /etc/init.d/mysql mentioned later.
4. Several important directories of
MySQL MySQL is not installed in one directory by default like SQL Server after installation. Its database files, configuration files and command files are in different directories. It is very important to understand these directories, especially for Linux beginners , Because the directory structure of Linux itself is more complicated, if you don't know the installation directory of MySQL, you can't talk about in-depth study.
These directories are described below.
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. The startup script
/etc/rc.d/init.d/ (the directory of the startup script file mysql)
5. Modify the login password
MySQL has no password by default. The importance of adding a password after installation is self-evident.
1. Command
usr/bin/mysqladmin -u root password 'new-password'
format: mysqladmin -u username -p old password password new password
2. Example
Example 1: Add a password of 123456 to root.
Type the following command:
[root@test1 local]# /usr/bin/mysqladmin -u root password 123456
Note: Because root has no password at the beginning, the -p old password can be omitted.
3. Test whether the modification is successful
1) Log in without a password
[root@test1 local]# mysql
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
An error is displayed, indicating that the password has been modified.
2) Log in with the modified password
[root@test1 local]# mysql -u root -p
Enter password: (enter the modified password 123456)
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
>
Success!
This is to modify the password through the mysqladmin command, or you can change the password by modifying the library.
6. Start and stop
1. Start
MySQL After the installation is complete, the startup file mysql is in the /etc/init.d directory, and you can run the following command when you need to start it.
[root@test1 init.d]# /etc/init.d/mysql start
2. Stop
/usr/bin/mysqladmin -u root -p shutdown
3. Automatically start
1) Check if mysql is in the automatic startup 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. If you want to move the directory to /home/data, you need to do the following steps:
1. Create a data directory
cd /home
mkdir data in the home directory
2. Stop the MySQL service process:
mysqladmin -u root -p shutdown
3. Put / Move the entire directory of var/lib/mysql to /home/data
mv /var/lib/mysql /home/data/
This will move the MySQL data files to /home/data/mysql
4. Find the my.cnf configuration file
if There is no my.cnf configuration file in the /etc/ directory, please go to /usr/share/mysql/ to find the *.cnf file, copy one of them to /etc/ and rename 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
In order to ensure that MySQL can work normally, it is necessary to Specifies the location where the mysql.sock file is generated. Modify the value to the right of the equal sign in the line socket=/var/lib/mysql/mysql.sock: /home/mysql/mysql.sock. The operation 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/mysql.sock (the original content, in order to be more secure Comment this line with "#")
socket = /home/data/mysql/mysql.sock (plus this line)
6. Modify the MySQL startup script /etc/rc.d/init.d/mysql
Finally, you need to modify the MySQL startup In the script /etc/rc.d/init.d/mysql, in the line of datadir=/var/lib/mysql, change the path to the right of the equal sign to your actual storage path: home/data/mysql.
[root@test1 etc]# vi /etc/rc.d/init.d/mysql
#datadir=/var/lib/mysql (comment this line)
datadir=/home/data/mysql (add this line)
7. Restart the MySQL service
/etc/rc.d/init.d/mysql start
or use the reboot command to restart Linux
if it works normally and the move is successful, otherwise check it against the previous 7 steps.
8. Common operations of
MySQL Note: Every 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 installed two databases: mysql and test. The mysql library is very important. It contains MySQL system information. When we change passwords and add new users, we actually use the relevant tables in this library to operate.
2. Display the tables in the database
mysql> use mysql; (open the library, open this library for each library operation, 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 structure of the data table:
describe table name;
4. Display the records in the table:
select * from table name;
for example: display the records in the user table in the mysql library. All users who can operate on MySQL users are in this table.
Select * from user;
5. Create a database:
create database database name;
for example: create a database whose name is aaa
mysql> create databases aaa;
6. Create a table:
use database name;
create table table name (field setting list);
For example: create a table name in the newly created aaa library, the table has id (serial number, automatic growth), xm (name), xb (gender), csny (date of birth) four fields
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 newly 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. Adding records
For example: adding several related records.
mysql> insert into name values('','Zhang San','male','1971-10-01');
mysql> insert into name values('','Baiyun','female','1972-05 -20');
use the select command to verify the results.
mysql> select * from name;
+----+------+------+------------+
| 1 | Zhang San | Male | 1971-10-01 |
| 2 | Baiyun | Female | 1972-05-20 |
+----+------+------+------------+
8. Modify the record,
for example: Change the date of birth of Zhang San to 1971-01-10
mysql> update name set csny='1971-01-10' where xm='Zhang San';
9. Delete the record
For example: delete the record of Zhang San.
mysql> delete from name where xm='Zhang San';
10. Delete database and delete table
drop database database name;
drop table table name;
9. Add MySQL user
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 in on any host and have the right to query, insert, modify, and delete all databases. First connect to MySQL as root user, then type the following command:
mysql> grant select,insert,update,delete on *.* to user_1@"%" Identified by "123";
The user added in Example 1 is very dangerous. If he knows the password of user_1, he can log in to your MySQL database on any computer on the Internet and do whatever he wants with your data. The solution is shown in Example 2.
Example 2. Add a user user_2 with a password of 123, so that this user can only log in on localhost, and can 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 even if the user knows the password of user_2, he cannot directly access the database from the Internet, and can only operate the aaa library through the MYSQL host.
mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123";
If you cannot log in to MySQL with the new user, use the following command when logging in:
mysql -u user_1 -p -h 192.168 .113.50 (-h is followed by the ip address of the host to be logged in)
X. Backup and recovery
1. Backup
For example: backup the aaa library created in the above example to the file back_aaa
[root@test1 root]# cd /home/data/ mysql (Go to the library directory, the library in this example has been transferred from val/lib/mysql to /home/data/mysql, see the content in Part 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
Install MySQL under Linux
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325164588&siteId=291194637
Recommended
Ranking