MySQL5.7 的GTID复制
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复制的更多相关文章
- Linux学习-基于CentOS7的MySQL5.7的GTID复制
一.GTID复制简介 GTID (global transaction id)全局事务标识符,MySQL5.6版本开始支持,GTID复制不像传统的复制方式(导步复制.半同步复制)需要找到binlog和 ...
- Centos7.5部署MySQL5.7基于GTID主从复制+并行复制+半同步复制+读写分离(ProxySQL) 环境- 运维笔记 (完整版)
之前已经详细介绍了Mysql基于GTID主从复制的概念,原理和配置,下面整体记录下MySQL5.7基于GTID主从复制+并行复制+增强半同步复制+读写分离环境的实现过程,以便加深对mysql新特性GT ...
- MySQL5.7不停业务将传统复制变更为GTID复制
由于GTID的优势,我们需要将传统基于file-pos的复制更改为基于GTID的复制,如何在线变更成为我们关心的一个点,如下为具体的方法: 目前我们有一个传统复制下的M-S结构: port 330 ...
- mysql之 MySQL 主从基于 GTID 复制原理概述
一. 什么是GTID ( Global transaction identifiers ):MySQL-5.6.2开始支持,MySQL-5.6.10后完善,GTID 分成两部分,一部分是服务的UUid ...
- MySQL的GTID复制与传统复制的相互转换
主库:192.168.225.128:3307从库1:192.168.225.129:3307 Gtid作为5.6版本以来的杀手级特性,却因为不支持拓扑结构内开关而饱受诟病.如果你需要从未开启GTID ...
- 深入MySQL复制(二):基于GTID复制
相比传统的MySQL复制,gtid复制无论是配置还是维护都要轻松的多.本文对gtid复制稍作介绍. MySQL基于GTID复制官方手册:https://dev.mysql.com/doc/refman ...
- Mysql基于GTID复制模式-运维小结 (完整篇)
先来看mysql5.6主从同步操作时遇到的一个报错:mysql> change master to master_host='192.168.10.59',master_user='repli' ...
- MHA-手动Failover流程(传统复制>ID复制)
本文仅梳理手动Failover流程.MHA的介绍详见:MySQL高可用架构之MHA 一.基本环境 1.1.复制结构 VMware10.0+CentOS6.9+MySQL5.7.21 ROLE HOST ...
- MySQL 5.7基于GTID复制的常见问题和修复步骤(二)
[问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when r ...
随机推荐
- Django框架中的Context使用
Django框架中的Context使用 2017年11月09日 20:01:09 aweilark 阅读数:1113 转载自:http://www.aichengxu.com/python/606 ...
- Kafka消费时报错:Producer connection to xxx:9092 unsuccessful
使用kafka消费数据时报Producer错误,具体错误如下: kafka.producer.SyncProducer:103 Producer connection to xxx:9092 unsu ...
- Bootstrap 框架
一,Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局, ...
- Ionic3的http请求如何实现token验证,并且超时返回登录页
要求 后台提供的接口,不能让人随便输入个链接就能访问,而是要加入一个token,token是动态的,每次访问的时候判断,有权限并且未过期的时候才可以访问接口. 后台的设计是 在登录的时候,首先要pos ...
- C# 获取变量或对象的栈与堆地址
C# 获取变量或对象的栈与堆地址 来源 https://www.cnblogs.com/xiaoyaodijun/p/6605070.html using System; using System.C ...
- 前后端分离之vue2.0+webpack2 实战项目 -- html模板拼接
对于前后端分离,如何把一个页面的公共部分比如head, header, footer, content等组合成一个完整的html 是一个值得考虑的地方. 对于php,我们可以利用include加载其他 ...
- 【HDU - 4340】Capturing a country(树形DP)
BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...
- HNOI2019 退役记
窗外风雨大作. 凌晨两点又被噩梦惊醒,朦胧中看见书桌的灯还亮着. 走近书桌,有一个人正在敲打着键盘,屏幕上是一些似曾相识的东西. 被水浸湿的头发随意地搭在额头上,鼻梁上架着一副眼镜,镜片上全是小水珠, ...
- 每天一个Linux命令(03):du命令
du命令 今天找开发定位问题,看到他使用了这个命令,查看文件,之前知道df,所以今天的每天系列把这命令 du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空 ...
- [NOI2010]超级钢琴(RMQ+堆)
小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中Ai可正可负 ...