当前位置:网站首页>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.cnf
A sample location is as follows:
/etc/my.cnf
2. 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=ROW
Or you can specify the location of binlog (the path under linux is below):
log-bin=/data/mysql/log/mysqlbinlogbinlog-format=ROW
Note 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 written
7. 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_rows
InfoCOMMIT
, 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 test
The 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 test
11-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.sql
After 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
边栏推荐
- Preparations for web vulnerabilities
- WMware Tools installation failed segmentation fault solution
- Common network status codes
- [Yugong Series] July 2022 Go Teaching Course 016-Logical Operators and Other Operators of Operators
- Gabor滤波器学习笔记
- 【Demo】ABAP Base64加解密测试
- Jmeter参数传递方式(token传递,接口关联等)
- 寄存器(汇编语言)
- binglog log tracking: data backup and backup tracking
- PHP图片添加文字水印
猜你喜欢
MySQL数据库进阶篇
Gabor滤波器学习笔记
MySQL数据库面试题总结(2022最新版)
What is Promise?What is the principle of Promise?How to use Promises?
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
DNS resolution process [visit website]
DNS解析过程【访问网站】
ShardingSphere之未分片表配置实战(六)
go mode tidy出现报错go warning “all“ matched no packages
Meeting OA project pending meeting, all meeting functions
随机推荐
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
ELK deployment script---pro test available
Shell programming of conditional statements
Detailed explanation of 9 common reasons for MySQL index failure
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
【愚公系列】2022年07月 Go教学课程 013-常量、指针
DATA AI Summit 2022提及到的对 aggregate 的优化
DNS resolution process [visit website]
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
SWM32 Series Tutorial 6 - Systick and PWM
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
typescript18-对象类型
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
h264和h265解码上的区别
go mode tidy出现报错go warning “all“ matched no packages
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
Meeting OA project pending meeting, all meeting functions
In-depth understanding of the auto-increment operator from two error-prone written test questions
Jetpack Compose learning (8) - State and remeber
【深入浅出玩转FPGA学习14----------测试用例设计2】