Centos7 Sample code for implementing MySQL log based data restoration

  • 2021-08-12 04:18:44
  • OfStack

Brief introduction

Binlog log, namely binary log file, is used to record the SQL statement information of the user's operation to the database. When the data is deleted by mistake, we can restore the deleted data through binlog log. The methods of restoring data are divided into traditional binary file restoration data and binary file restoration data based on GTID

Pre-preparation

Prepare an Centos7 virtual machine, close the firewall and selinux, configure the IP address, synchronize the system time, and install the MySQL database

Traditional binary log restores data

Modify the configuration file


[root@localhost ~]# vi /etc/my.cnf
server-id=1
log-bin=binlog

# Restart the database service 
[root@localhost ~]# systemctl restart mysqld

Operational database


mysql> create database mydb charset utf8mb4;
mysql> use mydb;
mysql> create table test(id int)engine=innodb charset=utf8mb4;
mysql> insert into test values(1);
mysql> insert into test values(2);
mysql> insert into test values(3);
mysql> insert into test values(4);
mysql> commit;
mysql> update test set id=10 where id=4;
mysql> commit;
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec)
mysql> drop database mydb;

View binary log information


mysql> show master status\G;
*************************** 1. row ***************************
       File: binlog.000001
     Position: 1960
   Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

 
# Find the points of creating and deleting libraries , For 219 And 1868
mysql> show binlog events in 'binlog.000001';
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name   | Pos | Event_type   | Server_id | End_log_pos | Info                                |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000001 | 219 | Query     |     1 |     329 | create database mydb charset utf8mb4                |
| binlog.000001 | 1868 | Query     |     1 |    1960 | drop database mydb                         |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+

Save as binary log information


[root@localhost ~]# mysqlbinlog --start-position=219 --stop-position=1868 /var/lib/mysql/binlog.000001 > /tmp/binlog.sql

Recovery of data


# Temporary closure 2 Binary log records to avoid duplicate records 
mysql> set sql_log_bin=0;
# Recovery of data 
mysql> source /tmp/binlog.sql
# Restart 2 Binary log record 
mysql> set sql_log_bin=1;

View data recovery


mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mydb        |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mydb;
Database changed
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec) , 

Restore data based on GTID2 binary log

Modify the configuration file


[root@localhost ~]# vi /etc/my.cnf
server-id=1
log-bin=binlog
gtid_mode=ON
enforce_gtid_consistency=true
log_slave_updates=1

# Restart the database service 
[root@localhost ~]# systemctl restart mysqld

Operational database


mysql> create database mydb1;
mysql> use mydb1;
Database changed
mysql> create table t1(id int)engine=innodb charset=utf8mb4;
mysql> insert into t1 values(1);
mysql> insert into t1 values(2);
mysql> insert into t1 values(3);
mysql> insert into t1 values(11);
mysql> insert into t1 values(12);
mysql> commit;
mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  11 |
|  12 |
+------+
5 rows in set (0.00 sec)
mysql> drop database mydb1;

View binary log information


mysql> show master status\G;
*************************** 1. row ***************************
       File: binlog.000003
     Position: 1944
   Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 51d3db57-bf69-11ea-976c-000c2911a022:1-8
1 row in set (0.00 sec)

mysql> show binlog events in 'binlog.000003';
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name   | Pos | Event_type   | Server_id | End_log_pos | Info                               |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| binlog.000003 | 154 | Gtid      |     1 |     219 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:1' |
| binlog.000003 | 219 | Query     |     1 |     316 | create database mydb1                       |
| binlog.000003 | 1784 | Gtid      |     1 |    1849 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:8' |
| binlog.000003 | 1849 | Query     |     1 |    1944 | drop database mydb1                        |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+

Save as binary log information


#8 The transaction record is to delete the database, so only the recovery is needed 1-7 Number transaction record can be used 
[root@localhost ~]# mysqlbinlog --skip-gtids --include-gtids='51d3db57-bf69-11ea-976c-000c2911a022:1-7' /var/lib/mysql/binlog.000003 > /tmp/gtid.sql

Parameter description:
--include-gtids: Include transactions
--exclude-gtids: Exclude transactions
--skip-gtids: Skipping Transactions

Recovery of data


mysql> create database mydb charset utf8mb4;
mysql> use mydb;
mysql> create table test(id int)engine=innodb charset=utf8mb4;
mysql> insert into test values(1);
mysql> insert into test values(2);
mysql> insert into test values(3);
mysql> insert into test values(4);
mysql> commit;
mysql> update test set id=10 where id=4;
mysql> commit;
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec)
mysql> drop database mydb;
0

View data recovery


mysql> create database mydb charset utf8mb4;
mysql> use mydb;
mysql> create table test(id int)engine=innodb charset=utf8mb4;
mysql> insert into test values(1);
mysql> insert into test values(2);
mysql> insert into test values(3);
mysql> insert into test values(4);
mysql> commit;
mysql> update test set id=10 where id=4;
mysql> commit;
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec)
mysql> drop database mydb;
1

Related articles: