The actual operation process of common MySQL commands

Source: Internet
Author: User
Tags mysql commands mysql host
The following articles mainly introduce the actual operations of MySQL commands frequently used in MySQL databases, including how to correctly connect to the MySQL database on the local machine and how to change the password correctly, the following describes how to add a new user. 1. Connect to MySQL on the local machine. First open the DOS window and then enter

The following articles mainly introduce the actual operations of MySQL commands frequently used in MySQL databases, including how to correctly connect to the MySQL database on the local machine and how to change the password correctly, the following describes how to add a new user. 1. Connect to MySQL on the local machine. First open the DOS window and then enter

The following articles mainly introduce the actual operations of MySQL commands frequently used in MySQL databases, including how to correctly connect to the MySQL database on the local machine and how to change the password correctly, the following describes how to add a new user.

1. Connect to MySQL on the local machine.

First, open the DOS window, enter the MySQL \ bin directory, then enter MySQL Command MySQL-u root-p, and press enter to prompt you to enter the password. note that there can be space or space before the user name, but there must be no space before the password; otherwise, you can re-enter the password.

If you have just installed MySQL, the Super User root has no password, so press enter to enter MySQL. The MySQL prompt is: MySQL>

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-u root-p 123)

3. exit MySQL command: exit (Press ENTER)

2. Change the password.

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

1. Add a password ab12 to the root user. First, enter the MySQL \ bin directory under DOS, and then type the following command

MySQLadmin-u root-password ab12

Note: because the root account does not have a password at the beginning, the old-p password can be omitted.

2. Change the root password to djg345.

MySQLadmin-u root-p ab12 password djg345

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"

1. Add a user test1 with a password abc so that he 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:

 
 
  1. grant select,insert,update,delete on *.* to [email=test1@”%]test1@”%[/email]” Identified by “abc”;

However, the added users are 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. See solution 2.

2. Add a user named "test2" with the password "abc" so that the user can only log on to localhost, you can also query, insert, modify, and delete the database mydb (localhost 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. He can only access the database through the web page on the MySQL host.

 
 
  1. grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “abc”;

If you do not want test2 to have a password, you can use another MySQL command to cancel the password.

 
 
  1. grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “”;

Next we will talk about database operations in MySQL. Note: you must first log on to MySQL. The following operations are performed at the MySQL prompt and each command ends with a semicolon.

I. Operation Skills

1. If you forget the extra points after you press Enter when making the command, you don't have to repeat the command. You just need to press a semicolon to press Enter.

That is to say, you can divide a complete command into several lines, and then use a semicolon to mark the end.

2. You can use the cursor to bring up or down the previous MySQL command.

Ii. Display commands

1. display the list of databases on the current database server:

 
 
  1. MySQL> SHOW DATABASES;

Note: the MySQL database contains the MySQL system information. We change the password and add new users to use this database for operations.

2. display data tables in the database:

 
 
  1. MySQL> USE Database Name;
  2. MySQL> show tables;

3. display the data table structure:

 
 
  1. MySQL> DESCRIBE table name;

4. Create a database:

 
 
  1. MySQL> create database name;

5. Create a data table:

 
 
  1. MySQL> USE Database Name;
  2. MySQL> create table Name (field name VARCHAR (20), field name CHAR (1 ));

6. delete a database:

 
 
  1. MySQL> drop database name;

7. delete a data table:

 
 
  1. MySQL> drop table name;

8. Clear records in the table:

 
 
  1. MySQL> delete from table name;

9. display the records in the table:

 
 
  1. MySQL> SELECT * FROM table name;

10. insert records into the table:

 
 
  1. MySQL> insert into table name VALUES ("hyq", "M ");

11. Update table data:

 
 
  1. MySQL-> UPDATE table name: SET field name: 1 = 'a'; field name: 2 = 'B' WHERE field name: 3 = 'C ';

12. load data into a data table in text mode:

 
 
  1. MySQL> load data local infile "D:/MySQL.txt" into table name;

13. Import the. SQL FILE command:

 
 
  1. MySQL> USE Database Name;
  2. MySQL> SOURCE d:/MySQL. SQL;

14. Modify the root password on the MySQL command line:

 
 
  1. MySQL> UPDATE MySQL. user SET password = PASSWORD ('new password') WHERE User = 'root ';
  2. MySQL> flush privileges;

15. display the Database Name of use:

 
 
  1. MySQL> SELECT DATABASE();

16. display the current user:

 
 
  1. MySQL> SELECT USER();

3. An instance for creating a database, creating a table, and inserting data

 
 
  1. drop database if exists school;

If SCHOOL exists, delete it.

 
 
  1. create database school;

Create a library SCHOOL

 
 
  1. use school;

Open Library SCHOOL

 
 
  1. create table teacher

Create Table TEACHER

 
 
  1. (
  2. Id int (3) auto_increment not null primary key,
  3. Name char (10) not null,
  4. Address varchar (50) default 'deep secret ',
  5. Year date
  6. );

Table creation ends

Insert fields as follows

 
 
  1. Insert into teacher values (", 'allen ', 'dalian Zhongyi', '2017-10-10 ′);
  2. Insert into teacher values (", 'jack', 'dalian erzhong ', '2017-12-23 ′);

If you type the preceding command at the MySQL prompt, debugging is not convenient.

(1) You can write the above commands into a text file as they are, for example, school. SQL, then copy it to c :\\, and enter the directory [url = file: // \ MySQL \ bin] \ MySQL \ bin [/url], and then type the following command:

MySQL-uroot-p password <c: \ school. SQL

If a row is successfully displayed, no display is displayed. If an error occurs, a prompt is displayed. (The preceding command has been debugged. You only need to remove the // annotation to use it ).

(2) You can use MySQL> source c: \ school. SQL after entering the command line. You can also import the school. SQL file to the database.

4. convert text data to the Data Warehouse

1. Text data should conform to the format: field data is separated by the tab key, and null values are separated by [url = file: // \ n] \ n [/url] instead. example:

3 rose Dalian No. 2, 1976-10-10

4 mike Dalian No. 1 1975-12-23

Assume that you save these two sets of data as a school.txt file and put it under the c-drive root directory.

2. Data Input MySQL Command

 
 
  1. Load data local infile "c: \ school.txt" into table name;

Note: You 'd better copy the file to the [url = file: // \ MySQL \ bin] \ MySQL \ bin [/url] Directory, use the use command to create the database where the table is located.

V. Back up the database: (run the command in the DOS [url = file: // \ MySQL \ bin] \ MySQL \ bin [/url] Directory)

1. Export the entire database

The exported files are stored in the MySQL \ bin directory by default.

MySQLdump-u username-p Database Name> exported file name

 
 
  1. MySQLdump -u user_name -p123456 database_name > outfile_name.sql

2. Export a table

MySQLdump-u user name-p database name Table Name> exported file name

 
 
  1. MySQLdump -u user_name -p database_name table_name > outfile_name.sql

3. Export a database structure

 
 
  1. MySQLdump -u user_name -p -d –add-drop-table database_name > outfile_name.sql

-D does not have data.-add-drop-table adds a drop table before each create statement.

4. Export with language Parameters

 
 
  1. MySQLdump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql

Vi. Database Remote logon permission settings

 
 
  1. GRANT ALL ON *.* TO MySQL@'%' IDENTIFIED BY 'MySQL123' WITH GRANT OPTION

The above content is an introduction to the common MySQL commands.

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.