MySQL5.7 的GTID复制

                                        作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  在MySQL5.6之后其官方推出了GTID复制方式,和传统的基于bin log复制方式有所不同,接下来我们一起来了解一下它!

一.什么是GTID(global transaction identitifiers)

  GTID复制是完全基于事务的复制,即每个在主库上执行的事务都会被分配到一个唯一的全局ID并记录和应用在主库上。

  这种复制方式简化了简历slave和master/slave之间的切换工作,因为其完全不需要找当前执行的bin log和log中的位置完成切换。

  一个GTID是master上执行的任何commit事务所分配的全局唯一ID标识,其由两部分组成。即GTID = source_id:transaction_id。source_id代表主库的server_uuid,transaction_id代表事务按顺序提交的ID,比如第一个提交则是1,第十个提交的事物则是10.

  GTID集合代表一组GTID.

二.MySQL基于GTID的复制

1>.MySQL基于GTID复制的原理   

  当一个事务在主库提交时,该事务就被赋予了一个GTID,并记录在主库的binary log

  主库的binary log会被传输到从库的relay log中,从库读取此GTID并生成gtid_next系统参数

  从库验证此GTID并没有在自己的binary log 中使用,则应用此事物在从库上。

 2>.MySQL5.6和MySQL5.7GTID复制模式之间的区别

  MySQL5.6的GTID复制模式,slave必须开启binary log和log_slave_updates参数,否则启动就报错,因为需要在binary log找到同步复制的信息(UUID:事务号),注意,开启log_slave_updates参数,是把relay-log里的日志内容再记录到本地的binary log里。

  但在MySQL5.7里,官方卫做了调整,用一张gtid_executed系统表记录同步复制的信息(UUID:事务号),这样就可以不用开启log_slave_updates参数,减少了从库的压力。

  从MySQL5.7.4版本开始,GTID会存在在mysql系统库的gtid_executed表中。

[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql>
mysql>
mysql> USE mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql>
mysql> desc gtid_executed;
+----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| source_uuid | char(36) | NO | PRI | NULL | |
| interval_start | bigint(20) | NO | PRI | NULL | |
| interval_end | bigint(20) | NO | | NULL | |
+----------------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql>
mysql> SELECT * FROM gtid_executed;
Empty set (0.00 sec) mysql>
mysql>

未开启gtid复制是查看mysql.gtid_executed为空!

#node101.yinzhengjie.org.cn配置gtid生效
[root@node101 ~]#
[root@node101 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=1
gtid-mode=on
enforce-gtid-consistency=on
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server restart
Shutting down MySQL............ SUCCESS!
Starting MySQL. SUCCESS!
[root@node101 ~]# #node102.yinzhengjie.org.cn配置gtid生效
[root@node102 ~]# /etc/init.d/mysql.server start
Starting MySQL SUCCESS!
[root@node102 ~]# 2019-03-06T14:57:02.069380Z mysqld_safe A mysqld process already exists [root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@node102 ~]#
[root@node102 ~]#

主库和从库都需要修改my.cnf配置文件开启gtid并使之生效!需重启MySQL实例!

[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql>
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec) mysql>
mysql> RESET SLAVE ALL;
Query OK, 0 rows affected (0.00 sec) mysql>
mysql> CHANGE MASTER TO
-> MASTER_HOST='node101.yinzhengjie.org.cn',
-> MASTER_PORT=3306,
-> MASTER_USER='copy',
-> MASTER_PASSWORD='yinzhengjie',
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql>
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec) mysql>
mysql>
mysql>
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: node101.yinzhengjie.org.cn
Master_User: copy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: yinzhengjie-mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: node102-relay-bin.000002
Relay_Log_Pos: 391
Relay_Master_Log_File: yinzhengjie-mysql-bin.000005
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: 154
Relay_Log_Space: 600
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: 1
Master_UUID: b2127a6e-3cf8-11e9-ae0d-000c29fe9bef
Master_Info_File: /yinzhengjie/softwares/mysql/data/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:
Executed_Gtid_Set:
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec) mysql>

从库上重新配置SLAVE相关参数

