1、基础知识
MySQL 5.7.6之后GTID_MODE提供了两个新的选项分别为ON_PERMISSIVE和OFF_PERMISSIVE
OFF_PERMISSIVE:不产生GTID事务, Slave接受不带GTID的事务(匿名事务)也接受带GTID的事务
ON_PERMISSIVE:新事务为gtid事务,slave接受GTID事务也接受不带GTID事务
GTID_MODE在主备库上的兼容情况如下:
gtid_next参数设置在匿名模式和GTID模式下的兼容情况
2、在线开启GTID模式操作步骤
2.1、主要操作
1、检查数据库中是否有不支持gtid的操作
设置ENFORCE_GTID_CONSISTENCY = WARN,运行一段时间,最好包含一天负载最高的时间段,有条件建议观察2~3天,检查日志是否有告警产生
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN;
2、如果步骤一没有告警产生,设置ENFORCE_GTID_CONSISTENCY为ON
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON;
3、设置GTID_MODE = OFF_PERMISSIVE
SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;
4、设置GTID_MODE = OFF_PERMISSIVE
SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE;
5、确保在线的匿名事物数为0,设置GTID_MODE=ON
SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT';
SET @@GLOBAL.GTID_MODE = ON;
6、重启slave,设置为GTID模式
STOP SLAVE [FOR CHANNEL 'channel'];
CHANGE MASTER TO MASTER_AUTO_POSITION = 1 [FOR CHANNEL 'channel'];
START SLAVE [FOR CHANNEL 'channel'];
7、将gtid参数配置到参数文件中
ENFORCE_GTID_CONSISTENCY = ON
GTID_MODE=ON
2.2、实验,在上述每个阶段均产生一个事物验证gtid的产生
主库操作日志
mysql> SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> insert into test(name) value('yong1');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(name) value('yong2');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test(name) value('yong3');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0 |
+-------------------------------------+-------+
1 row in set (0.01 sec)
mysql>
mysql> SET @@GLOBAL.GTID_MODE = ON;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into test(name) value('yong4');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
备库操作日志mysql> SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SET @@GLOBAL.GTID_MODE = ON_PERMISSIVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0 |
+-------------------------------------+-------+
1 row in set (0.00 sec)
mysql> SET @@GLOBAL.GTID_MODE = ON;
Query OK, 0 rows affected (0.06 sec)
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | yong |
| 2 | yong1 |
| 3 | yong2 |
| 4 | yong3 |
| 5 | yong4 |
+----+-------+
5 rows in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: repl
Master_Port: 3706
Connect_Retry: 60
Master_Log_File: binlog.000008
Read_Master_Log_Pos: 520
Relay_Log_File: relay.000002
Relay_Log_Pos: 687
Relay_Master_Log_File: binlog.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 520
Relay_Log_Space: 924
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 103706
Master_UUID: 6a8c58b0-51a7-11e8-8d8c-000c29991327
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 6a8c58b0-51a7-11e8-8d8c-000c29991327:2
Executed_Gtid_Set: 6a8c58b0-51a7-11e8-8d8c-000c29991327:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
binlog解析第一个insert语句
SET TIMESTAMP=1525674136/*!*/;
BEGIN
/*!*/;
# at 847
#180507 14:22:16 server id 103706 end_log_pos 908 CRC32 0x39db9e80 Rows_query
# insert into test(name) value('yong1')
# at 908
#180507 14:22:16 server id 103706 end_log_pos 958 CRC32 0x0043983a Table_map: `yong`.`test` mapped to number 108
# at 958
#180507 14:22:16 server id 103706 end_log_pos 1005 CRC32 0x751f37c9 Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `yong`.`test`
### SET
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='yong1' /* VARSTRING(400) meta=400 nullable=0 is_null=0 */
# at 1005
#180507 14:22:16 server id 103706 end_log_pos 1036 CRC32 0xcd38a654 Xid = 46
COMMIT/*!*/;
第二个insert语句
BEGIN
/*!*/;
# at 291
#180507 14:23:00 server id 103706 end_log_pos 352 CRC32 0xfccbcdd6 Rows_query
# insert into test(name) value('yong2')
# at 352
#180507 14:23:00 server id 103706 end_log_pos 402 CRC32 0x320fc42f Table_map: `yong`.`test` mapped to number 108
# at 402
#180507 14:23:00 server id 103706 end_log_pos 449 CRC32 0x3e671945 Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `yong`.`test`
### SET
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2='yong2' /* VARSTRING(400) meta=400 nullable=0 is_null=0 */
# at 449
#180507 14:23:00 server id 103706 end_log_pos 480 CRC32 0x0fc37aa9 Xid = 49
COMMIT/*!*/;
第三条insert语句产生gtid
# at 480
#180507 14:23:10 server id 103706 end_log_pos 524 CRC32 0x02d4d256 Rotate to binlog.000007 pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180507 14:23:10 server id 103706 end_log_pos 123 CRC32 0xf9248b17 Start: binlog v 4, server v 5.7.22-log created 180507 14:23:10
# at 123
#180507 14:23:10 server id 103706 end_log_pos 154 CRC32 0x2f87c3f1 Previous-GTIDs
# [empty]
# at 154
#180507 14:23:23 server id 103706 end_log_pos 219 CRC32 0x58b85659 GTID last_committed=0 sequence_number=1 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '6a8c58b0-51a7-11e8-8d8c-000c29991327:1'/*!*/;
# at 219
#180507 14:23:23 server id 103706 end_log_pos 291 CRC32 0x67b3e766 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1525674203/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549120/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 291
#180507 14:23:23 server id 103706 end_log_pos 352 CRC32 0x22d72bfc Rows_query
# insert into test(name) value('yong3')
# at 352
#180507 14:23:23 server id 103706 end_log_pos 402 CRC32 0x4ff170b9 Table_map: `yong`.`test` mapped to number 108
# at 402
#180507 14:23:23 server id 103706 end_log_pos 449 CRC32 0x91443ddd Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `yong`.`test`
### SET
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='yong3' /* VARSTRING(400) meta=400 nullable=0 is_null=0 */
# at 449
#180507 14:23:23 server id 103706 end_log_pos 480 CRC32 0x09004f94 Xid = 52
COMMIT/*!*/;
第四条insert语句
# at 480
#180507 14:23:59 server id 103706 end_log_pos 524 CRC32 0xfb775fc3 Rotate to binlog.000008 pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180507 14:23:59 server id 103706 end_log_pos 123 CRC32 0x5fab71ad Start: binlog v 4, server v 5.7.22-log created 180507 14:23:59
# Warning: this binlog is either in use or was not closed properly.
# at 123
#180507 14:23:59 server id 103706 end_log_pos 194 CRC32 0x830a108d Previous-GTIDs
# 6a8c58b0-51a7-11e8-8d8c-000c29991327:1
# at 194
#180507 14:25:26 server id 103706 end_log_pos 259 CRC32 0xa1ab9f47 GTID last_committed=0 sequence_number=1 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '6a8c58b0-51a7-11e8-8d8c-000c29991327:2'/*!*/;
# at 259
#180507 14:25:26 server id 103706 end_log_pos 331 CRC32 0xa40bc261 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1525674326/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549120/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 331
#180507 14:25:26 server id 103706 end_log_pos 392 CRC32 0xedd9b134 Rows_query
# insert into test(name) value('yong4')
# at 392
#180507 14:25:26 server id 103706 end_log_pos 442 CRC32 0xaa9eefab Table_map: `yong`.`test` mapped to number 108
# at 442
#180507 14:25:26 server id 103706 end_log_pos 489 CRC32 0x3384c16f Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `yong`.`test`
### SET
### @1=5 /* INT meta=0 nullable=0 is_null=0 */
### @2='yong4' /* VARSTRING(400) meta=400 nullable=0 is_null=0 */
# at 489
#180507 14:25:26 server id 103706 end_log_pos 520 CRC32 0x566695c2 Xid = 66
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
3、在线关闭GTID模式操作步骤
3.1、主要操作
1、关闭复制,找到复制呃位置,切换到传统模式
stop slave;
change master to master_host='172.172.178.75',master_port=3306,master_user='repl',master_password='root',master_log_file='3306-bin.000056',master_log_pos=897553222,MASTER_AUTO_POSITION = 0;
##以从库的Relay_Master_Log_File和Exec_Master_Log_Pos 作为MASTER_LOG_FILE和MASTER_LOG_POS 来指定复制开始位置。
##必须要指定MASTER_AUTO_POSITION = 0,否则会有如下报错:
start slave;
2、主备依次设置GTID_MODE为ON_PERMISSIVE、OFF_PERMISSIVE
set @@global.gtid_mode=ON_PERMISSIVE;
set @@global.gtid_mode=OFF_PERMISSIVE;
3、检查global.gtid_owned变量字符串已经为空
select @@global.gtid_owned;
4、主备库关闭GTID
SET @@GLOBAL.GTID_MODE = OFF;
enforce_gtid_consistency=OFF