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

SQL Server Comment Shortcut Key Operation

Batch comments in SQL Server Batch Annotation Ctr...

MySQL paging analysis principle and efficiency improvement

MySQL paging analysis principle and efficiency im...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Pure CSS and Flutter realize breathing light effect respectively (example code)

Last time, a very studious fan asked if it was po...

How to use Docker container to access host network

Recently, a system was deployed, using nginx as a...

Detailed explanation of the use of Vue mixin

Table of contents Use of Vue mixin Data access in...

Some questions about hyperlinks

<br />I am very happy to participate in this...

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Ubuntu installs multiple versions of CUDA and switches at any time

I will not introduce what CUDA is, but will direc...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

Best Practices for Implementing Simple Jira Projects with React+TS

A set of projects for training react+ts Although ...

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux yum install nginx-module-g...

MySQL 8.0.22 installation and configuration method graphic tutorial

This article records the installation and configu...

Vue uses the method in the reference library with source code

The official source code of monaco-editor-vue is ...