How to recover accidentally deleted data records (binlog) in MySQL

Preface

The subject of the question deleted the record by mistake in the online environment today (November 27, 2022), and there was no backup data. The cause of the accident was investigated all night, and finally no production accident occurred. I would like to record this in this article.

References

https://blog.csdn.net/qq_23543983/article/details/127298578

This article is a practical supplementary explanation of the above operations.

1 Check the binlog log

First make sure your binlog is turned on. Generally, the online environment will be opened. The command is as follows:

show VARIABLES like '%log_bin%';

Then log in to the server where you store MySQL. Find the folder where binlog logs are stored. Generally, the project team operation and maintenance will know the location of this file. After you find it, you will find a lot of log files, as shown below:

Insert image description here
Note that the last log file is updated incrementally in real time. The file will always be written. If you deleted it recently, just look for the file circled in the picture above. If it was deleted in other time periods, look for the file named by the date.

The earlier and faster the data recovery time is, the better. We only keep records on our server for 45 days!

2 Find the corresponding binlog file and execute the command directly

The following sql gives a time period. It is the approximate time period during which you captured the wrong operation.

 mysqlbinlog --no-defaults --start-datetime='2022-10-12 15:00:00' --stop-datetime='2022-10-12 17:00:00' /var/lib/mysql/mysql-bin.000031 >/var/lib/mysql/mysql_delete20221012.sql

After execution, a sql file will be generated. It contains all the data you misoperated. (The data is encrypted) There are a lot of records in it. We only need to find the starting index and the ending index.

It is recommended to use head -100; and tail -100 to view the starting and ending nodes. Find these two result values ​​and record them:

332334767
and
366250788

Then execute the following command to accurately export the misoperation SQL through the node:

mysqlbinlog --no-defaults -vv --start-position=332334767 --stop-position=366250788 /var/lib/mysql/mysql-bin.000031 >/var/lib/mysql/bin_data.sql

In fact, it’s just about splicing your time periods together. All INSERT and UPDATE statements will be captured (as shown in the legend below):

Insert image description here

3 Find the data you accidentally deleted

Generally speaking, the data you accidentally deleted will be roughly marked with an identifier. Here is the document number. I vaguely remember what the document encoding of the deleted data is, then we can fuzzy search for this condition:

less bin_data.sql | grep -C 100 "单据编号xxxxx"

In this way, you can get the corresponding reverse INSERT statement, as shown below:

Insert image description here

4 Convert delete statement into insert statement

You can search on Baidu yourself, or you can refer to the following command:

cat /var/lib/mysql/bin_data.sql | sed -n '/###/p' | sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;' |sed -r 's/(@17.*),/\1;/g' | sed 's/@[1-9]=//g' | sed 's/@[1-9][0-9]=//g' >  /var/lib/mysql/delete2insert.sql

Pay attention to comply with the grammatical format. You'll have to modify it manually, but it's pretty much the same. You need to manually change some symbols such as; in the file.

You can also refer to the following: https://www.lmlphp.com/user/62049/article/item/2326681/

Finish

learn a lesson!

Guess you like

Origin blog.csdn.net/weixin_44757863/article/details/128071839