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

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

Computed properties and listeners details

Table of contents 1. Calculated properties 1.1 Ba...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

B2C website user experience detail design reference

Recently, when using Apple.com/Ebay.com/Amazon.co...

MYSQL custom function to determine whether it is a positive integer example code

You can write a function: Mainly use regular expr...

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

MySQL Basics Quick Start Knowledge Summary (with Mind Map)

Table of contents Preface 1. Basic knowledge of d...

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...

How to configure ssh/sftp and set permissions under Linux operating system

Compared with FTP, SSH-based sftp service has bet...