当前位置:网站首页>MySql data recovery method personal summary
MySql data recovery method personal summary
2022-07-31 00:47:00 【drhrht】
I. Introduction
mysql has the binlog function, which can record the operations performed on the table and the executed SQL statements;
The following mainly summarizes the opening method and usage method of binlog;
If you delete some data in the database table by mistake, or delete the entire table by mistake, modify the table structure by mistake, etc., you can use this method to restore.
Second, Steps
1. If you want to use the MySql data recovery function, you first need to modify the Mysql configuration file and enable the binlog function.(Some versions of binlog are not enabled by default, so you need to manually modify the configuration file to enable)
(1)windows environment
The name of the configuration file is my.ini, you can install the software everything to search for this file.A sample path is as follows:
C:ProgramDataMySQLMySQL Server 5.7my.ini(2) linux environment
The name of the configuration file is my.cnf, you can use the find command to search for the location of the file:
sudo find / -name my.cnfA sample location is as follows:
/etc/my.cnf2. After finding the configuration file (my.ini/my.cnf), add the following configuration (of course, first check whether the configuration file has already configured this line, do not repeat it):
log-bin=mysqlbinlogbinlog-format=ROWOr you can specify the location of binlog (the path under linux is below):
log-bin=/data/mysql/log/mysqlbinlogbinlog-format=ROWNote that when binlog is specified in a folder, you must create a folder yourself, otherwise restarting mysql will report an error.
3. After the configuration is complete, restart mysql.
4. Now, operations on MySQL databases, tables, data, etc., and execution of additions, deletions, and changes to sql will all be recorded in binlog.(Search for mysqlbinlog to find this log file)
5. To test, create a new table binlog_test with random columns, then write a few lines of data, and then clear the table data.(ready to use binlog to restore data)
6. The command to view binlog is as follows:
mysql> show binlog events; #Only view the contents of the first binlog filemysql> show binlog events in 'mysqlbinlog.000002';#View the contents of the specified binlog filemysql> show binary logs; #Get a list of binlog filesmysql> show master status; #View the binlog file currently being written7. First look at which binlog is currently used:
show master status;After I executed it, I found that the currently used binlog is: mysqlbinlog.000004
8. Then check the contents of this binlog:
show binlog events in 'mysqlbinlog.000004';After execution, you can find the sql of the binlog_test table you just created in the Info column of the query result (the corresponding Event_type is >Query);
You can see the record row that just emptied the table data, Event_type is Delete_rows;
In the above, you can see that after the table is created, the Event_type of the new data is Write_rows;
9. Analyze this binlog, our goal is to restore the data that was just deleted, so after finding the create table statement, the first Event_type is the line of Query,Its Pos is 460;
Then find Event_type which is the closest, above is the line of Delete_rowsInfoCOMMIT, its Event_type is Xid, End_log_pos is 1199.
(End_log_pos of each line corresponds to Pos of the next line)
10. Now I have found a range of execution logs from 460 to 1199, which is the log of adding data to the table binlog_test, so as long as the operations in between are repeated, the data can be restored.
11-1.windows, find the location of mysqlbinlog.exe;
under linux, find the location of mysqlbinlog (this is in the bin directory of mysqlan executable file);
Then execute the following command to restore the data:
mysqlbinlog --no-defaults --start-position=460 --stop-position=1199 "C:ProgramDataMySQLMySQL Sever 5.7Datamysqlbinlog.000004" -d test | mysql -uroot -proot testThe specified start position is 460 and the end position is 1199. After the command is executed, the command within the specified range in the log will be read and executed again.
The meanings of other commands are:
"-d test" means, specify the database as test (binlog_test table is in the test database)"|" is the pipe character"mysql -uroot -proot test" is the mysql account password, log in to the target database"-uroot": account is root"-proot": password is root"test": the database is test11-2. You can also export the logs within the specified range in the binlog log, and then execute the source command to restore the data, as follows:
mysqlbinlog "C:ProgramDataMySQLMySQL Sever 5.7Datamysqlbinlog.000004" -d test --skip-gtids --start-position=460 --stop-position=1199 > test.sqlAfter executing this command, the logs in the specified range are exported to test.sql;
Then log in to mysql and execute the command:
mysql> use test;mysql> source test.sql;This will restore the data.
III. Other Notes
1. If you use truncate to delete table data, a few lines of records will be added to binlog to record the truncate operation. Examples are as follows:
mysqlbinlog.000004 | 3122 | Query | 1 | 3211 | use 'test'; TRUNCATE 'binlog_test'If you want to restore the data, you need to find the Pos where the data was first inserted, to the End_log_pos before the truncate, and then re-execute the commands during this period.
2. The addition, deletion and modification operations will be recorded in the binlog, and the slow query sql can also be recorded in the slow query log, but you also need to modify the configuration file to open the slow query log.
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- Common network status codes
- Go study notes (84) - Go project directory structure
- registers (assembly language)
- 【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
- 【952. 按公因数计算最大组件大小】
- 【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- 什么是Promise?Promise的原理是什么?Promise怎么用?
- [In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
- 【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
猜你喜欢
随机推荐
Go 学习笔记(84)— Go 项目目录结构
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
MySQL数据库进阶篇
Strict Mode for Databases
MySQL数据库约束,表的设计
Go study notes (84) - Go project directory structure
Consistency and Consensus of Distributed Systems (1) - Overview
registers (assembly language)
论文理解:“Designing and training of a dual CNN for image denoising“
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
ShardingSphere之读写分离(八)
BOM系列之Navigator对象
正则表达式密码策略与正则回溯机制绕过
binglog log tracking: data backup and backup tracking
加密传输过程
xss绕过:prompt(1)
Kotlin协程:协程上下文与上下文元素









