Centos7 implements sample code for restoring data based on MySQL logs

Centos7 implements sample code for restoring data based on MySQL logs

Introduction

Binlog logs, that is, binary log files, are used to record SQL statement information of user operations on the database. When data is accidentally deleted, we can restore the deleted data through binlog logs. The methods of restoring data are divided into traditional binary file restoration and GTID-based binary file restoration.

Preliminary preparation

Prepare a Centos7 virtual machine, turn off the firewall and selinux, configure the IP address, synchronize the system time, and install the MySQL database

Traditional binary log restore 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 for creating and deleting databases, which are 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

Recover Data

#Temporarily turn off binary logging to avoid duplicate logging mysql> set sql_log_bin=0;
#Restore datamysql> source /tmp/binlog.sql
#Restart binary logging mysql> set sql_log_bin=1;

Check data recovery status

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),

Restoring data based on GTID 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

#Transaction record 8 is to delete the database, so only transaction records 1-7 need to be restored [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: Skip transactions

Recover Data

mysql> set sql_log_bin=0;
mysql> source /tmp/gtid.sql
mysql> set sql_log_bin=1;

Check data recovery status

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

mysql> use mydb1;
Database changed
mysql> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
| 3 |
| 11 |
| 12 |
+------+
5 rows in set (0.00 sec)

This is the end of this article about the sample code for implementing MySQL log-based data restoration on Centos7. For more information about Centos7 MySQL log restoration data, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Explanation of mysql error exceptions in linux background log (recommended)
  • How to enable log management function after installing MySQL on Windows and Linux

<<:  Detailed explanation of the basic usage of MySQL triggers [create, view, delete, etc.]

>>:  JavaScript Snake Implementation Code

Recommend

Solve the problem of docker images disappearing

1. Mirror images disappear in 50 and 93 [root@h50...

Use button trigger events to achieve background color flashing effect

To achieve the background color flashing effect, j...

Vue+node realizes audio recording and playback function

Result: The main part is to implement the code lo...

Windows cannot start MySQL service and reports error 1067 solution

Suddenly when I logged into MySQL, it said that a...

MySQL advanced learning index advantages and disadvantages and rules of use

1. Advantages and Disadvantages of Indexes Advant...

JavaScript design pattern chain of responsibility pattern

Table of contents Overview Code Implementation Pa...

MySql sharing of null function usage

Functions about null in MySql IFNULL ISNULL NULLI...

Implementing the preview function of multiple image uploads based on HTML

I recently wrote a script for uploading multiple ...

Implementation steps for enabling docker remote service link on cloud centos

Here we introduce the centos server with docker i...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

jQuery implements accordion effects

This article shares the specific code of jQuery t...