MySQL utility commands

MySQL utility commands

Wikipedia, the free encyclopedia

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, and then enter the bin directory under the mysql installation directory, for example: D:/mysql/bin, then type the command mysql -uroot -p, and you will be prompted to enter the password after pressing Enter. If MYSQL has just been installed, the super The user root does not have a password, so you can enter MYSQL directly by pressing Enter. The MYSQL prompt is: mysql> 

2. Example 2: Connect to MYSQL on a remote host

   Suppose the IP of the remote host is: 10.0.0.1, the username is root, and the password is 123. then type the following command:
   mysql -h10.0.0.1 -uroot -p123 
  (Note: u and root do not need to add spaces, and the others are the same) 

3. Exit the MYSQL command

   exit (carriage return) 

 

(2) Change the password:

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

1. Example 1: Add a password of 123 to root. First enter the directory C:/mysql/bin under DOS, and then type the following command:

   mysqladmin -uroot -password 123 
   Note: Because root has no password at the beginning, the -p old password item can be omitted. 

2. Example 2: Change the root password to 456

   mysqladmin -uroot -pab12 password 456 

(3) Add a new user: (Note: Unlike the above, the following is a command in the MYSQL environment, so it is 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 a password of abc, so that he can log in on any host and have the permissions to query, insert, modify, and delete all databases. First connect to MYSQL as root user, then type the following command:
   grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";

   But the user added in example 1 is very dangerous. If you want someone to know 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. The solution See Example 2.
   Example 2. Add a user test2 with a password of 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, that is, the host where the MYSQL database is located) , so that even if the user knows the password of test2, he cannot directly access the database from the internet, but can only access it 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 type another command to remove the password.
   grant select,insert,update,delete on mydb.* to test2@localhost identified by "";

(4) Display command

1. Display the database list:

   show databases;
   There were only two databases at the beginning: 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 this library to operate.

2. Display the data table in the library:

   use mysql; //Open the library
   show tables;

3. Display the structure of the data table:

   describe tablename; 

4. Build a library:

   create database 库名; 

5. Create a table:

   use library name;
   create table table name (field setting list);

6. Delete database and delete table:

   drop database 库名;
   drop table table name;

7. Clear the records in the table:

   delete from 表名; 

8. Display the records in the table:

   select * from 表名;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053292&siteId=291194637