Mysql uses binglog to restore data steps

Project scenario:

Recently, a project encountered a scenario where binlog was used to restore data, and a test was done at home using a windows environment. For the record, it is actually similar to the linux environment, but the instructions are different. You can convert it yourself for linux.


recovery steps

1. Check whether the database has binlog enabled

Execute the following command:

show variables like '%log_bin%'

insert image description here

It can be seen that 1 on means that it has been turned on, and version 8.0 is turned on by default. 2. basename is the storage location of binglog 3. index is the index of binlog storage. After opening, it is shown in the figure below.
insert image description here
Records are the names of all binlogs.
If it is not opened, just restart sql after configuring it yourself, and it will not be expanded here.

2. Execute the delete statement

delete from a where a.id =1; 

3. View binlog

2.1 Generally, the data we want to restore is basically deleted by mistake on the same day. We directly convert the binglog file of the day into a readable txt file. Looking at the binlog.index above, we can know that the last file is the binlog file of the day. The default size of this file is It is 1g, if the amount of data is not very large, it can be converted directly.

D:\javaTools\mysql-8.0.12-winx64\bin\mysqlbinlog --no-defaults -vv --base64-output=decode-rows binlog.000764>binlog1.txt

2.2 If you are not sure which binlog it is, you can also convert it by time

D:\javaTools\mysql-8.0.12-winx64\bin\mysqlbinlog --no-defaults  --base64-output=decode-rows -v --database=test1  --start-datetime="2022-11-13 00:00:00" --stop-datetime="2022-11-14 00:00:00" binlog.000764 > binlog2.txt

2.3 After opening the file, as shown in the figure below,
insert image description here
you can see the execution of the delete statement in the binlog.

4. Data recovery

4.1 Restore the entire binlog file data

mysqlbinlog --no-defaults binlog.000001|mysql -u root -p test

The error is as follows:

'mysql' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

You need to add the mysql bin to the system path.
4.2 Restore according to the number of starting and ending lines
insert image description here

After converting the file through the steps of 2.1, you need to get the number of lines before begin, you can see that the number of start and end lines of the delete statement is 230-438,
then the restored statement is;

D:\javaTools\mysql-8.0.12-winx64\bin\mysqlbinlog --no-defaults --start-position=230 --stop-position=438 binlog.000002|mysql -u root -p

insert image description here

The error is reported as follows. The delete statement cannot be restored by using these two methods. I have tried several methods but still can’t solve it. If you have a solution, you can leave a message to let me know.
4.3 Since the above method does not work, just convert the delete statement into an insert statement to restore it, and use another method to restore the data.
In the Linux environment, use the txt file just now to execute the following instructions.

cat binlog1.txt | 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=//g'| sed 's/@[1-9]=/,/g' | sed 's/@[1-9][0-9]=/,/g' > test.sql

The content of the converted file is as follows, you can see that it is not a normal sql command, you can convert it yourself.

;INSERT INTO `test1`.`a`
SELECT
  6 ,
  ,'小红1667188306698' ,
  ,1 ,
  ,12 ,
  ,'2022-10-31 11:51:47' , 

converted sql

  insert
	into
	`test1`.`a`
select
	6,
	'小红1667188306698' ,
	
	1 ,
	
	12 ,
	
	'2022-10-31 11:51:47'

4.4 execute sql
insert image description here

Summarize:

The data can be restored through the binlog. After testing and viewing the documents, if it is a drop table, the data should be restored through the method 4.2, but the deleted row cannot be restored. It is necessary to convert the binlog log into an insert statement to restore the data. If there is
a small partner who can directly use the mysqlbinlog method to restore the data, please let me know and make progress together.

Guess you like

Origin blog.csdn.net/qq_34526237/article/details/127838165