MySQL full backup + binary log recovery


The following problems may occur during the use of the database. How to solve them?

All preparations were made at 10:30 on Friday, but a misoperation occurred at 11 o'clock, deleting all the data in the student table. How to quickly restore the data to the state before deletion?

At this time, you can use MySQL full backup + binary log to restore to the state before the data was deleted.
Let's simulate this situation:

Prerequisite configuration: turn on binary log

[root@mysql mysql]# vim /etc/my.cnf
[mysqld]  #在mysqld下配置logbin
...
#log bin  
log_bin
server_id = 1

1. Create a new library

root@(none) 10:15  mysql>create database sc;
Query OK, 1 row affected (0.00 sec)

Create a table and insert data:

root@(none) 10:15  mysql>use sc
Database changed
root@sc 10:16  mysql>create table student(id int primary key, name varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)

root@sc 10:18  mysql>insert  into student values(1,'lisi'),(2,'wangwu'),(3,'zhangsan');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
root@sc 10:19  mysql>select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | lisi     |
|  2 | wangwu   |
|  3 | zhangsan |
+----+----------+
3 rows in set (0.00 sec)

2. Full backup of data

Use mysqldump to prepare all data:

mysqldump -u[MySQL user name] -p[password] [database name] [table name] > [backup file name]

[root@mysql backup]# pwd
/backup
[root@mysql backup]# mysqldump -uroot -p"123456" sc  student > sc_student.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

3. Simulation data changes

root@sc 10:27  mysql>insert  into student values(4,'xiaoming');
Query OK, 1 row affected (0.01 sec)

root@sc 10:28  mysql>update student set name='xiaohong' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
root@sc 10:29  mysql>select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | xiaohong |
|  2 | wangwu   |
|  3 | zhangsan |
|  4 | xiaoming |
+----+----------+
4 rows in set (0.00 sec)

4. Simulation operation errors

Delete all data in the table:

root@sc 10:57  mysql>delete from student;
Query OK, 4 rows affected (0.00 sec)

root@sc 10:58  mysql>select * from student;
Empty set (0.00 sec)

5.Data recovery

Restoring full equipment:

[root@mysql backup]# mysql -uroot -p'Sanchuang123#' sc  < sc_student.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

It was found that there was only complete data and no modified data:

root@sc 10:58  mysql>select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | lisi     |
|  2 | wangwu   |
|  3 | zhangsan |
+----+----------+
3 rows in set (0.00 sec)

Binary log recovery:

Bin-log is an operation that records all mysql events. Bin-log can be used to perform complete recovery, point-in-time recovery, and location-based recovery.

View the binary log in use:

root@sc 11:17  mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     3812 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Find the time point when the data was modified:

[root@mysql mysql]# mysqlbinlog -vv mysql-bin.000001 

Insert image description here
Recover data:
Time-based recovery method:

[root@mysql mysql]# mysqlbinlog --start-datetime="2023-07-17 10:27:51" --stop-datetime="2023-07-17 10:51:10" mysql-bin.000001 | mysql -u root -p'123456'
mysql: [Warning] Using a password on the command line interface can be insecure.

Based on location:

[root@mysql mysql]# mysqlbinlog  --start-position=1985 --stop-position=2669  mysql-bin.000001 |mysql -uroot -p'123456'

Full recovery is not necessary and all binary logs can be used if the amount of data is not particularly large.

Reference link: Summary of mysql/mariadb knowledge points (25): Binary log

Guess you like

Origin blog.csdn.net/zheng_long_/article/details/131760356