Library related operations

Table of contents

1. Create a database

1. Create database rules

2. Create a case

2. Character set and verification rules

1. Check the system default character set and verification rules

2. Check the character sets and verification rules supported by the database

3. The impact of verification rules on the database

3. Manipulate the database

1. View the database and the current database

2. Display the creation statement

3. Modify the database

4. Delete the database

5. Backup and recovery

6. Check the connection status

1. Create a database

1. Create database rules

grammar:

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]

create_specification:
    [DEFAULT] CHARACTER SET charset_name
    [DEFAULT] COLLATE collation_name
illustrate:
Capital letters indicate keywords;
[] is optional;
CHARACTER SET: Specifies the character set used by the database;
COLLATE: Specify the verification rules of the database character set;

2. Create a case

Create a database named db1
create database db1;
Create a db2 database using utf8 character set
create database db2 charset=utf8;
Create a db3 database using utf character set and proofreading rules
create database db3 charset=utf8 collate utf8_general_ci;
When we create a database without specifying the character set and verification rules, the system uses the default character set: utf8, verification rules
是:utf8_general_ci。

2. Character set and verification rules

The character set of the database refers to the encoding method used for characters stored in the database . Different character sets can represent different character ranges and sizes. The verification rules of the database refer to the rules followed when comparing and sorting characters in the database . Different verification rules will affect the query results and performance.

1. Check the system default character set and verification rules

show variables like 'character_set_database';

show variables like 'collation_database';

2. Check the character sets and verification rules supported by the database

show charset;

\

show collation;

3. The impact of verification rules on the database

(1) Create a database and use utf8_ general_ ci [ case-insensitive ] for verification rules.
create database option1 collate utf8_general_ci;
use option1;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');

(2) Create a database and use utf8_bin [ case-sensitive] for verification rules.
create database option2 collate utf8_bin;
use option2;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');

(3) Search and sort the tables in the two databases

The database option1 verification rule uses utf8_ general_ ci [ case-insensitive ]

use option1;
mysql> select * from person where name='a';
select * from person order by name;

The database option2 verification rule uses utf8_ general_ ci [ case-sensitive ]

use option2;
mysql> select * from person where name='a';
select * from person order by name;

3. Manipulate the database

1. View the database and the current database

show databases;
select database();

2. Display the creation statement

show create database option1;

3. Modify the database

Modifying the database mainly refers to modifying the character set and verification rules of the database

ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
alter database option1 charset=gbk;

The above code changes the option1 database character set to gbk.

4. Delete the database

drop database option2;
Result after deletion:
(1) The corresponding database cannot be seen inside the database
(2) The corresponding database folder is deleted, cascade deleted, and all data tables inside are deleted.
Note: Do not delete the database at will

5. Backup and recovery

(1) Backup

mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径

Example: Back up the option1 library to a file (exit the connection)

mysqldump -P3306 -uroot -p -B option1 >test1.sql

You can see that after the backup, there is a test.sql file in the path. Next, use vim to open it and take a look at the content.

At this time, you can open and take a look at the contents of the test1.sql file. In fact, all our statements for creating a database, building tables, and importing data are loaded into this file.

2 (restore database)

source /var/lib/mysql/test1.sql;

(3) Precautions

If the backup is not the entire database, but one of the tables
 mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
Back up multiple databases simultaneously
# mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径
If you back up a database without the -B parameter, when restoring the database, you need to create an empty database first , then use the database, and then use source to restore.

6. Check the connection status

show processlist;

It can tell us which users are currently connected to our MySQL . If it is found that a certain user is not your normal login, it is very likely that your database has been invaded. In the future, when you find that your database is slow, you can use this command to check the database connection status.

 

Guess you like

Origin blog.csdn.net/weixin_65592314/article/details/132706510