MySQL study notes. Data backup and recovery

data backup

Overview

Data backup is very important. A series of reasons such as system crash may cause data loss in the database. Therefore, the database should be backed up regularly to minimize the loss when unexpected situations occur.

Use MySQLdump command to backup

MySQLdump is a very useful database backup tool provided by MySQL. When using this tool, you can generate a text file that contains multiple CREATE and INSERT statements, which can be used to recreate tables and insert data.
Syntax format: mysqldump -u root -p 数据库名 >路径/yingmo.sql
Here we first create a database named yingmo with three tables.
Insert picture description here
1. MySQLdump backups all tables in a single database

mysqldump -u root -p yingmo >F:/yingmo.sql
Enter password: ****

At this time, a file named yingmo.sql will be generated in the local folder.
Insert picture description here
Open it and take a look.
Insert picture description here
2. MySQLdump backs up a table
in the database. Backs up the student table in the yingmo database.

mysqldump -u root -p yingmo student>F:/yingmo.sql
Enter password: ****

Insert picture description here
3.MySQLdump back up multiple databases
to back up multiple databases, separated by a space between the need to use multiple databases -databases parameter name
syntax mysqldump -u root -p --databases 数据库1 数据库2>F:/yingmo.sql
and then create a database named wb and create a table

mysqldump -u root -p --databases yingmo wb >F:/yingmo.sql
Enter password: ****

Insert picture description here
4. MySQLdump backs up all databases.
Syntax format: mysqldump -u root -p --all-databases >F:/yingmo.sql
no need to specify the database name

Data Recovery

Overview

On the basis of the above-mentioned data that has been backed up, data loss is minimized by restoring the data.
We already know when backing up the data above, that the CREATE and INSERT statements are included during the backup, so you can use the MySQL statement to restore.

MySQL command recovery

Here first delete the previously created database, including: yingmo wb

mysql> drop database yingmo;
Query OK, 7 rows affected (0.05 sec)

mysql> drop database wb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kefu               |
| mysql              |
+--------------------+
3 rows in set (0.00 sec)

The following starts to restore
Syntax format: mysql -u root -p < F:/yingmo.sqlno need to specify the database name

C:\Users\acer>mysql -u root -p < F:/yingmo.sql
Enter password: ****

C:\Users\acer>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 5.1.60-community-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kefu               |
| mysql              |
| wb                 |
| yingmo             |
+--------------------+
5 rows in set (0.00 sec)

mysql> use yingmo;
Database changed
mysql> show tables;
+------------------+
| Tables_in_yingmo |
+------------------+
| course           |
| sc               |
| student          |
| view_003         |
| view_c001        |
| view_male        |
| view_sg          |
+------------------+
7 rows in set (0.00 sec)

OK has been restored.

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/109602739