Linux mysql commands

Source: Internet
Author: User
Tags mysql commands mysql host import database

Linux mysql commands

1. commands for starting mysql in linux:

Mysqladmin start

/Ect/init. d/mysql start (the installation path of mysql is earlier)

2. Command for restarting mysql in linux:

Mysqladmin restart

/Ect/init. d/mysql restart (the installation path of mysql is earlier)

3. Run the following command to disable mysql in linux:

Mysqladmin shutdown

/Ect/init. d/mysql shutdown (the installation path of mysql is earlier)

4. Connect to mysql on the local machine:

Enter the mysql \ bin directory, type the mysql-uroot-p command, and press enter to enter the password.

Exit mysql command: exit (Press ENTER)

5. Modify the mysql password:

Mysqladmin-u username-p old password New password

Or enter the mysql command line set password for root = PASSWORD ("root ");

6. Add new users. (Note: commands in the mysql environment are followed by a semicolon as the command Terminator)

Grant select on database. * to username @ login host identified by "password"

For example, if a user's test password is added to 123, 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 to connect to mysql, and then type the following command:

Grant select, insert, update, delete on *. * to "Identified by" 123 ";

Ii. mysql database operations

You must first log on to mysql. The related operations are performed at the mysql prompt, and each command ends with a semicolon.

1. display the Database List.

Show databases;

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;

GBK: create database test2 default character set gbk COLLATE gbk_chinese_ci;

UTF8: create database 'test2' default character set utf8 COLLATE utf8_general_ci;

5. Create a table:

Use Database Name;

Create table Name (field setting list );

Linux mysql commands

6. Delete databases and tables:

Drop database name;

Drop table name;

7. Clear records in the table:

Delete from table name;

Truncate table name;

8. Display records in the table:

Select * from table name;

9. Encoding Modification

To change the encoding format of mysql:

When mysql is started, add the mysqld_safe command line

-- Default-character-set = gbk

To change the encoding format of a Database: Enter the command at the mysql prompt

Alter database db_name default character set gbk;

10. rename a table

Alter table t1 rename t2;

11. View SQL statement Efficiency

Explain <table_name>

Example: explain select * from t3 where id = 3952602;

12. load data into database tables in text mode (for example, D:/mysql.txt)

Mysql> load data local infile "D:/mysql.txt" into table mytable;

Iii. Data Import and Export

1. Transfer text data to the database

Text data should conform to the format: field data is separated by the tab key, and null value is used instead. Example:

1 name duty 2006-11-23

Data Import command load data local infile "file name" into table name;

2. Export databases and tables

Mysqldump -- opt news> news. SQL (back up all the tables in the database news to the news. SQL file. news. SQL is a text file with any file name .)

Mysqldump -- opt news author article> author. article. SQL (back up the author table and article table in the database news to author. article. SQL file, author. article. SQL is a text file with any file name .)

Mysqldump -- databases db1 db2> news. SQL (back up database dbl and db2 to the news. SQL file. news. SQL is a text file with any file name .)

Mysqldump-h host-u user-p pass -- databases dbname> file. dump

Import the database dbname on the host named "user" and "password pass" to file. dump.

Mysqldump -- all-databases> all-databases. SQL (back up all databases to all-databases. SQL files, a all-databases. SQL is a text file, any file name .)

3. Import Data

Mysql <all-databases. SQL (import database)

Mysql> source news. SQL; (run the mysql command to import tables)

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 directory mysqlbin, then type the command mysql-uroot-p, and press enter to prompt you to enter the password. If you have just installed MYSQL, super User root has no password, so 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 110.110.110.110, the user name is root, and the password is abcd123. Enter the following command:

Mysql-h110.110.110.110-uroot-pabcd123

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

3. exit MYSQL command: exit (Press ENTER ).

Ii. Change the password

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

1. Example 1: Add a password ab12 to the root user. First, enter the directory mysqlbin in DOS, and then type the following command:

Mysqladmin-uroot-password ab12

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

Mysqladmin-uroot-pab12 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 \"

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 to connect to MySQL, and then type the following command:

Grant select, insert, update,

Delete on *. * to test1 @ \ "% \" Identified by \ "abc \";

However, the User Added in Example 1 is 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. 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, 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), so that the user knows the password of test2, he cannot access the database directly from the internet, but can only access the database through the web pages on the MySQL host.

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.