MySQL - Library Operations

Table of contents

1. Library operation

1. Create a database

2. Create a database case

2. Character set and verification rules

1. View the system default character set and verification rules

2. View the character set supported by the database

3. View the character set validation rules supported by the database

4. The impact of validation rules on the database 

3. Manipulate the database

1. Check the database

2. Show create statement

3. Modify the database

4. Delete the database

5. Backup and restore

1. Database backup and recovery

2. Table backup and recovery

Fourth, check the connection


1. Library operation

1. Create a database

CREATE DATABASE [IF NOT EXISTS] db_name [[DEFAULT] CHARSET=charset_name] [[DEFAULT] COLLATE=collation_name];
-----------------------------------------------------------------------------------
create database [if not exists] db_name [[dafault] charset=charset_name] [[default]
collate=collation_name];
  • Uppercase in SQL represents keywords, and [ ] represents optional items. (Here I changed all the uppercase letters to lowercase. The lowercase letters are easier to recognize than the uppercase ones. I will give lowercase letters later for easy reading. I won’t go into details here.  )
  • CHARSET (charset) is used to specify the encoding format used by the database.
  • COLLATE (collate) is used to specify the validation rules adopted by the database

Note:  If the encoding format or verification rules of the database are not specified when creating the database, the corresponding encoding format or verification rules in the MySQL configuration file will be used by default.

2. Create a database case

Create a database named db1

create database db1;

If the MySQL configuration file has not been modified, the default encoding format is utf8, and the default verification rule is utf8_general_ci.

Create a db2 database using the utf8 character set

create database db2 charset=utf8;

charset=utf8 in SQL can also be written as character set=utf8 or character set utf8. 

Create a db3 database using the utf8 character set with the collation rule utf8_general_ci 

create database db3 charset=utf8 collate=utf8_general_ci;

collate=utf8_general_ci in SQL can also be written as collate utf8_general_ci. 

2. Character set and verification rules

1. View the system default character set and verification rules

show variables like 'character_set_database';
show variables like 'collation_database';

 The character set and verification rules viewed at this time are all under the specified database

2. View the character set supported by the database

show charset;

The character set mainly controls what language is used. For example, utf8 can use Chinese.  

3. View the character set validation rules supported by the database

show collation;

4. The impact of validation rules on the database 

  • not case sensitive

Create a database with validation rules using utf8_ general_ ci [case insensitive]

  • case sensitive

Create a database, the verification rules use utf8_ bin verification rules [case-sensitive]

        We used different verification rules to create the above two databases, created a person table, and inserted four records into it, namely a A b B; Next, we make a query on these two tables and see what the difference is.

        The test1 table adopts the default character set and verification rules. We query the record whose name is equal to a through select, and find that a and A are displayed, that is, the default verification rule utf8_general_ci is case- insensitive .

        Note: In the above figure, I only have use test1; the problem occurred: because the database is too large, that is, there are many tables in the database, so if the database information is read in advance, it will be very slow, so it is stuck, if There are very few tables in the database and there will be no problem. So add a -A;

        The verification rule adopted by the test2 table is utf8_bin. We query the record whose name is equal to a through select, and find that the displayed value is a, that is, the verification rule utf8_bin is case- sensitive .

3. Manipulate the database

1. Check the database

show databases;

2. Show create statement

show create database 数据库名;

  • MySQL recommends using uppercase for keywords in SQL, but it is not required.
  • The name of the database is backticked to prevent conflicts between the used database name and keywords.
  • /*!40100 DEFAULT CHARACTER SET utf8 */It is not a comment, it means that if the current MySQL version is greater than 4.10, the following SQL statement will be executed.

3. Modify the database

ALTER DATABASE db_name [[DEFAULT] CHARSET=character_name] [[DEFAULT] COLLATE=collation_name];
--------------------------------------------------------------------
alter database db_name [[default] charset=character_name] [[default] collate=collation_name];
illustrate:
  • The modification of the database mainly refers to modifying the character set of the database, and the verification rules

For example, change the character set of the database to gbk, and change the verification rule of the database to gbk_bin. as follows: 

4. Delete the database

DROP DATABASE [IF EXISTS] db_name;
----------------------------------
drop database [if exists] db_name;

        After deleting the database, the folder corresponding to the database will be deleted, and after deleting the database, all tables under the database will also be deleted in cascade, so do not delete the database at will.

5. Backup and restore 

Use the following command to back up the specified database: 

mysqldump -P 端口号 -u 用户名 -p 密码 -B 数据库名1 数据库名2 ...  > 数据库备份存储的文件路径

1. Database backup and recovery

 To demonstrate database backup, let's create a database and create two tables in it. as follows:

Insert two records into the stu table. as follows:

 

Also insert two records in the tec table. as follows:

At this time, execute the following command on the command line to back up the database, and specify to store the backup files in the current directory . as follows:

Open the log.sql file and you can see that the content in the file is actually the various SQL commands we execute in the database, including SQL statements such as creating a database, creating a table, and inserting data. as follows:

So far we have completed the backup of the database

In order to demonstrate the recovery of the database, we delete the test1 database we just created:

At this time, let the MySQL server execute the following command to restore the database. as follows:

When actually restoring the database, the SQL statements in the database backup file are executed sequentially, and the database is restored after execution. as follows:

At the same time, the two tables under the database and the data in the tables are also restored. as follows:

2. Table backup and recovery

table backup

Use the following command to back up the specified table:

mysqldump -P 端口号 -u 用户名 -p 密码 数据库名 表名1 表名2 ... > 表备份存储的文件路径

        If you only want to back up the stu table in the database, you can execute the following command on the command line and specify to store the backup file in the current directory . as follows:

 At this time, the SQL statements related to the stu table in history will be saved in the backup file. as follows:

 So far the backup of the table is complete! !

table recovery 

        Before restoring the table, you need to select a database first, indicating which database the table needs to be restored to. In order to prevent the restored table from duplicating the table name of the existing table in the database, you will generally choose to create an empty table when restoring the table. database, and restore the tables in that database.

Use the following command in the database to restore the specified table:

source 表备份存储的文件路径

Create an empty database test3:

Execute the following command to restore the table. as follows:

        After the SQL statement in the backup file is executed, the stu table is restored under the database, and the data in the table is also restored. as follows:

Fourth, check the connection

show processlist;

  • Id column: an identifier, which can kill the thread with the specified id through kill id in MySQL.
  • User column: Display the current user, if not root, this command will only display the SQL statements within your authority.
  • Host column: It shows which IP and which port the statement is sent from, which can be used to trace the user who made the statement in question.
  • db column: which database is the currently executed command on, if no database is specified, the value is NULL.
  • Command column: Displays the commands executed by the current connection, generally sleep (Sleep), query (Query) and connection (Connect).
  • Time column: Indicates the time that the thread is in the current state, in seconds.
  • State column: Displays the state of the SQL statement using the current connection.
  • Info column: Generally, it records the statements executed by the thread. By default, only the first 100 characters are displayed. If you want to see all the information, you need to use show full processlist.

        show processlist can tell us which users are currently connected to our MySQL. If you find out that a user is not logged in normally, it is very likely that your database has been invaded. If you find your database is slow in the future, you can Use this SQL to view the database connection. 

Guess you like

Origin blog.csdn.net/sjsjnsjnn/article/details/128754494