Correct MySQL command operation procedure

Source: Internet
Author: User
Tags mysql commands

The following articles mainly describe the correct usage of MySQL commands, including how to correctly connect to the MySQL database and correct operation steps to change the password, this section describes how to add new users and display MySQL database commands.

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 bin directory under the MySQL installation directory, for example, D: \ MySQL \ bin, and then enter the MySQL Command MySQL-uroot-p, after you press enter, you are prompted to enter the password. If you have just installed MySQL, the Super User root does not have a password, so you can directly 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 10.0.0.1, the user name is root, and the password is 123. Enter the following command:

 
 
  1. MySQL -h10.0.0.1 -uroot -p123  

Note: u and root do not need to add spaces. The same applies to others)

3. Exit MySQL Command

Exit and press Enter)

(2) Change the password:

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

1. Example 1: Add a 123 password to the root user. First enter the directory C: \ MySQL \ bin in DOS, and then type the following command:

 
 
  1. MySQLadmin -uroot -password 123  

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 password of root to 456.

 
 
  1. MySQLadmin -uroot -pab12 password 456  

(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

Connect to MySQL and enter the following MySQL command:

 
 
  1. grant select,insert,update,delete on *.* to test1@"%" Identified by "abc"; 

However, the User Added in Example 1 is very dangerous. If you want someone to know the password of test1, then he can log on to your MySQL database on any computer on the internet and

You can do whatever you want. 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 ".

Refers to 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.

The web page on the machine is accessed.

 
 
  1. 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.

 
 
  1. grant select,insert,update,delete on mydb.* to test2@localhost identified by "";  

(4) display MySQL commands

1. display the Database List:

Show databases;
At the beginning, there were only two databases: MySQL and test. The MySQL database contains the MySQL system information. We change the password and add new users to use this database for operations.

2. display the data tables in the database:

Use MySQL; // open the database

Show tables;

3. display the data table structure:

Describe table name;

4. database creation:

Create database name;

5. Create a table:

Use Database Name;

Create table Name (field setting list );

6. Delete databases and tables:

Drop database name;

Drop table name;

7. Clear records in the table:

Delete from table name;

8. Display records in the table:

Select * from table name;

The above content is an introduction to MySQL commands, and I hope you will get some benefits.

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.