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模式操作步骤

----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

MySQL 5.7 在线启用和关闭GTID的更多相关文章

  1. MySQL 在线开启&关闭GTID模式

    MySQL 在线开启&关闭GTID模式 目录 MySQL 在线开启&关闭GTID模式 基本概述 在线开启GTID 1. 设置GTID校验ENFORCE_GTID_CONSISTENCY ...

  2. MySQL5.7 GTID在线开启与关闭【转】

    当前场景   当前某些业务还有未开启GTID服务组,升级5.7后,如何检测是否符合开启GTID条件,如何在线修改切换使用GTID:已经升级5.7后,已经开启GTID,如何快速回滚后退: 线上gtid如 ...

  3. MYSQL主从复制搭建及切换操作(GTID与传统)

    结构如下: MYSQL主从复制方式有默认的复制方式异步复制,5.5版本之后半同步复制,5.6版本之后新增GTID复制,包括5.7版本的多源复制. MYSQL版本:5.7.20 操作系统版本:linux ...

  4. SELinux查看、启用、关闭

    SELinux查看.启用.关闭 查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux ...

  5. MySQL管理_数据库启动与关闭

    MySQL数据库服务器通常指的的是mysqld,而命令行mysql则是mysql客户端程序,这两个概念通常容易混淆.通常启动mysql服务器即是启动mysqld进程,mysqld启动后,可以通过mys ...

  6. 如何在Windows Server 2016启用或关闭Internet Explorer增强的安全配置

    一般我们安装完服务器后,开启 Internet Explorer 会发现无法上网或者上网内容被屏蔽掉了 问题的发生原因 在 Windows Server 2016 通常扮演重要的服务器角色,不应该用来 ...

  7. 利用 bat 批量处理命令实现手动控制mysql /Oracle 服务的开启和关闭

    利用 bat 批量处理命令实现手动控制mysql /Oracle 服务的开启和关闭 因为最近在学习数据库的知识,主要学习的是oracle 数据库,然而好巧啊,java也是在学习,我们老师现在要我们做一 ...

  8. mysql严格模式的开启、关闭

    关于mysql严格模式的开启.关闭 由于项目中对一些默认值设置问题,以及种种原因,mysql数据库需要使用非严格模式开发(mysql最近的版本默认是开启严格模式的). linux下mysql服务下操作 ...

  9. MySQL 5.7在线设置复制过滤【转】

    转自 MySQL 5.7在线设置复制过滤 - yayun - 博客园 https://www.cnblogs.com/gomysql/p/4991197.html 5.7也GA了,有许多新的特性,其中 ...

随机推荐

  1. Helvetic Coding Contest 2016 online mirror C2

    Description Further research on zombie thought processes yielded interesting results. As we know fro ...

  2. springBoot集成web service

    转载大神: https://blog.csdn.net/u011410529/article/details/68063541?winzoom=1 https://blog.csdn.net/nr00 ...

  3. myeclipse非正常关闭解决方法

    http://blog.csdn.net/xb12369/article/details/24960347

  4. myeclipse 上安装 Maven

    myeclipse 上安装 Maven3   环境准备: JDK 1.6 Maven 3.0.4 myeclipse 8.6.1 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Mav ...

  5. 操作手册_MyEclipse

    前言 假 如 你 的 人 生 有 理 想,那 么 就 一 定 要 去 追,不 管 你 现 在 的 理 想 在 别 人 看 来是 多 么 的 可 笑 , 你 也 不 用 在 乎 , 人 生 蹉 跎 几  ...

  6. Visual Studio中的引用项目和直接引用DLL文件

    在VS中引用类库时有多种方法,其中用的最多的就是在引用时选择项目选项卡引用本解决方案下的类库项目和选择浏览选项卡直接引用类库DLL文件,实际上这两种引用方式略有不同,今天就为大家总结下. C#本地项目 ...

  7. springboot集成freemarker 配置application.properties详解

    #配置freemarker详解 #spring.freemarker.allow-request-override=false # Set whether HttpServletRequest att ...

  8. python复数

    复数的概念在很久以前,数学家们被下面的等式困扰.x2=-1这是因为任何实数(无论正负)乘以自己总会得到一个非负数.一个数怎么可以乘以自己得到一负数?没有这样的实数存在.就这样18世纪,数学家们发了一个 ...

  9. 一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa  跨终端技术:HTML 5.CSS 3.JavaScri ...

  10. angularjs嵌套路由

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...