In the previous article, we talked about how to correctly install the MySQL database. Next we will talk about the correct use of the database. We mainly learn some common MySQL commands from the following aspects: connecting to the MySQL database, correct password modification, and adding users.
MySQL database use 1. Connect to MySQL.
Format: MySQL-h host address-u user name-p User Password
1. Example 1: connect to MySQL on the local machine.
First, open the DOS window, enter the directory MySQLbin, then type the command MySQL-uroot-p, and press enter to prompt you to enter the password. If you have just installed MySQL, super User root has no password, so press enter to enter MySQL. The MySQL prompt is: MySQL>
2. Example 2: connect to MySQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Enter the following command:
MySQL-h110.110.110.110-uroot-pabcd123
Note: u and root do not need to add spaces. The same applies to others)
3. exit MySQL command: exit and press Enter)
MySQL database use 2. Change the password.
Format: MySQLadmin-u username-p old password New password
1. Example 1: Add a password ab12 to the root user. First, enter the directory MySQLbin in DOS, and then type the following command
MySQLadmin-uroot-password ab12
Note: because the root account does not have a password at the beginning, the old-p password can be omitted.
2. Example 2: Change the root password to djg345.
MySQLadmin-uroot-pab12 password djg345
MySQL database usage 3. Add new users.
Note: Unlike the above, the following commands in the MySQL environment are followed by a semicolon as the command Terminator)
Format: grant select on database. * to username @ login host identified by "password"
Example 1: Add a user named "test1" with the password "abc" so that the user 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:
Grant select, insert, update, delete on *. * to test1 @ "%" Identified by "abc ";
However, the User Added in Example 1 is very dangerous. If someone knows the password of test1, then he can log on to your MySQL database on any computer on the internet and do whatever he wants for your data. For the solution, see Example 2.
Example 2: Add a user named "test2" with the password "abc" so that the user can only log on to localhost and query, insert, modify, and delete the Database "mydb". localhost indicates the local host, that is, the host where the MySQL database is located. In this way, the user knows the password of test2, and cannot directly access the database from the internet, but can only access the database through the web page on the MySQL host.
- grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
If you do not want test2 to have a password, you can run another command to remove the password.
- grant select,insert,update,delete on mydb.* to test2@localhost identified by "";
The above content is an introduction to the use of MySQL databases. I hope you will get some benefits.