详解讲解如何使用MySQL的binlog 恢复数据

连续3篇讲解binlog了,之前围绕使用binlog 主从复制教程MySQL之binlog(二进制日志)解析教程,相信大家已经有了一定mysql基础。

恢复是binlog的两大主要作用之一,接下来通过实例演示如何利用binlog恢复数据:

更多精彩内容请看 web前端中文站
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语句逐个重新执行一次而已。

【注:本文源自网络文章资源,由站长整理发布】

0
如无特殊说明,文章均为原作者原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到