连续 3 篇讲解binlog了,之前围绕使用 binlog 主从复制教程和MySQL 之 binlog(二进制日志)解析教程,相信大家已经有了一定 mysql 基础。
恢复是binlog的两大主要作用之一,接下来通过实例演示如何利用binlog恢复数据:
更多精彩内容请看 web 前端中文站
http://www.lisa33xiaoq.net 可按 Ctrl + D 进行收藏
首先,看下当前binlog的位置:
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000008 | 1847 | | | | +------------------+----------+--------------+------------------+-------------------+
接着想测试表 tb_person 中插入两条记录:
insert into tb_person set name="person_1", address="beijing", sex="man", other="test-1"; insert into tb_person set name="person_2", address="beijing", sex="man", other="test-2";
记录当前binlog位置:
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000008 | 2585 | | | | +------------------+----------+--------------+------------------+-------------------+
查询数据:
mysql> select * from tb_person where name ="person_2" or name="person_1"; +----+----------+---------+-----+--------+ | id | name | address | sex | other | +----+----------+---------+-----+--------+ | 6 | person_1 | beijing | man | test-1 | | 7 | person_2 | beijing | man | test-2 | +----+----------+---------+-----+--------+//www.lisa33xiaoq.net
删除一条数据。
mysql> select * from tb_person where name ="person_2" or name="person_1"; +----+----------+---------+-----+--------+ | id | name | address | sex | other | +----+----------+---------+-----+--------+ | 6 | person_1 | beijing | man | test-1 | +----+----------+---------+-----+--------+
binlog恢复(指定 pos 点恢复/部分恢复)
mysqlbinlog --start-position=1847 --stop-position=2585 mysql-bin.000008 > test.sql mysql> source /var/lib/mysql/3306/test.sql
数据恢复完成后查询验证一下。
mysql> select * from tb_person where name ="person_2" or name="person_1"; +----+----------+---------+-----+--------+ | id | name | address | sex | other | +----+----------+---------+-----+--------+ | 6 | person_1 | beijing | man | test-1 | | 7 | person_2 | beijing | man | test-2 | +----+----------+---------+-----+--------+
binlog 恢复数据,就是让 mysql 将保存在 binlog 日志中指定段落区间的 sql 语句逐个重新执行一次而已。
【注:本文源自网络文章资源,由站长整理发布】