当前位置:网站首页>MySql data recovery method personal summary
MySql data recovery method personal summary
2022-08-04 05:31:00 【little things】
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 from 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, for example:
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. Addition, deletion and modification operations will be recorded in binlog, and slow query sql can also be recorded in slow query log, but you also need to modify the configuration file yourself to enable 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
边栏推荐
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
- C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
- 4.3 基于注解的声明式事务和基于XML的声明式事务
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
- What is the salary of a software testing student?
- Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
- The symbol table
- 少年成就黑客,需要这些技能
- 3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
- 8、自定义映射resultMap
猜你喜欢
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
一个对象引用的思考
px、em、rem的区别
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
Cannot read properties of null (reading ‘insertBefore‘)
符号表
About yolo7 and gpu
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
Resolved error: npm WARN config global `--global`, `--local` are deprecated
随机推荐
处理List<Map<String, String>>类型
7. Execution of special SQL
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
文献管理工具 | Zotero
FPGA学习笔记——知识点总结
px、em、rem的区别
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
8.03 Day34---BaseMapper查询语句用法
TSF微服务治理实战系列(一)——治理蓝图
Cannot read properties of null (reading ‘insertBefore‘)
The difference between px, em, and rem
去重的几种方式
day13--postman接口测试
sql server如何得到本条记录与上一条记录的差异,即变动值
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
部署LVS-DR群集【实验】
canal实现mysql数据同步