[Turn]mysql Common Command Collection [absolute essence]

Source: Internet
Author: User
Tags mysql host mysql command line

Test environment: MySQL 5.0.45 "NOTE: You can use mysql> Select version () in MySQL to view the database version"

First, connect MySQL.

Format: mysql-h host address-u user name-P user Password

1. Connect to MySQL on this machine.

First open the DOS window, then enter the directory Mysql\bin, and then type the command Mysql-u root-p, enter after the prompt you to lose the password. Note that the user name can have a space or no space, but before the password must have no space, or let you re-enter the password.

If you have just installed MySQL, superuser root is no password, so the direct return to enter the MySQL, MySQL prompt is: mysql>

2. Connect to MySQL on the remote host. Assume the remote host IP is: 110.110.110.110, the user name is root, the password is abcd123. Type the following command:

Mysql-h110.110.110.110-u Root-p 123; (Note: You can not add a space between the root and the other)

3. Exit MySQL command: Exit (enter)

Second, change the password.

Format: Mysqladmin-u username-P Old password password new password

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

Mysqladmin-u Root-password AB12

Note: Because Root does not have a password at the beginning, the-p old password can be omitted.

2, then change the root password to djg345.

Mysqladmin-u root-p ab12 Password djg345

third, add new users.
(Note: Unlike the above, the following is because it is a command in a MySQL environment, so it is followed by a semicolon as a command terminator)

Format: Grant Select on database. * To User name @ login host identified by "password"

1, add a user test1 password for ABC, so that he can log on any host, and all databases have query, insert, modify, delete permissions. First connect to MySQL with the root user, and then type the following command:

Grant Select,insert,update,delete on * * to [e-mail protected] "%" identified by "ABC";

But the added user is very dangerous, you want to like someone to know test1 password, then he can be on any computer on the Internet to log into your MySQL database and your data can do whatever you like, solution see 2.

2, add a user test2 password for ABC, so that he can only login on localhost, and the database mydb can query, insert, modify, delete operations (localhost refers to the local host, that is, the MySQL database host),

This allows the user to use a password that knows test2, and he cannot access the database directly from the Internet, but only through a Web page on the MySQL host.

Grant Select,insert,update,delete on mydb.* to [e-mail protected] identified by "ABC";

If you do not want to test2 have a password, you can call another command to erase the password.

Grant Select,insert,update,delete on mydb.* to [e-mail protected] identified by "";

Next I am MySQL on the database aspects of the operation. Note: You must first log in to MySQL, the following actions are performed at the prompt of MySQL, and each command ends with a semicolon.

First, the Operation skill

1, if you hit the command, enter after the discovery forgot to add a semicolon, you do not have to re-play the command, as long as a semicolon to enter the return on it.

In other words, you can break a complete command into a few lines, and then use a semicolon to make the end sign OK.

2. You can use the cursor up and down keys to recall the previous command.

Second, show the command

1. Displays the list of databases in the current database server:

Mysql> SHOW DATABASES;

Note: MySQL library has the MySQL system information, we change the password and new users, is actually using this library to operate.

2. Display the data table in the database:

mysql> use library name;
Mysql> SHOW TABLES;

3, display the structure of the data table:

mysql> DESCRIBE table name;

4. Establish the database:

mysql> CREATE database name;

5. Set up the data sheet:

mysql> use library name;
mysql> CREATE table name (field name VARCHAR (20), Field name CHAR (1));

6. Delete the database:

mysql> DROP database name;

7. Delete Data sheet:

mysql> DROP table name;

8. Empty the records in the table:

Mysql> DELETE from table name;

9. Display the records in the table:

Mysql> SELECT * from table name;

10. Insert a record into the table:

mysql> INSERT into table name VALUES ("HyQ", "M");

11. Update the data in the table:

mysql-> UPDATE table name SET field name 1= ' A ', field name 2= ' B ' WHERE field name 3= ' C ';

12. Load data into the data table in text mode:

mysql> LOAD DATA LOCAL INFILE "d:/mysql.txt" into table name;

13. Import the. sql File command:

mysql> use database name;
Mysql> SOURCE D:/mysql.sql;

14, the command line to modify the root password:

