How to restore data using binlog in mysql5.7

How to restore data using binlog in mysql5.7

Step 1: Ensure that MySQL has binlog enabled

show variables like '%log_bin%';
log_bin is enabled when it is on.

insert image description here

Step 2: Enter the binlog file directory and find the binary log file

insert image description here

mysql> show binary logs; #Get the binlog file listmysql> show master status; #View the binlog file currently being writtenmysql> reset master; Reset binlog 

insert image description here

Step 3: Use the mysqlbinlog tool command to view the database addition, deletion, modification and query records (you must switch to the mysqlbinlog directory to be effective) or directly specify binlog

Example 1 : Query the operation log from 2021-3-12 14:00:00 to 2021-3-12 14:03:00. The database is g_xinxiangshop. Enter the following command to write the data into a spare txt file.

/usr/local/mysql/bin/mysqlbinlog --no-defaults --database=g_xinxiangshop --start-datetime="2021-3-12 14:00:00" --stop-datetime="2021-3-12 14:03:00" /usr/local/mysql/data/mysql-bin.000001 > /tmp/binlog.txt

Example 2 : Query the operation log of the database g_xinxiangshop from 2021-3-12 14:00:00 to 2021-3-12 14:03:00, and filter out the operation records that only include the data in the g_user table. Enter the following command to write the data to a spare txt file

/usr/local/mysql/bin/mysqlbinlog --no-defaults --database=g_xinxiangshop --start-datetime="2021-3-12 14:00:00" --stop-datetime="2021-3-12 14:03:00" /usr/local/mysql/data/mysql-bin.000001 | grep g_user > /tmp/binlog.txt

Example 3 : Query the operation log of the database g_shoptest from 2021-3-15 15:25:00 to 2021-3-15 15:35:00 and output it to the screen
/usr/local/mysql/bin/mysqlbinlog --no-defaults --database=g_shoptest --start-datetime="2021-3-15 15:25:00" --stop-datetime="2021-3-15 15:35:00" /data/mysql/mysql-bin.000001 |more

insert image description here

The picture is the same as Example 1 and Example 2. You can perform the recovery operation after seeing the truncate operation and the record point!

Step 4: Test using bin_log to recover data

Login to mysql
test :

1. reset master; reset binlog and regenerate logs

insert image description here

2. Test a table by inserting a piece of data and then accidentally deleting it.

insert image description here

insert image description here

3. View bin-log command: show binlog events in 'mysql-bin.000001';

insert image description here

As shown in the figure above, we can see that the deletion points are between 928 and 1294; the previously added data are between 154 and 520

insert image description here

/usr/local/mysql/bin/mysqlbinlog /usr/local/mysql/data/mysql-bin.000001 --start-position 154 --stop-position 520 | mysql -uroot -p g_shoptest

After the execution is complete, go and see if you deleted the data and it is back.

insert image description here

binlog command

1. The most commonly used method is to restore the data at the specified data end, which can be directly restored to the database:

mysqlbinlog --start-date="2021-3-12 14:00:00" --stop-date="2021-3-12 14:03:00" mysql_bin.000001 |mysql -uroot -p123456

2. Specify the start and end positions. From the binary log generated above, we can know the start and end positions of a log. We can specify the log from position A to position B during the recovery process. The following two parameters are required to specify:

  • --start-positon="50" //Specify starting from position 50
  • --stop-position="100"//Specify to end at position 100

/usr/local/mysql/bin/mysqlbinlog /usr/local/mysql/data/mysql-bin.000001 --start-position 7903538 --stop-position 7904498|mysql -uroot -p123456

**Common ErrorERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 31, event_type: 35

*The problem is caused by the mysqlbinlog version

View the mysqlbinlog used by the current os

shell> which mysqlbinlog
/usr/bin/mysqlbinlog

View the mysqlbinlog currently used by mysql

±--------------±------------------+
| Variable_name | Value |
±--------------±------------------+
| basedir | /usr/local/mysql/ |
±--------------±------------------+

mysql> show variables like 'basedir';

Comparing the two versions

shell> /usr/bin/mysqlbinlog --version
shell> /usr/local/mysql/bin/mysqlbinlog --version

To solve this problem, just specify the mysqlbinlog path

/usr/local/mysql/bin/mysqlbinlog --no-defaults --database=g_xinxiangshop --start-datetime="2021-3-12 14:00:00" --stop-datetime="2021-3-12 14:03:00" /usr/local/mysql/data/mysql-bin.000001 > /tmp/binlog.txt

This is the end of this article about how to use binlog to recover data in MySQL 5.7. For more information about MySQL binlog recovery, 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:
  • MySQL database recovery (using mysqlbinlog command)
  • Binlog related commands and recovery techniques in MySQL
  • MySQL Binlog Data Recovery: Detailed Explanation of Accidentally Deleting a Database
  • How to use binlog for data recovery in MySQL
  • Teach you to automatically restore the log file (binlog) of the MySQL database
  • Detailed steps to restore MySQL database through binlog files on Linux
  • Explain MySQL's binlog log and how to use binlog log to recover data
  • MySQL uses mysqldump+binlog to completely restore the deleted database principle analysis
  • MySQL uses binlog logs to implement data recovery
  • How to use MySQL binlog to restore accidentally deleted databases

<<:  Facebook's nearly perfect redesign of all Internet services

>>:  Detailed explanation of VUE's data proxy and events

Recommend

Detailed explanation of real-time backup knowledge points of MySQL database

Preface The need for real-time database backup is...

How to view the storage location of MySQL data files

We may have a question: After we install MySQL lo...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

Mini Program implements custom multi-level single-select and multiple-select

This article shares the specific code for impleme...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

Vue3+el-table realizes row and column conversion

Table of contents Row-Column Conversion Analyze t...

Summary of 11 amazing JavaScript code refactoring best practices

Table of contents 1. Extracting functions 2. Merg...

Detailed steps for Spring Boot packaging and uploading to Docker repository

Important note: Before studying this article, you...

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...

Reasons and solutions for MySQL failing to create foreign keys

When associating two tables, a foreign key could ...

The difference between MySQL database stored procedures and transactions

Transactions ensure the atomicity of multiple SQL...

A brief discussion on logic extraction and field display of Vue3 in projects

Table of contents Logical Layering Separate busin...