背景:

对于MySQL的binlog的查看都是用其自带的工具mysqlbinlog进行操作的,其实还有另一个方法来操作binlog,就是Replication中的SQL线程去操作binlog,其实binlog和relaylog的格式是一样的。下面开始介绍如何用该方法进行对binlog的操作。

测试1:

---初始化:
root@192.168.200.25 : aaa 03:51:38>reset master;
Query OK, 0 rows affected (0.12 sec) root@192.168.200.25 : aaa 03:51:55>reset slave;
Query OK, 0 rows affected (0.00 sec) root@192.168.200.25 : aaa 03:51:59>create table test_binlog(id int not null auto_increment,name varchar(10),primary key(id))
engine =innodb default charset utf8;
Query OK, 0 rows affected (0.17 sec) root@192.168.200.25 : aaa 03:52:11>insert into test_binlog(name) values('a'),('b'),('c'),('d'),('e');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 03:52:20>insert into test_binlog(name) values('aa'),('bb'),('cc'),('dd'),('ee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 03:52:24>insert into test_binlog(name) values('aaa'),('bbb'),('ccc'),('ddd'),('eee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 ---删除数据,为了恢复:
root@192.168.200.25 : aaa 03:52:28>flush logs;
Query OK, 0 rows affected (0.13 sec) root@192.168.200.25 : aaa 03:53:21>drop table test_binlog;
Query OK, 0 rows affected (0.08 sec)

把Binlog复制到Relaylog 目录:

root@zhoujy:/var/lib/mysql# cp /var/log/mysql/mysql-bin.000001 /var/lib/mysql/relay-bin.000001
root@zhoujy:/var/lib/mysql# chown -R mysql:mysql relay-bin.000001

修改my.cnf文件:

relay_log  = slave_relay
skip-slave-start
server-id = 2 #测试例子是自己的binlog对自己恢复,根据Replication原理,需要修改其Server_id SQL线程才能进行操作

需要生成master.info,relay-log.info,slave_relay.index等Replication需要的文件。

root@192.168.200.25 : aaa 04:00:29>change master to master_host='192.168.220.25',master_user='rep',master_password='rep',
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120,master_port=3306; #随便change 到一个地址,目的是为了生成SQL线程

关闭MySQL并修改Relaylog相关的文件:

vi relay-log.info
---
./slave_relay.000001
4
mysql-bin.000001
120
---
修改成:
---
./relay-bin.000001 #复制过来的binlog文件
4 #开始复制的postition
mysql-bin.000001
120
---
vi slave_relay.index
---
./slave_relay.000001
---
修改成:
---
./relay-bin.000001 #复制过来的binlog文件
---

开启MySQL并查看复制的状态:

root@192.168.200.25 : aaa 04:08:04>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.220.25
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: relay-bin.000001 #Relaylog 已经被替换
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB: aaa
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 1203
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:

最后启动SQL线程:

root@192.168.200.25 : aaa 04:14:58>select * from test_binlog;
ERROR 1146 (42S02): Table 'aaa.test_binlog' doesn't exist
root@192.168.200.25 : aaa 04:15:00>start slave sql_thread;
Query OK, 0 rows affected (0.00 sec) #已经恢复: root@192.168.200.25 : aaa 04:15:11>select * from test_binlog;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | aa |
| 7 | bb |
| 8 | cc |
| 9 | dd |
| 10 | ee |
| 11 | aaa |
| 12 | bbb |
| 13 | ccc |
| 14 | ddd |
| 15 | eee |
+----+------+
15 rows in set (0.00 sec)

例1结束,上面测试说明通过binlog,把其当成relaylog进行处理,成功的用SQL线程进行恢复。

测试2:

上面是对一个binlog进行恢复的,那如何对多个binlog进行恢复呢?马上来测试下:

---log1中
root@192.168.200.25 : aaa 04:57:39>create table test_binlog(id int not null auto_increment,name varchar(10),primary key(id))
engine =innodb default charset utf8;
Query OK, 0 rows affected (0.17 sec) root@192.168.200.25 : aaa 04:57:47>insert into test_binlog(name) values('a'),('b'),('c'),('d'),('e');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 04:57:52>insert into test_binlog(name) values('aa'),('bb'),('cc'),('dd'),('ee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 04:57:57>flush logs;
Query OK, 0 rows affected (0.13 sec) ---log2中
root@192.168.200.25 : aaa 04:58:04>insert into test_binlog(name) values('aaaa'),('bbbb'),('cccc'),('dddd'),('eeee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 04:58:10>insert into test_binlog(name) values('aaaaa'),('bbbbb'),('ccccc'),('ddddd'),('eeeee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 04:58:15>flush logs;
Query OK, 0 rows affected (0.12 sec) ---log3中
root@192.168.200.25 : aaa 04:58:19>insert into test_binlog(name) values('Aaaaaa'),('Bbbbbb'),('Cccccc'),('Dddddd'),('Eeeeee');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 04:58:23>use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A ---切换当前数据库,test下面插入10行
Database changed
root@192.168.200.25 : test 04:58:29>insert into aaa.test_binlog(name) values('A'),('B'),('C'),('D'),('E');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : test 04:58:33>insert into aaa.test_binlog(name) values('AA'),('BB'),('CC'),('DD'),('EE');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 ---切换当前数据库
root@192.168.200.25 : test 05:34:09>use aaa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
root@192.168.200.25 : aaa 05:34:19>insert into aaa.test_binlog(name) values('AAAAAA'),('BBBBBBB'),('CCCCCCC'),('DDDDDDD'),('EEEEEEE');
Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0 root@192.168.200.25 : aaa 05:34:40>select * from test_binlog;
+----+---------+
| id | name |
+----+---------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | aa |
| 7 | bb |
| 8 | cc |
| 9 | dd |
| 10 | ee |
| 11 | aaaa |
| 12 | bbbb |
| 13 | cccc |
| 14 | dddd |
| 15 | eeee |
| 16 | aaaaa |
| 17 | bbbbb |
| 18 | ccccc |
| 19 | ddddd |
| 20 | eeeee |
| 21 | Aaaaaa |
| 22 | Bbbbbb |
| 23 | Cccccc |
| 24 | Dddddd |
| 25 | Eeeeee |
| 26 | A |
| 27 | B |
| 28 | C |
| 29 | D |
| 30 | E |
| 31 | AA |
| 32 | BB |
| 33 | CC |
| 34 | DD |
| 35 | EE |
| 36 | AAAAAA |
| 37 | BBBBBBB |
| 38 | CCCCCCC |
| 39 | DDDDDDD |
| 40 | EEEEEEE |
+----+---------+
40 rows in set (0.00 sec)

按照例1中的步骤操作,复制Binlog:

cp mysql-bin.000001 /var/lib/mysql/relay-bin.000001
cp mysql-bin.000002 /var/lib/mysql/relay-bin.000002
cp mysql-bin.000003 /var/lib/mysql/relay-bin.000003 chown -R mysql:mysql relay-bin.00000*

修改my.cnf文件:

server-id               = 12
relay_log = slave_relay
skip-slave-start
replicate_do_db = aaa

需要生成master.info,relay-log.info,slave_relay.index等Replication需要的文件。

change master to master_host='192.168.220.25',master_user='rep',master_password='rep',MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120,master_port=3306;

关闭MySQL并修改Relaylog相关的文件:

vi relay-log.info 修改成:
---
./relay-bin.000001
4
mysql-bin.000001
120
--- vi slave_relay.index 修改成,这里和例1不同
---
./relay-bin.000001
./relay-bin.000002
./relay-bin.000003
---

开启MySQL并查看复制的状态:

root@192.168.200.25 : aaa 05:38:04>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.220.25
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: relay-bin.000001 #Relaylog 已经被替换
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB: aaa
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 2889
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.06 sec)

最后启动SQL线程:

root@192.168.200.25 : aaa 05:44:11>select * from test_binlog;
ERROR 1146 (42S02): Table 'aaa.test_binlog' doesn't exist root@192.168.200.25 : aaa 05:44:18>start slave sql_thread;
Query OK, 0 rows affected (0.00 sec) root@192.168.200.25 : aaa 05:44:29>select * from test_binlog;
+----+---------+
| id | name |
+----+---------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | aa |
| 7 | bb |
| 8 | cc |
| 9 | dd |
| 10 | ee |
| 11 | aaaa |
| 12 | bbbb |
| 13 | cccc |
| 14 | dddd |
| 15 | eeee |
| 16 | aaaaa |
| 17 | bbbbb |
| 18 | ccccc |
| 19 | ddddd |
| 20 | eeeee |
| 21 | Aaaaaa |
| 22 | Bbbbbb |
| 23 | Cccccc |
| 24 | Dddddd |
| 25 | Eeeeee |
| 36 | AAAAAA |
| 37 | BBBBBBB |
| 38 | CCCCCCC |
| 39 | DDDDDDD |
| 40 | EEEEEEE |
+----+---------+
30 rows in set (0.01 sec)

这里看到可以正常的恢复了,但是数据不对,少了10行(26~35行)。为什么少了?这里大家应该都很清楚了,因为是在初始化的时候有10行数据是在test数据库下面插入的,而在配置文件中只复制aaa数据库下的操作记录(replicate_do_db = aaa)。这也是通过Relaylog恢复的一个优点。

总结:

上面的测试在5.1到5.15.1到5.5的操作都没有问题,所以通过本文介绍的方法去操作Binlog,更有灵活性,可以随时进行stop、start slave操作;通过测试了解到,在用binlog进行增量备份的场景下,使用该方法更有效直接的进行恢复,比本身的mysqlbinlog 更有效;也可以解决mysqlbinlog的一些问题:
(*) Max_allowed_packet问题

(*) 恼人的Blob/Binary/text字段问题
(*) 特殊字符的转义问题

(*) 没有"断点恢复":执行出错后,没有足够的报错,也很难从失败的地方继续恢复

MySQL 利用SQL线程对Binlog操作(转)的更多相关文章

  1. MySQL 利用SQL线程对Binlog操作

    背景: 对于MySQL的binlog的查看都是用其自带的工具mysqlbinlog进行操作的,其实还有另一个方法来操作binlog,就是Replication中的SQL线程去操作binlog,其实bi ...

  2. (2.7)Mysql之SQL基础——表的操作与查看

    (2.7)Mysql之SQL基础——表的操作与查看 搜索关键字:mysql表操作,comment注释操作,mysql临时表 0.临时表 create temporary table 1.创建表(在in ...

  3. 利用SQL Profiler 追踪数据库操作

    SQL Server 事件探查器 是一个界面,用于创建和管理跟踪并分析和重播跟踪结果. 这些事件保存在一个跟踪文件中,稍后试图诊断问题时,可以对该文件进行分析或用它来重播一系列特定的步骤. SQL S ...

  4. 第10讲:利用SQL语言实现关系代数操作

    一.实现并.交.差运算 1. 基本语法形式:子查询 [union [all] | intersect [all] | except [all] 子查询] ①意义:将关系代数中的∪.∩.- 分别用uni ...

  5. mysql利用sql脚本插入数据中文乱码

    将其中的 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SE ...

  6. mysql利用binlog进行数据恢复

    目录 mysql利用binlog进行数据恢复 binlog基本配置和格式 binlog基本配置 查看binlog状态 binlog的三种格式 转换成sql mysql自带的mysqlbinlog 利用 ...

  7. mysql 利用binlog增量备份,还原实例

    mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...

  8. 在MySQL中使用init-connect与binlog来实现用户操作追踪记录

    在MySQL中使用init-connect与binlog来实现用户操作追踪记录 分类: MySQL 前言: 测试环境莫名其妙有几条重要数据被删除了,由于在binlog里面只看到是公用账号删除的,无法查 ...

  9. mysql利用binlog恢复数据详细例子

    模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...

随机推荐

  1. WPF Step By Step 控件介绍

    WPF Step By Step 控件介绍 回顾 上一篇,我们主要讨论了WPF的几个重点的基本知识的介绍,本篇,我们将会简单的介绍几个基本控件的简单用法,本文会举几个项目中的具体的例子,结合这些 例子 ...

  2. Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式

    1 重构Point2类 1.1 问题 本案例使用初始化方法重构Point2类,类中有横坐标x.纵坐标y两个属性,并且有一个能显示位置show方法.在主程序中创建两个Point2类的对象,设置其横纵坐标 ...

  3. 【avalon源码】scpCompile

    function noop() {} function scpCompile(array) { return Function.apply(noop, array) } // var fn = new ...

  4. C# 入门篇之listview用法

    在用listview,经常对各种属性都不了解,用完了,下次又忘:写个随笔记记. 看到别人已经有总结很详细的,就直接转过来了,可能中间会有一些自己加的. ------------------------ ...

  5. 310. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  6. poj 3159 dijkstra 最短路

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  7. java List 排序 Collections.sort() 对 List 排序

    class User { String name; String age;  public User(String name,String age){  this.name=name;  this.a ...

  8. hive 常见面试题

    (笔者自己做记录) 1.Hive内外部表的区别删除表是否影响外部数据2.Hive如何做到权限管理hive下可以修改配置后创建用户管理,但是仅仅是为了防止误操而已,如果要真的为了安全操作建议使用 Ker ...

  9. POJ-2481 Cows (线段树单点更新)

    题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...

  10. Python实现__metaclass__实现方法运行时间统计

    几天前写的,参考了园友的一篇文章,链接找不到了.先感谢,找到了链接再补上.