MySQL Usage Guide (Part 1)
Author: Donkey Kong
There are many friends who have installed MySQL but don’t know how to use it. In this article, we will learn some common MYSQL commands from connecting to MYSQL, changing passwords, adding users, etc.
1. Connect to MYSQL.
Format: mysql -h host address -u username -p user password
1. Example 1: Connect to MYSQL on this machine.
First open the DOS window, then enter the directory mysqlbin, and then type the command mysql -uroot -p. After pressing Enter, you will be prompted to enter your password. If MYSQL has just been installed, the super user root does not have a password, so just press Enter. You can enter MYSQL. The MYSQL prompt is: mysql>
2. Example 2: Connect to MYSQL on the remote host. Assume that the IP of the remote host is: 110.110.110.110, the user name is root, and the password is abcd123. Then type the following command:
mysql -h110.110.110.110 -uroot -pabcd123
(Note: u and root do not need to add spaces, the same goes for others)
3. Exit the MYSQL command: exit (Enter )
2. Change the password.
Format: mysqladmin -u username -p old password password new password
1. Example 1: Add a password ab12 to root. First, enter the directory mysqlbin under DOS, and then type the following command
mysqladmin -uroot -password ab12
Note: Because root does not have a password at the beginning, the -p old password item can be omitted.
2. Example 2: Change the root password to djg345.
mysqladmin -uroot -pab12 password djg345
3. Add new users. (Note: Different from the above, the following are commands in the MYSQL environment, so they 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 test1 with the password abc, so that he can log in on any host and have query, insert, modify, and delete permissions on all databases. First connect to MYSQL as the root user, and then type the following command:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
But the user added in Example 1 It is very dangerous. If someone knows the password of test1, then he can log in to your mysql database on any computer on the Internet and do whatever he wants with your data. See Example 2 for the solution.
Example 2: Add a user test2 with the password abc, so that he can only log in on localhost, and can query, insert, modify, and delete the database mydb (localhost refers to the local host, where the MYSQL database is located) host), in this way, even if the user knows the password of test2, he cannot directly access the database from the Internet, and can only access it through the web page on the MYSQL host.