阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL基于GTIDs的MySQL Replication

575次阅读
没有评论

共计 3969 个字符,预计需要花费 10 分钟才能阅读完成。

MySQL M-S GTID

基于 GTIDs 的 MySQL Replication

什么是 GTIDs 以及有什么特定?

1、GTIDs(Global transaction identifiers)全局事务标识符,是 mysql 5.6 新加入的一项技术

2、当使用 GTIDs 时,每一个事务都可以被识别并且跟踪

3、添加新的 slave 或者当发生故障需要将 master 身份或者角色迁移到 slave 上时,都无需考虑是哪一个二进制日志以及哪个 position 值,极大简化了相关操作

4、GTIDs 是完全基于事务的,因此不支持 MYISAM 存储引擎

5、GTID 由 source_id 和 transaction_id 组成:
1>source_id 来自于 server_uuid, 可以在 auto.cnf 中看到
2>transation_id 是一个序列数字,自动生成

使用 GTIDs 的限制条件有哪些?

1、不支持非事务引擎(MYisam),因为可能会导致多个 gtid 分配给同一个事务
2、create table … select 语句不支持(主库语法报错)
3、create/drop temporary table 语句不支持
4、必须使用 enforce-gtid-consistency 参数
5、sql-slave-skip-counter 不支持(传统的跳过错误方式)
6、GTID 复制环境中必须要求统一开启和 GTID 或者关闭 GTID
7、在 mysql 5.6.7 之前,使用 mysql_upgrade 命令会出现问题

GTID 的生命周期包含以下部分:

1. A transaction is executed and committed on the master.
This transaction is assigned a GTID using the master’s UUID and the smallest nonzero transaction sequence number not yet used on this server; the GTID is written to the master’s binary log (immediately preceding the transaction itself in the log).

2. After the binary log data is transmitted to the slave and stored in the slave’s relay log, the slave reads the GTID and sets the value of its gtid_next system variable as this GTID. This tells the slave that the next transaction must be logged using this GTID.It is important to note that the slave sets gtid_next in a session context.

3. The slave verifies that this GTID has not already been used to log a transaction in its own binary log. If this GTID has not been used, the slave then writes the GTID, applies the transaction, and writes the transaction to its binary log. By reading and checking the transaction’s GTID first, before processing the transaction itself, the slave guarantees not only that no previous transaction having this GTID has been applied on the slave, but also that no other session has already read this GTID but has not yet committed the associated transaction. In other words, multiple clients are not permitted to apply the same transaction concurrently.

4. Because gtid_next is not empty, the slave does not attempt to generate a GTID for this transaction but instead writes the GTID stored in this variable—that is, the GTID obtained from the master—immediately preceding the transaction in its binary log.

总结:有了 GTID 大大的简化了复制的过程,降低了维护的难度

配置基于 GTIDs 的 Replication

在生产环境中,大多数情况下使用的 MySQL5.6 基本上都是从 5.5 或者更低的版本升级而来,这就意味着之前的 mysql replication 方案是基于传统的方式部署,并且已经在运行,因此,接下来我们就利用已有的环境升级至基于 GITDs 的 Replication

传统的方案部署参考:https://www.cnblogs.com/yanjieli/p/9831084.html

注意:

1、开启 GITDs 需要在 master 和 slave 上都配置gtid-mode,log-bin,log-slave-updates,enforce-gtid-consistency(该参数在 5.6.9 之前是 –disable-gtid-unsafe-statement)

2、其次,slave 还需要增加 skip-slave-start 参数, 目的是启动的时候,先不要把 slave 起来,需要做一些配置

详细操作步骤:

当前环境是传统的 AB 复制转换成 GTID 模式

master:192.168.1.166

slave:192.168.1.114

1、将 master 和 slave 服务器都设置为 read-only

mysql>set @@global.read_only=ON;

2、停止两台服务器的 mysql 服务

3、配置 master

master:[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin
gtid-mode=on
log-slave-updates
enforce-gtid-consistency
[root@master ~]# service mysqld restart

4、配置 slave

slave:[root@slave1 ~]# vim /etc/my.cnf 
gtid-mode=on
log-bin
log-slave-updates
enforce-gtid-consistency
skip-slave-start

[root@slave1 ~]# service mysqld restart

mysql> change master to master_host='192.168.1.166',master_port=3306,master_user='slave',master_password='123',master_auto_position=1;

mysql> start slave;

mysql> show slave status \G;
Auto_Position: 1

5、关闭 read-only 模式

mysql> set @@global.read_only=OFF;

6、测试

master 查看:
mysql> select * from db01.table03;
+------+------+
| id   | name |
+------+------+
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
+------+------+
6 rows in set (0.07 sec)

slave 查看:mysql> select * from db01.table03;
+------+------+
| id   | name |
+------+------+
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
+------+------+
6 rows in set (0.07 sec)

master 插入数据并查看:mysql> insert into db01.table03 values(5,'ouou');
Query OK, 1 row affected (0.04 sec)

mysql> select * from db01.table03;
+------+------+
| id   | name |
+------+------+
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    5 | ouou |
+------+------+
7 rows in set (0.00 sec)

slave 查看:mysql> select * from db01.table03;
+------+------+
| id   | name |
+------+------+
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    1 | haha |
|    2 | wowo |
|    4 | yoyo |
|    5 | ouou |
+------+------+
7 rows in set (0.00 sec)

并且 master 上面操作后查看 slave 的状态,下面就会有事务产生
mysql> show slave status\G;
Retrieved_Gtid_Set: 5624c184-5b55-11e8-b117-000c293dfd08:1
Executed_Gtid_Set: 5624c184-5b55-11e8-b117-000c293dfd08:1
Auto_Position: 1

 

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-28发表,共计3969字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19348
评论数
4
阅读量
7803227
文章搜索
热门文章
开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南 大家好,我是星哥。之前介绍了腾讯云的 Code...
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板 1Panel:小白轻松构建 Web 服务与面板加固指南 哈喽,我是星哥,经常有人问我不...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装 Black 群晖 DSM7.2 系统安装教程(在 Vmware 虚拟机中、实体机均可)! 前言 大家好,...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
零成本上线!用 Hugging Face免费服务器+Docker 快速部署HertzBeat 监控平台

零成本上线!用 Hugging Face免费服务器+Docker 快速部署HertzBeat 监控平台

零成本上线!用 Hugging Face 免费服务器 +Docker 快速部署 HertzBeat 监控平台 ...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定! 前言 作为 NAS 玩家,你是否总被这些...
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸

一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸

一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸 前言 作为天天跟架构图、拓扑图死磕的...
星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的NAS中!

星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的NAS中!

星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的 NAS 中! 大家对「数据安全感」的需求越来越高 ...