[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql> use course
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+------------------+
| Tables_in_course |
+------------------+
| course |
| dept |
| score |
| students |
| teacher |
+------------------+
5 rows in set (0.00 sec) mysql> select * from students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | Jason Yin | 0 | 1 |
| 2 | Andy | 0 | 1 |
| 3 | Bob | 0 | 1 |
| 4 | Ruth | 1 | 2 |
| 5 | Mike | 0 | 2 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql>
mysql> insert into students values(6,'Jenny',1,1);
Query OK, 1 row affected (0.00 sec) mysql>
mysql> show master status;
+------------------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+----------------------------------------+
| yinzhengjie-mysql-bin.000005 | 436 | | | b2127a6e-3cf8-11e9-ae0d-000c29fe9bef:1 |
+------------------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec) mysql>
mysql>
mysql> show master status;
+------------------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+------------------------------------------+
| yinzhengjie-mysql-bin.000005 | 718 | | | b2127a6e-3cf8-11e9-ae0d-000c29fe9bef:1-2 |
+------------------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec) mysql>
mysql> select * from students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | Jason Yin | 0 | 1 |
| 2 | Andy | 0 | 1 |
| 3 | Bob | 0 | 1 |
| 4 | Ruth | 1 | 2 |
| 5 | Mike | 0 | 2 |
| 6 | Jenny | 1 | 1 |
| 7 | DANNY | 0 | 2 |
+-----+-----------+--------+---------+
7 rows in set (0.00 sec) mysql>

主库上插入测试数据

