连续3篇讲解binlog。恢复是binlog的两大主要作用之一,接下来通过实例演示如何利用binlog恢复数据:
首先,看下当前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 | +----+----------+---------+-----+--------+
删除一条数据。
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语句逐个重新执行一次而已。
: » 详解使用MySQL的binlog(二进制日志)恢复数据的教程
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/252381.html