mysql> UPDATE mysql.user SET password=password (' New password ') WHERE user= ' root ';
mysql> FLUSH privileges;

15. Display the database name of use:

Mysql> SELECT DATABASE ();

16. Display the current User:

Mysql> SELECT USER ();

A city build and build tables and instances of inserting data

Drop database if exists school; Delete if school is present

Create Database School; Building a library School

Use school; Open Library School

CREATE TABLE teacher//Create tables Teacher
(
ID int (3) auto_increment NOT null primary key,
Name Char (TEN) is not NULL,
Address varchar (+) Default ' Shenzhen ',
Year Date
); End of Build table

The following is the Insert field
Insert into teacher values (", ' Allen ', ' Dalian One ', ' 1976-10-10′ ');
Insert into teacher values (", ' Jack ', ' Dalian II ', ' 1975-12-23′ ');

It is also possible to type the above commands at the MySQL prompt, but it is not easy to debug.

(1) You can write the above command as-is to a text file, assume School.sql, then copy to c:\\, and enter directory \\mysql\\bin in DOS, and then type the following command:

Mysql-uroot-p Password < C:\\school.sql

If successful, empty a row without any display, and if there is an error, there is a hint. (The above command has been debugged, you can use it only if you remove//comment).

(2) or enter the command line after using mysql> source C:\\school.sql; You can also import the School.sql file into the database.

Iv. transferring text data to the database

1, the text data should conform to the format: The field data is separated by the TAB key, the null value is replaced by \\n. Example:

3 Rose Dalian II 1976-10-10

4 Mike Dalian One 1975-12-23

Suppose you save these two sets of data as school.txt files, placed in the C packing directory.

2, the data incoming command, load data local infile "C:\\school.txt" into the table name;

Note: You might want to copy the file to the \\mysql\\bin directory, and use the using command to hit the library that contains the table.

++++++++++++++++++++++++++++++++++++++

(If you enter it one by one, it is troublesome.) We can add all the records to your database table in the form of a text file. Create a text file "Mysql.txt", each line containing a record, with a locator (tab) to separate the values. )

Client clients Chinese characters garbled problem: [<mysql installation directory has a configuration file My.ini, which has the client and server-side encoding settings.

Your default encoding on the server is set to UTF8, then the storage data is UTF8, this is true, but if you want to view the data from the MySQL Command line client, that is, clients, if the default encoding set by the client is also UTF8, then there will be garbled, So the default encoding of the client is modified to GBK, and does not affect the encoding used by the data store, but only when we view the data with MySQL Command line client, we return the encoded Chinese that we converted to GBK.
This does not need to set names every time "GBK".

The specific settings are as follows:
In My.ini:

Client-side:

[Client]

port=3306

[MySQL]

Default-character-set=gbk


Service-side:

[Mysqld]

# The default character set that would be used if a new schema or table is
# created and no character set is defined
Default-character-set=utf8>]

++++++++++++++++++++++++++++++++++++++

V. BACKUP DATABASE: (command executed in DOS \\mysql\\bin directory)

1. Export the entire database

The export file is present in the Mysql\bin directory by default

Mysqldump-u user name-p database name > exported file name

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

MYSQLDUMP-U USER_NAME-P database_name table_name > OUTFILE_NAME.SQL

3. Export a database structure

Mysqldump-u user_name-p-d–add-drop-table database_name > Outfile_name.sql

-D No data –add-drop-table add a drop table before each CREATE statement

4. Export with language parameters

mysqldump-uroot-p–default-character-set=latin1–set-charset=gbk–skip-opt database_name > Outfile_name.sql

1. Backing Up the database
Mysqldump-uroot-p test_db > Test_db.sql
2. Recovering a Database
Mysql-uroot-p test_db < Test_db.sql
3. Create permissions
Grant all privileges the test_db.* to [email protected] ' localhost ' identified by ' 123456 ';
Compatible with mysql4.1 before mode:
Update Mysql.user set Password=old_password (' 123456 ') where user= ' test_db ';
4. Forgot password
Add "Skip-grant-tables" to the "mysqld" configuration section of the "my.cnf" or "My.ini" file and then restart MySQL to log in to modify the root password.

[Turn]mysql Common Command Collection [absolute essence]

Related Article

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.