Linux MySQL Command Overview

Source: Internet
Author: User
Tags mysql host mysql tutorial import database mysql command line

I. Summary:

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 database name;
5. Create a table:
Use Database Name;
Create Table Name (field setting list );
6. Delete databases and tables:
Drop database 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;

9. Encoding Modification
If you want to change the entireMySQLOfEncodingFormat:
StartMySQLWhen the mysqld_safe command line is added
-- Default-character-set = GBK

If you want to changeEncodingFormat: InMySQLEnter the command after the prompt
Alter database db_name default Character Set GBK;

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

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.

Grant select, insert, update, delete on mydb
. * To Test2 @ localhost identified \"\";

The above describes logon, user addition, password change, and other issues. Next, let's take a look at the 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 use a semicolon as the end mark to complete the operation.

2. You can use the cursor to bring up or down the previous commands. However, an old MySQL version I used earlier does not support this feature. I am using a mysql-3.23.27-beta-win.

Ii. Display 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. If you have learned FOXBASE, you will not be unfamiliar with it.

Show tables;

3. display the data table structure:

Describe table name;

4. database creation:

Create Database database name;

5. Create a table:

Use Database Name;

Create Table Name (field setting list );

6. Delete databases and tables:

Drop database 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;

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

Drop database if exists school; // Delete if school exists

Create Database school; // create a database School

Use school; // open the school library

Create Table teacher // create table teacher

(

Id int (3) auto_increment not null primary key,

Name char (10) Not null,

Address varchar (50) default 'shenzhen ',

Year date

); // Table creation ends

// Insert fields as follows

Insert into teacher values ('', 'glengang ', 'shenzhen Zhongyi', '2017-10-10 ');

Insert into teacher values ('', 'jack', 'shenzhen Zhongyi ', '2017-12-23 ');

Note: In the table in progress (1), set the ID to a numeric field of 3: int (3) and make it automatically add one: auto_increment for each record. It cannot be blank: not null and set it as the main field primary key (2) Set name to the character field with a length of 10 (3) set address to the character field with a length of 50, the default value is Shenzhen. What is the difference between varchar and Char?ArticleAgain. (4) set year as the date field.

If you type the preceding command at the MySQL prompt, debugging is not convenient. You can write the above commands into a text file as they are. SQL, then copy to C :\\, and enter the directory \ mysql \ bin in DOS status, and then type the following command:

Mysql-uroot-P password <c: \ school. SQL

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

 

 

4. Transfer text data to the database

1. Text data should conform to the format: field data is separated by the tab key, and the null value is replaced by \ n.

Example:

3 ROSE:

4 MIKE: Shenzhen No. 1,-12-23

2. Data Import command load data local infile \ "file name \" into Table table name.

Note: You 'd better copy the file to the \ mysql \ bin directory and use the use command to create the database where the table is located.

V. Back up the database:

1. mysqldump -- opt school> school. bbb

Mysqldump -- opt school> school. bbb
(The command is executed in the \ mysql \ bin directory of DoS)

Note: Back up the database school to the school. BBB file. School. BBB is a text file with any file name. Open it and you will find new discoveries.

Note: In fact, MySQL's database operations are similar to those of other SQL databases. You 'd better read this SQL book. Here I will only introduce some basic things. In fact, I only understand these things. The best MySQL tutorial is the MySQL Chinese reference manual translated by Yan Zi. It is not only free to download from every related website, but also the most authoritative. Unfortunately, it is not in the chm format like \ "PhP4 Chinese album \", and it is not convenient to search for function commands.

2. Change the logon Password

1) mysqladmin-u username-P old Password new password:

Example: mysqladmin-u Root Password 21 Century

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

2) directly modify the root user password of the User table:

Mysql> User MySQL;
Mysql> Update user set pasword = PASSWORD ('21century ') where user = 'root ';
Mysql> flush privileges;

Note: flush privileges indicates force refresh of the memory authorization table. Otherwise, the buffer password is used.

3. test whether the password is successfully modified:

1) login without a password:

[Root @ test1 local] # MySQL
Error 1045: Access denied for user: 'root @ localhost' (using password: No)

An error is displayed, indicating that the password has been modified.

2) log on with the modified password:

[Root @ test1 local] # mysql-u root-P
Enter Password: (enter the modified password 21 century)
Welcome to the MySQL monitor. commands end with; or \ G.
Your MySQL connection ID is 177 to server version: 3.23.48
Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
Mysql>

Successful!

You can use the mysqladmin command to change the password or the database to change the password.

 

 

4. Start and Stop:

Start: MySQL has been changed since version 3.23.15. By default, the service is started by mysql users after installation, and cannot be started by root users.

If you have to use the root user for startup, you must add the -- user = Root parameter (./safe_mysqld -- user = root &) to stop: mysqladmin-u root-P shutdown.

5. Export the meeting database:

Mysqldump-uroot-p21century meeting> db_meeting. SQL

Dbname database:

Mysqldump-uroot-p21century dbname <XXX. SQL

Importing a database can also execute a large number of SQL statements at a time, similar to @ my_script. SQL in Oracle, which is very useful when mysqldump does not work.

Example: #./MySQL-uroot-P

(Note: The create database, use databasename, create table, and insert into statements can all be written in the above step file)

6. Rename the table:

Rename table ztemp to ztemp4;

7. Modify Field attributes:

Alter table bbabase change news_id ID varchar (5) not null;

8. Add a field after the content in the table:

Alter table bbabase add leave_time datetime not null after

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.