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

Detailed explanation of Javascript event capture and bubbling methods

Table of contents 1. Event Processing Model 1. Ev...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

Detailed explanation of MySQL startup options and system variables examples

Table of contents Boot Options Command Line Long ...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

Solution to MySQL Installer is running in Community mode

Today I found this prompt when I was running and ...

How to use Xtrabackup to back up and restore MySQL

Table of contents 1. Backup 1.1 Fully prepared 1....

Some indicators of excellent web front-end design

The accessibility of web pages seems to be somethi...

Zabbix monitoring solution - the latest official version 4.4 [recommended]

Zabbix 2019/10/12 Chenxin refer to https://www.za...

Detailed explanation of the difference between tags and elements in HTML

I believe that many friends who are new to web pag...

Example analysis of mysql variable usage [system variables, user variables]

This article uses examples to illustrate the usag...

How to control the startup order of docker compose services

summary Docker-compose can easily combine multipl...

How to build a React project with Vite

Table of contents Preface Create a Vite project R...

SQL implementation of LeetCode (177. Nth highest salary)

[LeetCode] 177.Nth Highest Salary Write a SQL que...

Docker online and offline installation and common command operations

1. Test environment name Version centos 7.6 docke...