[root@node102 ~]#
[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql>
mysql> USE course
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | Jason Yin | 0 | 1 |
| 2 | Andy | 0 | 1 |
| 3 | Bob | 0 | 1 |
| 4 | Ruth | 1 | 2 |
| 5 | Mike | 0 | 2 |
| 6 | Jenny | 1 | 1 |
| 7 | DANNY | 0 | 2 |
+-----+-----------+--------+---------+
7 rows in set (0.00 sec) mysql>
mysql>

从库上已经同步到了从库

mysql> SELECT * FROM mysql.gtid_executed;                      #这张表记录了复制了哪些事物!
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| b2127a6e-3cf8-11e9-ae0d-000c29fe9bef | 1 | 1 |
| b2127a6e-3cf8-11e9-ae0d-000c29fe9bef | 2 | 2 |
+--------------------------------------+----------------+--------------+
2 rows in set (0.00 sec) mysql>
mysql>

  

三.MySQL使用GTID复制的限制条件

  由于GTID复制是依赖于事物的,所以MySQL的一些属性不支持。

  当一个事物中既包含对InnoDB表的操作,也包含对非事物型存储引擎表(MyISAM)的操作时,就会导致一个事物中可能会产生多个GTID的情况;或者是当master和slave的表使用的存储引擎不一样时,都会导致GTID复制功能不正常。

  CREATE TABLE ... SELECT语句在基于语句复制的环境中是不安全的,在基于行复制的环境中,此语句会被拆分成两个事件,一个是创建表,一个是INSERT数据,在某些情况下这两个事件会被分配相同的GTID,而导致insert的操作被忽略,所以GTID复制不支持CREATE TABLE .... SELECT语句。

[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql>
mysql> CREATE TABLE temp SELECT * FROM course.students;
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
mysql>
mysql>

mysql> CREATE TABLE temp SELECT * FROM course.students;

  CREATE/DROP TEMPORARY TABLE语句在GTID复制环境中不能放在事物中执行,只能单独执行,并且autocommit要开启

  sql_slave_skip_counter语句是不支持的,如果想要跳过事务,可以使用gitd_exected变量。

MySQL5.7 的GTID复制的更多相关文章

  1. Linux学习-基于CentOS7的MySQL5.7的GTID复制

    一.GTID复制简介 GTID (global transaction id)全局事务标识符,MySQL5.6版本开始支持,GTID复制不像传统的复制方式(导步复制.半同步复制)需要找到binlog和 ...

  2. Centos7.5部署MySQL5.7基于GTID主从复制+并行复制+半同步复制+读写分离(ProxySQL) 环境- 运维笔记 (完整版)

    之前已经详细介绍了Mysql基于GTID主从复制的概念,原理和配置,下面整体记录下MySQL5.7基于GTID主从复制+并行复制+增强半同步复制+读写分离环境的实现过程,以便加深对mysql新特性GT ...

  3. MySQL5.7不停业务将传统复制变更为GTID复制

      由于GTID的优势,我们需要将传统基于file-pos的复制更改为基于GTID的复制,如何在线变更成为我们关心的一个点,如下为具体的方法: 目前我们有一个传统复制下的M-S结构: port 330 ...

  4. mysql之 MySQL 主从基于 GTID 复制原理概述

    一. 什么是GTID ( Global transaction identifiers ):MySQL-5.6.2开始支持,MySQL-5.6.10后完善,GTID 分成两部分,一部分是服务的UUid ...

  5. MySQL的GTID复制与传统复制的相互转换

    主库:192.168.225.128:3307从库1:192.168.225.129:3307 Gtid作为5.6版本以来的杀手级特性,却因为不支持拓扑结构内开关而饱受诟病.如果你需要从未开启GTID ...

  6. 深入MySQL复制(二):基于GTID复制

    相比传统的MySQL复制,gtid复制无论是配置还是维护都要轻松的多.本文对gtid复制稍作介绍. MySQL基于GTID复制官方手册:https://dev.mysql.com/doc/refman ...

  7. Mysql基于GTID复制模式-运维小结 (完整篇)

    先来看mysql5.6主从同步操作时遇到的一个报错:mysql> change master to master_host='192.168.10.59',master_user='repli' ...

  8. MHA-手动Failover流程(传统复制&GTID复制)

    本文仅梳理手动Failover流程.MHA的介绍详见:MySQL高可用架构之MHA 一.基本环境 1.1.复制结构 VMware10.0+CentOS6.9+MySQL5.7.21 ROLE HOST ...

  9. MySQL 5.7基于GTID复制的常见问题和修复步骤(二)

    [问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when r ...

随机推荐

  1. kubernetes ceph-rbd挂载步骤 类型storageClass

    由于kubelet本身并不支持rbd的命令,所以需要添加一个kube系统插件: 下载插件 quay.io/external_storage/rbd-provisioner 下载地址: https:// ...

  2. 第五十六 css选择器和盒模型

    1.组合选择器 群组选择器 #每个选择为可以位三种基础选择器任意一个,用逗号隔开,控制多个. div,#div,.div{ color:red } 后代(子代)选择器 .sup .sub{ 后代 } ...

  3. Educational Codeforces Round 62 (Rated for Div. 2)

    A. Detective Book 题意:一个人读书  给出每一章埋的坑在第几页可以填完 . 一个人一天如果不填完坑他就会一直看 问几天能把这本书看完 思路:模拟一下 取一下过程中最大的坑的页数  如 ...

  4. 洛谷P1373小a和uim大逃离题解

    题目 这个题好坑啊,首先是他会卡空间,然后我们就只能把一种比较好理解的状态给舍弃,因为空间开不下,然而采用一种难理解的状态就是\(dp[i][j][l][0/1]\)表示\(i\),\(j\)位置,两 ...

  5. Docker基本使用(一)

    使用docker输入hello world Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world $ docker ru ...

  6. codeforces 600E . Lomsat gelral (线段树合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  7. thymeleaf中的判断总结

    判断String字符串,添加引号 th:class="${flag=='forum.html'}?'active'" 判断boolean类型,注意不能当成字符串处理,不能添加引号 ...

  8. 【CF997E】Good Subsegments (线段树+单调栈)

    Description 原题链接 给你一个长度为\(n\)的排列\(~P\),定义一段子区间是好的,当且仅当这个子区间内的值构成了连续的一段.例如对于排列\(\{1,3,2 \}\),\([1, 1] ...

  9. 【BZOJ2034】最大收益(贪心)

    [BZOJ2034]最大收益(贪心) 题面 BZOJ 题解 首先显然让价值越大的占用一个时刻一定更优. 所以把所有东西按照价值排序之后来处理,那么显然就是把前面的全部放好之后,考虑来放当前这个东西,如 ...

  10. [luogu4479][BJWC2018]第k大斜率【二维偏序+二分+离散化+树状数组】

    传送门 https://www.luogu.org/problemnew/show/P4479 题目描述 在平面直角坐标系上,有 n 个不同的点.任意两个不同的点确定了一条直线.请求出所有斜率存在的直 ...