mysqlbinlog恢复delete的数据

实验目的

delete数据后,用mysqlbinlog进行数据恢复

实验过程

原表

mysql> select * from mytest;
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | xw01 |    90 |
|  2 | xw02 |    92 |
|  3 | xw03 |    93 |
|  4 | xw04 |    94 |
|  5 | xw05 |    93 |
|  6 | xw06 |    96 |
|  7 | xw7  |    97 |
+----+------+-------+
7 rows in set (0.00 sec)

mysql>

# 查看binlog日志文件
root@dg02-xianwetitest-dy03:/var/log/mysql# ls
error.log  mysql-bin.000001  mysql-bin.000002  mysql-bin.index
root@dg02-xianwetitest-dy03:/var/log/mysql#



mysql> show binlog events in 'mysql-bin.000002' limit 10;
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                                   |
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------+
| mysql-bin.000002 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.33-0ubuntu0.16.04.1-log, Binlog ver: 4 |
| mysql-bin.000002 | 123 | Previous_gtids |         1 |         154 |                                                        |
| mysql-bin.000002 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                   |
| mysql-bin.000002 | 219 | Query          |         1 |         291 | BEGIN                                                  |
| mysql-bin.000002 | 291 | Table_map      |         1 |         344 | table_id: 108 (test.mytest)                            |
| mysql-bin.000002 | 344 | Update_rows    |         1 |         408 | table_id: 108 flags: STMT_END_F                        |
| mysql-bin.000002 | 408 | Xid            |         1 |         439 | COMMIT /* xid=91 */                                    |
| mysql-bin.000002 | 439 | Anonymous_Gtid |         1 |         504 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                   |
| mysql-bin.000002 | 504 | Query          |         1 |         576 | BEGIN                                                  |
| mysql-bin.000002 | 576 | Table_map      |         1 |         629 | table_id: 108 (test.mytest)                            |
+------------------+-----+----------------+-----------+-------------+--------------------------------------------------------+
10 rows in set (0.00 sec)





# 测试删除3行数据
mysql> delete from mytest where id=1 or id=2 or id=3;
Query OK, 3 rows affected (0.00 sec)

mysql> select * from mytest;
+----+------+-------+
| id | name | score |
+----+------+-------+
|  4 | xw04 |    94 |
|  5 | xw05 |    93 |
|  6 | xw06 |    96 |
|  7 | xw7  |    97 |
+----+------+-------+
4 rows in set (0.00 sec)

实验mysqlbin找到删除的命令

mysql> show binlog events in 'mysql-bin.000002' ;
.... 此次省略部分输出
	| mysql-bin.000002 | 2811 | Query          |         1 |        2883 | BEGIN                                                                                                                                                                                                                     |
| mysql-bin.000002 | 2883 | Table_map      |         1 |        2936 | table_id: 115 (test.mytest)                                                                                                                                                                                               |
| mysql-bin.000002 | 2936 | Write_rows     |         1 |        2984 | table_id: 115 flags: STMT_END_F                                                                                                                                                                                           |
| mysql-bin.000002 | 2984 | Xid            |         1 |        3015 | COMMIT /* xid=331 */                                                                                                                                                                                                      |
| mysql-bin.000002 | 3015 | Anonymous_Gtid |         1 |        3080 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                      |
| mysql-bin.000002 | 3080 | Query          |         1 |        3152 | BEGIN                                                                                                                                                                                                                     |
| mysql-bin.000002 | 3152 | Table_map      |         1 |        3205 | table_id: 115 (test.mytest)                                                                                                                                                                                               |
| mysql-bin.000002 | 3205 | Delete_rows    |         1 |        3282 | table_id: 115 flags: STMT_END_F                                                                                                                                                                                           |
| mysql-bin.000002 | 3282 | Xid            |         1 |        3313 | COMMIT /* xid=338 */                                                                                                                                                                                                      |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
47 rows in set (0.00 sec)


#找到"Delete_rows"对应所在的pos,是3080到3282
# 实验mysqlbin从binlog文件查找到删除对应的语句
root@dg02-xianwetitest-dy03:/var/log/mysql# mysqlbinlog  --start-position=3080  --stop-position=3282 --database=test mysql-bin.000002 -vv |grep ^"###"
### DELETE FROM `test`.`mytest`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xw01' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3=90 /* INT meta=0 nullable=1 is_null=0 */
### DELETE FROM `test`.`mytest`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xw02' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3=92 /* INT meta=0 nullable=1 is_null=0 */
### DELETE FROM `test`.`mytest`
### WHERE
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xw03' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3=93 /* INT meta=0 nullable=1 is_null=0 */



mysqlbinlog --start-position=3080 --stop-position=3282 --database=test mysql-bin.000002 -vv  | sed -n '/###/p' | sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;' |sed -r 's/(@6.*),/\1;/g' | sed 's/@[1-9]=//g' | sed 's/@[1-9][0-9]=//g'
INSERT INTO `test`.`mytest`
SELECT
  1 ,
  'xw01' ,
  90 ,
