Common MySQL operations

Source: Internet
Author: User
Tags mysql host
Do you have a headache for obtaining common MySQL operations? If this is the case, the following articles will give you corresponding solutions. The following articles mainly introduce common MySQL operations, and the following are specific descriptions of related content. Note: Each Command in MySQL must end with a semicolon. 1. display the database mysqlshowdatabases;

Do you have a headache for obtaining common MySQL operations? If this is the case, the following articles will give you corresponding solutions. The following articles mainly introduce common MySQL operations, and the following are specific descriptions of related content. Note: Each Command in MySQL must end with a semicolon. 1. display the database mysql> showdatabases;

Do you have a headache for obtaining common MySQL operations? If this is the case, the following articles will give you corresponding solutions. The following articles mainly introduce common MySQL operations, and the following are specific descriptions of related content.

Note: Each Command in MySQL must end with a semicolon.

1. display the database

 
 
  1. mysql> show databases;
  2. +----------+
  3. | Database |
  4. +----------+
  5. | mysql|
  6. | test|
  7. +----------+
  8. 2 rows in set (0.04 sec)

Mysql has just been installed with two databases: mysql and test. The mysql database is very important. It contains MySQL system information. We change the password and add new users. In fact, we use the relevant tables in this database for operations.

2. Common MySQL operations; displays tables in the database

 
 
  1. Mysql> use mysql; (open the database. to operate on each database, open the database, similar to foxpro)
  2. Database changed
  3. Mysql> show tables;
  4. + ----------------- +
  5. | Tables_in_mysql |
  6. + ----------------- +
  7. | Columns_priv |
  8. | Db |
  9. | Func |
  10. | Host |
  11. | Tables_priv |
  12. | User |
  13. + ----------------- +
  14. 6 rows in set (0.01 sec)

3. display the data table structure:

 
 
  1. Describe table name;

4. display the records in the table:

 
 
  1. Select * from table name;

For example, the user table records in the mysql database are displayed. All users who can operate on MySQL users are in this table.

 
 
  1. Select * from user;

5. database creation:

 
 
  1. Create database name;

For example, create a database named aaa

 
 
  1. mysql> create databases aaa;

6. Create a table:

 
 
  1. Use Database Name;
  2. Create table Name (field setting list );

For example, if you create a table name in the newly created aaa database, the table has four fields: id (serial number, auto-increment), xm (name), xb (gender), and csny (date of birth ).

 
 
  1. use aaa;
  2. mysql> create table name (id int(3) auto_increment not null primary key, xm char(8),xb char(2),csny date);

You can use the describe command to view the created table structure.

 
 
  1. mysql> describe name;
  2. +-------+---------+------+-----+---------+----------------+
  3. | Field | Type| Null | Key | Default | Extra |
  4. +-------+---------+------+-----+---------+----------------+
  5. | id| int(3) | | PRI | NULL| auto_increment |
  6. | xm| char(8) | YES || NULL||
  7. | xb| char(2) | YES || NULL||
  8. | csny | date| YES || NULL||
  9. +-------+---------+------+-----+---------+----------------+

7. Common MySQL operations: Add records

For example, add several related records.

Mysql> insert into name values ('', 'zhang san', 'mal', '2017-10-01 ');

Mysql> insert into name values ('', 'baiyun ', 'female', '2017-05-20 ');

The select command can be used to verify the result.

 
 
  1. Mysql> select * from name;
  2. + ---- + ------ + ------------ +
  3. | Id | xm | xb | csny |
  4. + ---- + ------ + ------------ +
  5. | 1 | Zhang San | male |
  6. | 2 | Baiyun | female | 1972-05-20 |
  7. + ---- + ------ + ------------ +

8. Modify records

For example, change the date of birth of John

 
 
  1. Mysql> update name set csny = '2017-01-10 'where xm = 'zhang san ';

9. delete records

For example, delete the records of Michael Jacob.

 
 
  1. Mysql> delete from name where xm = 'zhang san ';

10. Delete databases and tables

 
 
  1. Drop database name;
  2. Drop table name;

9. Add MySQL users

Format: grant select on database. * to username @ login host identified by "password"

Example 1: Add a user user_1 with a password of 123 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. mysql> grant select,insert,update,delete on *.* to user_1@"%" Identified by "123";

In example 1, the added user is very dangerous. If you know the user_1 password, then he can log on to your MySQL database on any computer on the Internet and do whatever he wants. For the solution, see Example 2.

Example 2: Add a user_2 password of 123 so that the user can only log on to localhost, you can also query, insert, modify, and delete the database aaa (localhost refers to the local host, that is, the host where the MySQL database is located), so that the user knows the password of user_2, he cannot directly access the database from the Internet, and can only operate the aaa database through the MYSQL host.

 
 
  1. mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123";

If a new user cannot log on to MySQL, run the following command during logon:

 
 
  1. Mysql-u user_1-p-h 192.168.113.50 (-h is followed by the IP address of the host to be logged on)

10. Common MySQL operations: backup and recovery

1. Backup

For example, back up the aaa library created in the previous example to the back_aaa file.

[Root @ test1 root] # cd/home/data/mysql (go to the database directory, this example library has been transferred from val/lib/mysql to/home/data/mysql, see section 7 above)

 
 
  1. [root@test1 mysql]# mysqldump -u root -p --opt aaa > back_aaa

2. Recovery

 
 
  1. [root@test mysql]# mysql -u root -p ccc < back_aaa

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.