当前位置:网站首页>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
边栏推荐
- Dynamic programming of the division of numbers
- 离线采集怎么看sql执行计划
- word 公式编辑器 键入技巧 | 写数学作业必备速查表
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
- 代码重构:面向单元测试
- 12、分页插件
- 4.3 基于注解的声明式事务和基于XML的声明式事务
- 心余力绌:企业面临的软件供应链安全困境
- JS basics - forced type conversion (error-prone, self-use)
- Grain Mall - Basics (Project Introduction & Project Construction)
猜你喜欢
震惊,99.9% 的同学没有真正理解字符串的不可变性
心余力绌:企业面临的软件供应链安全困境
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
[Evaluation model] Topsis method (pros and cons distance method)
Delphi-C端有趣的菜单操作界面设计
The idea setting recognizes the .sql file type and other file types
TSF微服务治理实战系列(一)——治理蓝图
[C language advanced] program environment and preprocessing
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
leetcode 12. Integer to Roman numeral
随机推荐
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
The symbol table
8. Custom mapping resultMap
7.16 Day22---MYSQL(Dao模式封装JDBC)
使用Loadrunner进行性能测试
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
redis中常见的面试题
Interesting Kotlin 0x0E: DeepRecursiveFunction
代码重构:面向单元测试
【一步到位】Jenkins的安装、部署、启动(完整教程)
8大软件供应链攻击事件概述
基于gRPC编写golang简单C2远控
MySQL日期函数
渗透测试(PenTest)基础指南
Bolb analysis of image processing (1)
Cannot read properties of null (reading ‘insertBefore‘)
SLSA 框架与软件供应链安全防护
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading