基于gtid的备份_恢复_注意事项_mysqldump_skip-gtids_include-gtids_exclude-gtids

相关参数

–skip-gtids

是否使用–skip-gtids=true 参数,要根据情况来定;

  • 第一种情况:
    如果我们是要恢复数据到源数据库或者和源数据库有相同 GTID 信息的实例,则使用mysqlbinlog进行导出时候,需要使用该参数。如果不带该参数的话,是无法恢复成功的。因为包含的 GTID 已经在源数据库执行过了,根据 GTID 特性,一个 GTID 信息在一个数据库只能执行一次,所以不会恢复成功。

  • 第二种情况:
    如果是恢复到其他实例的数据库并且不包含源实例的 GTID 信息,那么可以不使用该参数,使用或者不使用都可以恢复成功。

–include-gtids

 mysqlbinlog --include-gtids='cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1-3' /data/mysql/my3306/logs/mysql-bin.000001 > include_gtid_sql.sql 

表示只备份cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1-3的事务

–exclude-gtids

mysqlbinlog --exclude-gtids='cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4'  --skip-gtids /data/mysql/my3306/logs/mysql-bin.000001 > skip_gtid.sql

表示备份这个日志当中除了cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4这个事务的其它事务。

实验

场景目的:
恢复除删表动作以外的所有事务。

需求分析:

  1. 备份过程中不能包含drop table这个gitd的事务
  2. 备份文件是恢复到同一个实例,也就是gtid相同的。

1. 重置gtid

方便实验查看。 将事务从id从1开始

reset master;

2. 生成gtid

  • 创建表
mysql> create table t9 (id int primary key ,name varchar(10));
Query OK, 0 rows affected (0.03 sec)
  • 插入数据
mysql> insert into t9 values(1,'zhangsan1');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(2,'zhangsan2');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(3,'zhangsan3');
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)
  • 分析binlog
    上面的建表和插入数据的动作,产生了两个GTID_NEXT
mysql> show binlog events in 'mysql-bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                                                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
| mysql-bin.000001 |   4 | Format_desc    |      1116 |         123 | Server ver: 5.7.41-log, Binlog ver: 4                               |
| mysql-bin.000001 | 123 | Previous_gtids |      1116 |         154 |                                                                     |
| mysql-bin.000001 | 154 | Gtid           |      1116 |         219 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1'   |
| mysql-bin.000001 | 219 | Query          |      1116 |         349 | use `dbsql`; create table t9 (id int primary key ,name varchar(10)) |
| mysql-bin.000001 | 349 | Gtid           |      1116 |         414 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:2'   |
| mysql-bin.000001 | 414 | Query          |      1116 |         487 | BEGIN                                                               |
| mysql-bin.000001 | 487 | Table_map      |      1116 |         536 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 536 | Write_rows     |      1116 |         586 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 586 | Table_map      |      1116 |         635 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 635 | Write_rows     |      1116 |         685 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 685 | Table_map      |      1116 |         734 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 734 | Write_rows     |      1116 |         784 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 784 | Xid            |      1116 |         815 | COMMIT /* xid=29003260 */                                           |
+------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
13 rows in set (0.00 sec)
  • 插入数据并手动提交
mysql> insert into t9 values(4,'zhangsan4');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t9 values(5,'zhangsan5');
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)
  • 产生gitd
mysql> show binlog events in 'mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
| mysql-bin.000001 |    4 | Format_desc    |      1116 |         123 | Server ver: 5.7.41-log, Binlog ver: 4                               |
| mysql-bin.000001 |  123 | Previous_gtids |      1116 |         154 |                                                                     |
| mysql-bin.000001 |  154 | Gtid           |      1116 |         219 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1'   |
| mysql-bin.000001 |  219 | Query          |      1116 |         349 | use `dbsql`; create table t9 (id int primary key ,name varchar(10)) |
| mysql-bin.000001 |  349 | Gtid           |      1116 |         414 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:2'   |
| mysql-bin.000001 |  414 | Query          |      1116 |         487 | BEGIN                                                               |
| mysql-bin.000001 |  487 | Table_map      |      1116 |         536 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  536 | Write_rows     |      1116 |         586 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  586 | Table_map      |      1116 |         635 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  635 | Write_rows     |      1116 |         685 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  685 | Table_map      |      1116 |         734 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  734 | Write_rows     |      1116 |         784 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  784 | Xid            |      1116 |         815 | COMMIT /* xid=29003260 */                                           |
| mysql-bin.000001 |  815 | Gtid           |      1116 |         880 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:3'   |
| mysql-bin.000001 |  880 | Query          |      1116 |         953 | BEGIN                                                               |
| mysql-bin.000001 |  953 | Table_map      |      1116 |        1002 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 1002 | Write_rows     |      1116 |        1052 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 1052 | Table_map      |      1116 |        1101 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 1101 | Write_rows     |      1116 |        1151 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 1151 | Xid            |      1116 |        1182 | COMMIT /* xid=29003273 */                                           |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
20 rows in set (0.00 sec)
  • 删表
mysql> drop table t9;
Query OK, 0 rows affected (0.01 sec)
  • 新产生gtid
mysql> show binlog events in 'mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
| mysql-bin.000001 |    4 | Format_desc    |      1116 |         123 | Server ver: 5.7.41-log, Binlog ver: 4                               |
| mysql-bin.000001 |  123 | Previous_gtids |      1116 |         154 |                                                                     |
| mysql-bin.000001 |  154 | Gtid           |      1116 |         219 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1'   |
| mysql-bin.000001 |  219 | Query          |      1116 |         349 | use `dbsql`; create table t9 (id int primary key ,name varchar(10)) |
| mysql-bin.000001 |  349 | Gtid           |      1116 |         414 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:2'   |
| mysql-bin.000001 |  414 | Query          |      1116 |         487 | BEGIN                                                               |
| mysql-bin.000001 |  487 | Table_map      |      1116 |         536 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  536 | Write_rows     |      1116 |         586 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  586 | Table_map      |      1116 |         635 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  635 | Write_rows     |      1116 |         685 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  685 | Table_map      |      1116 |         734 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 |  734 | Write_rows     |      1116 |         784 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 |  784 | Xid            |      1116 |         815 | COMMIT /* xid=29003260 */                                           |
| mysql-bin.000001 |  815 | Gtid           |      1116 |         880 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:3'   |
| mysql-bin.000001 |  880 | Query          |      1116 |         953 | BEGIN                                                               |
| mysql-bin.000001 |  953 | Table_map      |      1116 |        1002 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 1002 | Write_rows     |      1116 |        1052 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 1052 | Table_map      |      1116 |        1101 | table_id: 127 (dbsql.t9)                                            |
| mysql-bin.000001 | 1101 | Write_rows     |      1116 |        1151 | table_id: 127 flags: STMT_END_F                                     |
| mysql-bin.000001 | 1151 | Xid            |      1116 |        1182 | COMMIT /* xid=29003273 */                                           |
| mysql-bin.000001 | 1182 | Gtid           |      1116 |        1247 | SET @@SESSION.GTID_NEXT= 'cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4'   |
| mysql-bin.000001 | 1247 | Query          |      1116 |        1364 | use `dbsql`; DROP TABLE `t9` /* generated by server */              |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------+
22 rows in set (0.00 sec)

3. 备份_skip_include_exclude_gtids

说明:

  1. 备份过程中不能包含drop table这个gitd的事务
  2. 如果是恢复到相同gitid的实例,一定要加skip-gtids
  • 备份包含指定的gitd
    表示只备份cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1-3的事务
 mysqlbinlog --include-gtids='cc997b24-cd1a-11ed-ac6d-000c2945c1f2:1-3' /data/mysql/my3306/logs/mysql-bin.000001 > include_gtid_sql.sql 
  • 备份排除的id
 mysqlbinlog --exclude-gtids='cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4' /data/mysql/my3306/logs/mysql-bin.000001 > exclude_gtid_sql.sql 
  • 备份_不包含gtid
    表示备份这个日志当中除了cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4这个事务的其它事务。
mysqlbinlog --exclude-gtids='cc997b24-cd1a-11ed-ac6d-000c2945c1f2:4'  --skip-gtids /data/mysql/my3306/logs/mysql-bin.000001 > skip_gtid.sql

4. 恢复测试

如果不包含skip-gtids,无法应用gtid

mysql> source include_gtid_sql.sql  ;
mysql> select * from dbsql.t9;
ERROR 1146 (42S02): Table 'dbsql.t9' doesn't exist

如果不包含skip-gtids,无法应用gtid

mysql> source exclude_gtid_sql.sql ;
mysql> select * from dbsql.t9;
ERROR 1146 (42S02): Table 'dbsql.t9' doesn't exist

包含了skip-gtids,可以恢复.

mysql> source skip_gtid.sql;
mysql> select * from dbsql.t9;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | zhangsan1 |
|  2 | zhangsan2 |
|  3 | zhangsan3 |
|  4 | zhangsan4 |
|  5 | zhangsan5 |
+----+-----------+
5 rows in set (0.01 sec)

你可能感兴趣的:(数据库,mysql)