MySQL 利用SQL线程对Binlog操作
背景:
对于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.1 和 5.1到5.5的操作都没有问题,所以通过本文介绍的方法去操作Binlog,更有灵活性,可以随时进行stop、start slave操作;通过测试了解到,在用binlog进行增量备份的场景下,使用该方法更有效直接的进行恢复,比本身的mysqlbinlog 更有效;也可以解决mysqlbinlog的一些问题:
(*) Max_allowed_packet问题
(*) 恼人的Blob/Binary/text字段问题
(*) 特殊字符的转义问题
(*) 没有"断点恢复":执行出错后,没有足够的报错,也很难从失败的地方继续恢复
更多信息:
http://www.orczhou.com/index.php/2013/11/use-mysql-replication-to-recove-binlog/
http://blog.itpub.net/22664653/viewspace-775778/
MySQL 利用SQL线程对Binlog操作的更多相关文章
- MySQL 利用SQL线程对Binlog操作(转)
背景: 对于MySQL的binlog的查看都是用其自带的工具mysqlbinlog进行操作的,其实还有另一个方法来操作binlog,就是Replication中的SQL线程去操作binlog,其实bi ...
- (2.7)Mysql之SQL基础——表的操作与查看
(2.7)Mysql之SQL基础——表的操作与查看 搜索关键字:mysql表操作,comment注释操作,mysql临时表 0.临时表 create temporary table 1.创建表(在in ...
- 利用SQL Profiler 追踪数据库操作
SQL Server 事件探查器 是一个界面,用于创建和管理跟踪并分析和重播跟踪结果. 这些事件保存在一个跟踪文件中,稍后试图诊断问题时,可以对该文件进行分析或用它来重播一系列特定的步骤. SQL S ...
- 第10讲:利用SQL语言实现关系代数操作
一.实现并.交.差运算 1. 基本语法形式:子查询 [union [all] | intersect [all] | except [all] 子查询] ①意义:将关系代数中的∪.∩.- 分别用uni ...
- mysql利用sql脚本插入数据中文乱码
将其中的 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SE ...
- mysql利用binlog进行数据恢复
目录 mysql利用binlog进行数据恢复 binlog基本配置和格式 binlog基本配置 查看binlog状态 binlog的三种格式 转换成sql mysql自带的mysqlbinlog 利用 ...
- mysql 利用binlog增量备份,还原实例
mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...
- 在MySQL中使用init-connect与binlog来实现用户操作追踪记录
在MySQL中使用init-connect与binlog来实现用户操作追踪记录 分类: MySQL 前言: 测试环境莫名其妙有几条重要数据被删除了,由于在binlog里面只看到是公用账号删除的,无法查 ...
- mysql利用binlog恢复数据详细例子
模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...
随机推荐
- linux下如何安装charles
charles是很好的抓包工具. 如何安装呢? 可以去官网下载:http://www.charlesproxy.com/download/ 如果是Debian系列的,可以直接用apt-get 安装 h ...
- Java学习笔记(二)不定时更新
Java语言画图 package cn.witksy.dev; import javax.imageio.ImageIO; import java.awt.*; import java.awt.ima ...
- java.lang.Class
java.lang.Class 一.Class类是什么 Class是一个类,位于java.lang包下. 在Java中每个类都有一个相对应的Class类的对象,换句话说:Java程序在启动运行时 一个 ...
- InnerClass内部类
1,内部类概述 定义:把A类定义在B类内部,则A类是内部类.如下所示: class Outer1{外部类 String name1; public void show(){ System.out.pr ...
- UVA1376.Animal Run (最小割转为最短路 && dijkstra)
Animal Run Time Limit:6000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ1011
今天搞了一下传说中的经典搜索题——poj1011,果然里面充斥着各种巧妙的剪枝,做完之后回味一下还是感觉构思太巧妙,所以总结记录一下加深理解. 原题:http://poj.org/problem?id ...
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...
- MongoDB的基本使用(二)
上一个文档说明了如何搭建一个Windows端MongoDB服务器,下面将简单介绍MongoDB的基本操作命令. 1. show dbs : 显示所有数据库 2. use bochao : 使用boch ...
- Effective Java 读书笔记之十 序列化
一.谨慎地实现Serializable接口 1.一旦一个类被发布,就大大地降低了“改变这个类的实现”的灵活性. 2.仔细设计类的序列化形式而不是接受类的默认虚拟化形式. 3.反序列化机制是一个“隐藏的 ...