Mysql database data recovery library has been deleted. How to restore the database?

Preface

This solution is aimed at recovering data in the Linux environment. It is not clear whether it can be used in Windows. You can refer to it.

The recovery of the mysql database is to recover lost or deleted data through the binlog log of MySQL itself. The binlog log file records all operations in the database, and CRUD/Rollback operations will be recorded in the log.

This method is to recover through the binlog log. You need to ensure that the binlog log is enabled in the database.

1. Conditions that need to be met to recover data

SQL script to query whether binlog is enabled:
If it is not enabled, this solution cannot be restored.

 show variables like '%log_bin%';

Insert image description here
View the directory where binlog stores log files

  show variables like '%datadir%';

Restrictions on the use of the my2sql plug-in:

  • When using the rollback/flashback function, the binlog format must be row, and binlog_row_image=full. DML statistics and large transaction analysis will not be affected. Only DML can be rolled back, but DDL cannot be rolled back.
  • Supports specifying -tl time zone to interpret the contents of the time/datetime field in binlog. The start time-start-datetime and end time-stop-datetime will also use this specified time zone, but note that the start and end times are for the unix timestamp saved in the binlog event header. The additional datetime information in the results is the unix timestamp in the binlog event header.
  • This tool is disguised as pulling binlog from the library. The user who needs to connect to the database has SELECT, REPLICATION SLAVE, REPLICATION CLIENT permissions.

Plug-in download address, free download: my2sql download address is free
Insert image description here

2. Start recovering data

1. The first step

Place the plug-in my2sql in the bin directory of the mysql installation directory, and then grant permissions to my2sql.

  chmod 777 my2sql

2. The second step

Create an empty folder "tmpdir" and give it 777 permissions.

 mkdir tmpdir

3. The third step

Download the binlong file to the local , then convert it into sql format , find the corresponding node (pos point) , the node name (pos point) is a purely digital timestamp, and the data of the specified time period can be restored according to the node.
To convert the binlog file format, first ensure that the versions of the local database and the production environment database are consistent , and then enter the following command (without newline symbols):

mysqlbinlog --no-defaults --base64-output=decode-rows -v 
--start-datetime="2022-08-09 08:00:00" --stop-datetime="2022-08-17 10:35:00" 
C:\MySQL\MySQL8.0\Desktop\mysql-bin.000039 -r ./output1.sql

illustrate:

mysqlbinlog --no-defaults --base64-output=decode-rows -v 
--start-datetime="开始时间" --stop-datetime="结束时间"  
'binlog文件的绝对路径' -r ./'转换后的文件名称'

Nodes (POS points) in the converted SQL file: The
corresponding nodes can be found according to the time in the binlog file. With the start node and end node, the data of the specified time period can be restored.
Insert image description here

4. The fourth step

In the bin directory, enter the following command (without line breaks):

./my2sql -user root -password 123456 -host 127.0.0.1 -port 3308  
-mode file -local-binlog-file ./mysql-bin.000038  -work-type 2sql  
-start-file mysql-bin.000038  -start-pos 606463342  -
stop-file mysql-bin.000038 -stop-pos 651675557  -output-dir ./tmpdir

illustrate:

./my2sql -user '账号' -password '密码' -host 'ip (bin 目录就是127)' -port '端口'  -mode file -local-binlog-file ./'binlog 文件名'  -work-type 2sql  -start-file 'binlog 文件名'  -start-pos '开始节点(节点数字在binlog日志中有)'  -stop-file 'binlog 文件名' -stop-pos '结束节点(节点数字在binlog日志中有)'  -output-dir ./tmpdir

After successful execution, the SQL statement of the corresponding node in the binlog file will be extracted into the file. I generated a total of three files this time, namely biglong_trx.txt, binlog_status.txt, and forward.38.sql .

File description:
biglong_trx.txt:
This file has little content, I haven’t understood it in depth, it is of no use, so I won’t pay attention to it for now.
binlog_status.txt:
This file describes the DML statistical information of the binlog file, such as which SQL statements were executed from the start node to the end node, when they were executed, what operations were executed, how many rows of data were affected, and what execution ended.
forward.38.sql:
This file stores the sql statement extracted from the binlog. This statement is a sql file that can be executed in the database .

5. Step 5

Open the extracted file in a text editor and check whether the corresponding SQL is correct and complete. Pay attention to the delete and rollback statements .

6. Step Six

If there is no problem with the sql review, then just execute the sql file in the library. It is recommended to use the navicat 15 tool and choose to run the sql file to execute the sql. At this point, the data recovery is complete.

Conclusion:

You must be cautious when it comes to formal data. Please do not execute any SQL scripts that modify the production environment in the production environment. Make regular backups or use a master-slave replication mode. The host only opens connections to applications. Others that require a database will connect to the slave database.
Attached is a blog address with a detailed explanation of the my2sql tool. Those who are interested can learn about the my2sql tool.
Address: https://blog.csdn.net/liuhanran/article/details/107425016


Guess you like

Origin blog.csdn.net/uziuzi669/article/details/126408915