Mysql使用binlog恢复数据解决误操作问题的两种方法

我怕爱的太早我们不能终老 提交于 2019-11-27 03:48:35

>新搭建的个人博客,欢迎光临<

为保证没有其他参数配置影响,重新安装配置了一台最小化安装的CentOS7虚拟机

1. 基础知识

安装mysql5.6数据库 https://my.oschina.net/sgmder/blog/1631045

Mysql binlog初步理解 https://my.oschina.net/sgmder/blog/1631432

 

2. 配置mysql,开启binlog、修改binlog模式为Row Level模式

vi /etc/my.cnf 

修改mysql配置文件,在[mysqld]下增加以下内容

# 注释: 开启binlog 文件名以mysql-bin开头
log-bin = mysql-bin
# 注释: 备份恢复模式不需要开启Row模式 闪回需要开启Row模式
binlog_format="ROW"

重启mysql数据库 binlog开启 生成文件/var/lib/mysql/mysql-bin.000001

service mysqld restart

登录数据库 然后创建测试数据库demo和测试表user

mysql> create database demo;
Query OK, 1 row affected (0.00 sec)
mysql> use demo;
Database changed
mysql> CREATE TABLE `user` ( `id` int(8) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `type` int(8) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)

准备完毕,刷新掉binlog文件,此时binlog文件mysql-bin.000001结束,进入mysql-bin.000002记录

mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)

备份数据库

[root@localhost ~]# mysqldump -u root -p --databases demo > db_demo_bak.sql

3. 使用备份+binlog记录方式回滚数据库

进入数据库 先插入2条测试数据

mysql> insert into user (id,name,type) value (10001,'A','1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user (id,name,type) value (10002,'B','1');
Query OK, 1 row affected (0.00 sec)

查看数据

mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | A    |    1 |
| 10002 | B    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)

模拟误操作 update修改数据库数据

mysql> update user set name = 'C';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | C    |    1 |
| 10002 | C    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)

user表的name字段被误操作修改,抓紧刷新掉binlog文件,mysql-bin.000002结束,后续操作进入mysql-bin.000003

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

此时,查看一下mysql的binlog文件

[root@localhost ~]# ls /var/lib/mysql
auto.cnf  ibdata1      ib_logfile1  mysql-bin.000001  mysql-bin.000003  mysql.sock
demo      ib_logfile0  mysql        mysql-bin.000002  mysql-bin.index   performance_schema

那么,mysql-bin.000002文件记录了从备份到误操作的所有数据库执行,现在我们需要恢复掉被update误操作掉的数据

先使用备份恢复数据库 此时表user没有数据

mysql> user demo;
mysql> drop table user;
mysql> source /root/db_demo_bak.sql
mysql> select * from user;
Empty set (0.00 sec)

查看mysql-bin.000002记录的操作,查看各操作的Pos和End_log_pos

mysql> show binlog events in 'mysql-bin.000002';
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000002 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.39-log, Binlog ver: 4 |
| mysql-bin.000002 | 120 | Query       |         1 |         192 | BEGIN                                 |
| mysql-bin.000002 | 192 | Table_map   |         1 |         243 | table_id: 72 (demo.user)              |
| mysql-bin.000002 | 243 | Write_rows  |         1 |         289 | table_id: 72 flags: STMT_END_F        |
| mysql-bin.000002 | 289 | Xid         |         1 |         320 | COMMIT /* xid=147 */                  |
| mysql-bin.000002 | 320 | Query       |         1 |         392 | BEGIN                                 |
| mysql-bin.000002 | 392 | Table_map   |         1 |         443 | table_id: 72 (demo.user)              |
| mysql-bin.000002 | 443 | Write_rows  |         1 |         489 | table_id: 72 flags: STMT_END_F        |
| mysql-bin.000002 | 489 | Xid         |         1 |         520 | COMMIT /* xid=148 */                  |
| mysql-bin.000002 | 520 | Query       |         1 |         592 | BEGIN                                 |
| mysql-bin.000002 | 592 | Table_map   |         1 |         643 | table_id: 72 (demo.user)              |
| mysql-bin.000002 | 643 | Update_rows |         1 |         723 | table_id: 72 flags: STMT_END_F        |
| mysql-bin.000002 | 723 | Xid         |         1 |         754 | COMMIT /* xid=149 */                  |
| mysql-bin.000002 | 754 | Rotate      |         1 |         801 | mysql-bin.000003;pos=4                |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
14 rows in set (0.00 sec)

120 - 320 第一次insert

320 - 520 第二次insert

520 - 754 误操作update

使用mysqlbinlog工具恢复insert操作 120 - 520 

[root@localhost ~]# mysqlbinlog --start-position=120 --stop-position=520 --database=demo /var/lib/mysql/mysql-bin.000003 | /usr/bin/mysql -u root -p mima666 -v demo

mysqlbinlog的详细使用方法参看官方文档 https://dev.mysql.com/doc/refman/5.6/en/mysqlbinlog.html

查看下user表数据

mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | A    |    1 |
| 10002 | B    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)

Ok,数据恢复成功

4. 使用binlog+MyFlash闪回方式回滚数据库

参考美团的开源项目MyFlash

https://gitee.com/mirrors/MyFlash

https://github.com/Meituan-Dianping/MyFlash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!