INSERT INTO `test`.`mytest`
SELECT
  2 ,
  'xw02' ,
  92 ,
INSERT INTO `test`.`mytest`
SELECT
  3 ,
  'xw03' ,
  93 ,
  • 再用文本工具编译为恢复的sql语句,如果太多可以用bin2sql工具处理

    INSERT INTO test.mytest
    SELECT
    1 ,
    'xw01' ,
    90 ;
    INSERT INTO test.mytest
    SELECT
    2 ,
    'xw02' ,
    92 ;
    INSERT INTO test.mytest
    SELECT
    3 ,
    'xw03' ,
    93

  • 恢复数据

    mysql> INSERT INTO test.mytest
    -> SELECT
    -> 1 ,
    -> 'xw01' ,
    -> 90 ;
    Query OK, 1 row affected (0.00 sec)
    Records: 1 Duplicates: 0 Warnings: 0

    mysql> INSERT INTO test.mytest
    -> SELECT
    -> 2 ,
    -> 'xw02' ,
    -> 92 ;
    Query OK, 1 row affected (0.00 sec)
    Records: 1 Duplicates: 0 Warnings: 0

    mysql> INSERT INTO test.mytest
    -> SELECT
    -> 3 ,
    -> 'xw03' ,
    -> 93 ;
    Query OK, 1 row affected (0.00 sec)
    Records: 1 Duplicates: 0 Warnings: 0

  • 恢复后检查

    mysql> select * from mytest;
    +----+------+-------+
    | id | name | score |
    +----+------+-------+
    | 1 | xw01 | 90 |
    | 2 | xw02 | 92 |
    | 3 | xw03 | 93 |
    | 4 | xw04 | 94 |
    | 5 | xw05 | 93 |
    | 6 | xw06 | 96 |
    | 7 | xw7 | 97 |
    +----+------+-------+
    7 rows in set (0.00 sec)

另外如何用mysqlbinlog语言转换太麻烦可以用binlog2sql工具,使用方法如下

参考文档:https://juejin.cn/post/7150801079196712967

root@dg02-xianwetitest-dy03:~/bin2sql/bin2sql/binlog2sql# /usr/bin/python2.7 binlog2sql/binlog2sql.py --start-position=3080 --stop-position=3282  --start-file='mysql-bin.000002' -h127.0.0.1  -uroot -proot@123 -dtest -tmytest
DELETE FROM `test`.`mytest` WHERE `score`=90 AND `id`=1 AND `name`='xw01' LIMIT 1; #start 3080 end 3282 time 2024-04-29 18:01:43
DELETE FROM `test`.`mytest` WHERE `score`=92 AND `id`=2 AND `name`='xw02' LIMIT 1; #start 3080 end 3282 time 2024-04-29 18:01:43
DELETE FROM `test`.`mytest` WHERE `score`=93 AND `id`=3 AND `name`='xw03' LIMIT 1; #start 3080 end 3282 time 2024-04-29 18:01:43
root@dg02-xianwetitest-dy03:~/bin2sql/bin2sql/binlog2sql#
root@dg02-xianwetitest-dy03:~/bin2sql/bin2sql/binlog2sql#
root@dg02-xianwetitest-dy03:~/bin2sql/bin2sql/binlog2sql#
root@dg02-xianwetitest-dy03:~/bin2sql/bin2sql/binlog2sql# /usr/bin/python2.7 binlog2sql/binlog2sql.py --start-position=3080 --stop-position=3282  --start-file='mysql-bin.000002' -h127.0.0.1  -uroot -proot@123 -dtest -tmytest -B
INSERT INTO `test`.`mytest`(`score`, `id`, `name`) VALUES (93, 3, 'xw03'); #start 3080 end 3282 time 2024-04-29 18:01:43
INSERT INTO `test`.`mytest`(`score`, `id`, `name`) VALUES (92, 2, 'xw02'); #start 3080 end 3282 time 2024-04-29 18:01:43
INSERT INTO `test`.`mytest`(`score`, `id`, `name`) VALUES (90, 1, 'xw01'); #start 3080 end 3282 time 2024-04-29 18:01:43


上面就是需要恢复数据的sql语句

实验总结

达到实验目的

相关推荐
Yz987622 分钟前
hive的存储格式
大数据·数据库·数据仓库·hive·hadoop·数据库开发
武子康27 分钟前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql
黑色叉腰丶大魔王32 分钟前
《MySQL 数据库备份与恢复》
mysql
苏-言34 分钟前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
Ljw...40 分钟前
索引(MySQL)
数据库·mysql·索引
菠萝咕噜肉i1 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
长风清留扬1 小时前
一篇文章了解何为 “大数据治理“ 理论与实践
大数据·数据库·面试·数据治理
OpsEye1 小时前
MySQL 8.0.40版本自动升级异常的预警提示
数据库·mysql·数据库升级
Ljw...1 小时前
表的增删改查(MySQL)
数据库·后端·mysql·表的增删查改
远歌已逝4 小时前
维护在线重做日志(二)
数据库·oracle