MySQL 8.0文档阅读:通过binlog恢复数据

文章目录

  • 前言
  • 演练

前言

比如手贱, 误删了数据. 或者代码BUG, 导致需要数据还原.
前提是已经开启了binlog, 并且有定期的全量备份

binlog格式
https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog-row-events.html

演练

https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog.html

准备一些数据, 然后执行一次delete操作

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      986 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

通过mysqlbinlog -v binlog.000001打印binlog信息, 找到这条delete操作

mysqlbinlog -v binlog.000001

指定位置范围
mysqlbinlog -v binlog.000001 --start-position=0 --start-position=986

指定时间范围
mysqlbinlog -v binlog.000001 --start-datetime="2005-12-25 11:25:56" 

找到那次的删除操作

# at 582
#190314 17:54:02 server id 1  end_log_pos 630 CRC32 0x63ee4f3e  Table_map: `test`.`t1` mapped to number 99
# at 630
#190314 17:54:02 server id 1  end_log_pos 670 CRC32 0x2aea3cce  Delete_rows: table id 99 flags: STMT_END_F

BINLOG '
uiSKXBMBAAAAMAAAAHYCAAAAAGMAAAAAAAEABHRlc3QAAnQxAAEDAAEBAQA+T+5j
uiSKXCABAAAAKAAAAJ4CAAAAAGMAAAAAAAEAAgAB/wABAAAAzjzqKg==
'/*!*/;
### DELETE FROM `test`.`t1`
### WHERE
###   @1=1
# at 670
#190314 17:54:02 server id 1  end_log_pos 701 CRC32 0xe15d817d  Xid = 554
COMMIT/*!*/;

执行恢复, 也就是将582位置之前的binlog再执行一遍

shell> mysqlbinlog binlog.000001 --stop-position=582 -v | mysql -uroot -p123456

你可能感兴趣的:(后端)