mysqlbinlog限制
该软件利用binlog中记录了操作前的数据镜像和操作后的数据镜像。有如下限制
1)binlog_format=row
2)必须打开binlog
3)只支持insert、update、delete
4)不支持drop 、truncate、alter等ddl语句
特别说明:自带的mysqlbinlog 命令如果没有 -B 参数,则不可用于下面范例。
预备知识:
egrep 参数:
-i :忽略大小写。
-C :后面的数字表示打印符合要求的行以及上下的行。
insert范例
创建测试数据
mysql> create database liangdb character set utf8;
Query OK, 1 row affected (0.01 sec)
mysql> use liangdb;
Database changed
创建测试表并插入数据
mysql> create table t_user_info(id int(20) auto_increment primary key ,name varchar(20));
mysql> insert into t_user_info values(1,'谢广坤'),(2,'赵四'),(3,'刘能'),(4,'王长贵');
mysql> insert into t_user_info values(11,'王大拿'),(12,'王木生'),(13,'谢永强'),(14,'王小蒙');
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
| 11 | 王大拿 |
| 12 | 王木生 |
| 13 | 谢永强 |
| 14 | 王小蒙 |
+----+-----------+
8 rows in set (0.00 sec)
mysql> show master status G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 4720
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
现在假设上面的id 11-14 误插入了,现在需要闪回,操作如下:
/opt/mysqlbinlog -vv mysql-bin.000004 |egrep -i -C 20 'insert'
BEGIN
/*!*/;
# at 4491
#170914 14:31:44 server id 3307105 end_log_pos 4551 CRC32 0x1099afaa Table_map: `liangdb`.`t_user_info` mapped to number 96
# at 4551
#170914 14:31:44 server id 3307105 end_log_pos 4689 CRC32 0x6078cb75 Write_rows: table id 96 flags: STMT_END_F
BINLOG '
UCK6WRNhdjIAPAAAAMcRAAAAAGAAAAAAAAEAB2xpYW5nZGIAC3RfdXNlcl9pbmZvAAIDDwI8AAKq
r5kQ
UCK6WR5hdjIAigAAAFESAAAAAGAAAAAAAAEAAgAC//wLAAAAFMOnxb3igLnDpcKkwqfDpuKAucK/
/AwAAAAUw6fFveKAucOmxZPCqMOn4oCdxbj8DQAAABLDqMKwwqLDpsKwwrjDpcK8wrr8DgAAABXD
p8W94oC5w6XCsMKPw6jigJnihKJ1y3hg
'/*!*/;
### INSERT INTO `liangdb`.`t_user_info`
### SET
### @1=11 /* INT meta=0 nullable=0 is_null=0 */
### @2='????¤§???' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### INSERT INTO `liangdb`.`t_user_info`
### SET
### @1=12 /* INT meta=0 nullable=0 is_null=0 */
### @2='?????¨?”?' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### INSERT INTO `liangdb`.`t_user_info`
### SET
### @1=13 /* INT meta=0 nullable=0 is_null=0 */
### @2='è°¢?°???o' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### INSERT INTO `liangdb`.`t_user_info`
### SET
### @1=14 /* INT meta=0 nullable=0 is_null=0 */
### @2='????°è’?' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 4689
#170914 14:31:44 server id 3307105 end_log_pos 4720 CRC32 0xad3890a3 Xid = 4313
COMMIT/*!*/;
在上面的记录中,找到对应insert语句end_log_pos ,找到关键字 BEGIN 往后看的第一个pos(BEGIN后面的 at 4491)和COMMIT往前看的最后一个end_log_pos(end_log_pos 4720)。
在上面中,分别找到开始和结束的end_log_pos 为:
--start-position=4491
--stop-position=4720
执行下述恢复命令:
/opt/mysqlbinlog -B -vv --start-position=4491 --stop-position=4720 mysql-bin.000004 | mysql -uXXXXXXXXX -pXXXXXXXXX -hXXXXXXXXX -PXXXXXXXXX liangdb
登录到数据库查看验证:
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
+----+-----------+
4 rows in set (0.00 sec)
至此数据现已恢复。
update范例
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
+----+-----------+
4 rows in set (0.00 sec)
mysql> update t_user_info set name='梁国军' where id >=3;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 梁国军 |
| 4 | 梁国军 |
+----+-----------+
4 rows in set (0.00 sec)
mysql> show master statusG
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 5324
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
查看binlog
/opt/mysqlbinlog -vv mysql-bin.000004 |egrep -i -C 20 'update'
BEGIN
/*!*/;
# at 5099
#170914 15:02:36 server id 3307105 end_log_pos 5159 CRC32 0x6ec97416 Table_map: `liangdb`.`t_user_info` mapped to number 96
# at 5159
#170914 15:02:36 server id 3307105 end_log_pos 5293 CRC32 0xf089ad91 Update_rows: table id 96 flags: STMT_END_F
BINLOG '
jCm6WRNhdjIAPAAAACcUAAAAAGAAAAAAAAEAB2xpYW5nZGIAC3RfdXNlcl9pbmZvAAIDDwI8AAIW
dMlu
jCm6WR9hdjIAhgAAAK0UAAAAAGAAAAAAAAEAAgAC///8AwAAAAzDpcuGy5zDqMaSwr38AwAAABXD
psKiwoHDpeKAusK9w6XigKDigLr8BAAAABTDp8W94oC5w6nigKLCv8OowrTCtfwEAAAAFcOmwqLC
gcOl4oC6wr3DpeKAoOKAupGtifA=
'/*!*/;
### UPDATE `liangdb`.`t_user_info`
### WHERE
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2='???è??' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### SET
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2='?¢??????' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### UPDATE `liangdb`.`t_user_info`
### WHERE
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='???é??è′μ' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### SET
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='?¢??????' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 5293
#170914 15:02:36 server id 3307105 end_log_pos 5324 CRC32 0x2f825c70 Xid = 4384
COMMIT/*!*/;
恢复
/opt/mysqlbinlog -B -vv --start-position=5099 --stop-position=5324 mysql-bin.000004 | mysql -uXXXXXXXXX -pXXXXXXXXX -hXXXXXXXXX -PXXXXXXXXX liangdb
验证。
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
+----+-----------+
4 rows in set (0.00 sec)
至此,已恢复。
delete范例
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
+----+-----------+
4 rows in set (0.00 sec)
mysql> delete from t_user_info where id >=3;
Query OK, 2 rows affected (0.01 sec)
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
+----+-----------+
2 rows in set (0.00 sec)
mysql> show master statusG
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 5869
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
查看binlog
/opt/mysqlbinlog -vv mysql-bin.000004 |egrep -i -C 20 'delete'
BEGIN
/*!*/;
# at 5699
#170914 15:08:31 server id 3307105 end_log_pos 5759 CRC32 0x81532552 Table_map: `liangdb`.`t_user_info` mapped to number 96
# at 5759
#170914 15:08:31 server id 3307105 end_log_pos 5838 CRC32 0x8eca6af1 Delete_rows: table id 96 flags: STMT_END_F
BINLOG '
7yq6WRNhdjIAPAAAAH8WAAAAAGAAAAAAAAEAB2xpYW5nZGIAC3RfdXNlcl9pbmZvAAIDDwI8AAJS
JVOB
7yq6WSBhdjIATwAAAM4WAAAAAGAAAAAAAAEAAgAC//wDAAAADMOly4bLnMOoxpLCvfwEAAAAFMOn
xb3igLnDqeKAosK/w6jCtMK18WrKjg==
'/*!*/;
### DELETE FROM `liangdb`.`t_user_info`
### WHERE
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2='???è??' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### DELETE FROM `liangdb`.`t_user_info`
### WHERE
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='???é??è′μ' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 5838
#170914 15:08:31 server id 3307105 end_log_pos 5869 CRC32 0x7ea27b47 Xid = 4402
COMMIT/*!*/;
恢复
/opt/mysqlbinlog -B -vv --start-position=5699 --stop-position=5869 mysql-bin.000004 | mysql -uXXXXXXXXX -pXXXXXXXXX -hXXXXXXXXX -PXXXXXXXXX liangdb
验证。
mysql> select * from t_user_info;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 谢广坤 |
| 2 | 赵四 |
| 3 | 刘能 |
| 4 | 王长贵 |
+----+-----------+
4 rows in set (0.00 sec)
至此,已恢复。