通过mysqlbinlog –skip-gtids恢复后再备份可能造成的坑
版本1
2
3
4
5
6
7
8[root@uz22199 backup]# innobackupex --version
innobackupex version 2.4.8 Linux (x86_64) (revision id: 97330f7)
[root@uz22199 backup]# mysql -e"select @@version"
+------------+
| @@version |
+------------+
| 5.7.18-log |
+------------+
源库1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33表结构与数据
root@mysqldb 21:51: [fandb]> show create table users\G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`email` varchar(10) DEFAULT NULL,
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
root@mysqldb 18:43: [fandb]> select* from users;
+-------+
| email |
+-------+
| 1 |
| 10 |
| 20 |
| 30 |
| 5 |
+-------+
插入一条数据
insert into users values(50); --GTID=1297
再删掉
delete from users where email=50; ----GTID=1298
当前Executed_Gtid_Set
root@mysqldb 18:35: [fandb]> show master status;
+------------------+----------+--------------+------------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------------+
| mysql-bin.000005 | 495 | | | 5c351518-78ec-11e7-8e7a-005056a610c3:1-1298 |
+------------------+----------+--------------+------------------+---------------------------------------------+
1 row in set (0.00 sec)
源库再次应用一下已经执行过得binlog, 再次应用insert into users values(50); 这一条
这里先不考虑有没有可能这样子去恢复数据,只做实验
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 [root@test43100 backup]# mysqlbinlog --skip-gtids --include-gtids='5c351518-78ec-11e7-8e7a-005056a610c3:1297' mysql-bin.000005 |mysql
root@mysqldb 18:43: [fandb]> select* from users;
+-------+
| email |
+-------+
| 1 |
| 10 |
| 20 |
| 30 |
| 5 |
| 50 |
+-------+
root@mysqldb 18:43: [fandb]> show master status;
+------------------+----------+--------------+------------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------------+
| mysql-bin.000005 | 617 | | | 5c351518-78ec-11e7-8e7a-005056a610c3:1-1299 |
+------------------+----------+--------------+------------------+---------------------------------------------+
源库Executed_Gtid_Set 已经到1299了
备份1
2
3
4
5
6
7
8
9
10
11innobackupex --user=backup --password='backup' --stream=tar /tmp | gzip -> full.tar.gz
170907 18:45:15 Backup created in directory '/tmp/'
MySQL binlog position: filename 'mysql-bin.000005', position '617', GTID of the last change '5c351518-78ec-11e7-8e7a-005056a610c3:1-1299'
170907 18:45:15 [00] Streaming <STDOUT>
170907 18:45:15 [00] ...done
170907 18:45:15 [00] Streaming <STDOUT>
170907 18:45:15 [00] ...done
xtrabackup: Transaction log of lsn (3112759) to (3112768) was copied.
170907 18:45:16 completed OK!
从备份输出信息和xtrabackup_binlog_info都可以看到,这个全备备份了1-12991
2[root@uz22199 full2]# more xtrabackup_binlog_info
mysql-bin.000005 617 5c351518-78ec-11e7-8e7a-005056a610c3:1-1299
把备份随便搞到一个地方恢复出来
(恢复过程省略)
查看恢复出来的库的Executed_Gtid_Set1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21root@mysqldb 18:48: [(none)]> show master status;
+------------------+----------+--------------+------------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------------+
| mysql-bin.000001 | 154 | | | 5c351518-78ec-11e7-8e7a-005056a610c3:1-1298 |
+------------------+----------+--------------+------------------+---------------------------------------------+
1 row in set (0.00 sec)
虽然看起来只执行到1298,但是50这条数据却有了
root@mysqldb 18:43: [fandb]> select* from users;
+-------+
| email |
+-------+
| 1 |
| 10 |
| 20 |
| 30 |
| 5 |
| 50 |
+-------+
如果此时我们直接将该库作为从库,change master到源库,那么start slave会报错,1299会再执行一边insert 50,会报1062错误.
而如果我们flush binary logs一次,再做全备
1 | root@mysqldb 21:51: [fandb]> flush binary logs; |
Executed_Gtid_Set依旧是1-1299
再次将备份恢复出来,查看新恢复出来的库1
2
3
4
5
6root@mysqldb 22:02: [(none)]> show master status;
+------------------+----------+--------------+------------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------------+
| mysql-bin.000001 | 154 | | | 5c351518-78ec-11e7-8e7a-005056a610c3:1-1299 |
+------------------+----------+--------------+------------------+---------------------------------------------+
此时恢复出来的库Executed_Gtid_Set为1-1299了
总结
那么要么以后通过mysqlbinlog –skip-gtids 恢复数据之后flush 一下binary logs;
要么恢复出来的库都手动根据xtrabackup_binlog_info去set global gtid_purged