当前位置:网站首页>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.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 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_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, 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
边栏推荐
- 力扣:343. 整数拆分
- 有趣的 Kotlin 0x0E:DeepRecursiveFunction
- 数的划分之动态规划
- 触觉智能分享-SSD20X实现升级显示进度条
- 2022年PMP考试延迟了,该喜该忧?
- 去重的几种方式
- 企业需要知道的5个 IAM 最佳实践
- 应届生软件测试薪资大概多少?
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
猜你喜欢
![[C language advanced] program environment and preprocessing](/img/ac/a13dd2cc47136d4938b6fc7fad660c.png)
[C language advanced] program environment and preprocessing

如何将 DevSecOps 引入企业?

使用Loadrunner进行性能测试

8.03 Day34---BaseMapper查询语句用法

Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法

day13--postman interface test

What is the salary of a software testing student?

npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa

解决错误:npm WARN config global `--global`, `--local` are deprecated

The symbol table
随机推荐
7.15 Day21---MySQL----索引
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
7.16 Day22---MYSQL(Dao模式封装JDBC)
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
Dynamic programming of the division of numbers
sql server如何得到本条记录与上一条记录的差异,即变动值
el-Select 选择器 底部固定
CentOS7 —— yum安装mysql
心余力绌:企业面临的软件供应链安全困境
8、自定义映射resultMap
如何低成本修bug?测试左移给你答案
7. Execution of special SQL
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
MySQL数据库面试题总结(2022最新版)
12、分页插件
深度学习21天——准备(环境配置)
What is the salary of a software testing student?
el-Select selector bottom fixed
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析