新特性解读 | InnoDB ReplicaSet:MySQL 副本集初体验
来源:SegmentFault
时间:2023-01-16 19:38:59 379浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《新特性解读 | InnoDB ReplicaSet:MySQL 副本集初体验》,介绍一下MySQL,希望对大家的知识积累有所帮助,助力实战开发!
作者:任仲禹
一、InnoDB ReplicaSet 介绍
- MySQL 副本集(官方名称:MySQL InnoDB ReplicaSet)在 MySQL 8.0.19 版本(2020-01-13 Released)之后开始支持,本质还是是基于 GTID 的异步复制
-
角色分为 Primary 和 Secondary
- Primary 即传统意义上的 Master,一个副本集只允许一个
- Secondary 即 Slave,允许一个或多个
- 通过 MySQL Shell 自带的 AdminAPI 创建、配置、删除等管理副本集
- 通过 MySQL Router 使用副本集,引导与连接方式与 InnoDB Cluster 和 MGR 有点类似,不同之处在于新增了 cluster_type = rs 集群类型。
二、通过 MySQL Shell 部署 Sandbox 实例
- MySQL Shell 除了集成 AdminAPI 外还提供了 MySQL Sandbox 功能,可轻松部署用以测试的 MySQL 数据库实例
- 通过 Sandbox 一键部署三个 MySQL 实例
# mysqlsh MySQL JS > dba.deploySandboxInstance(3306) MySQL JS > dba.deploySandboxInstance(3307) MySQL JS > dba.deploySandboxInstance(3308)
- 指定 root 密码后自动创建 MySQL 实例,默认数据目录在 $HOME/mysql-sandboxes/port

三、通过 MySQL Shell 配置 InnoDB 副本集
3.1 创建集群管理账户
- 创建集群管理员账户 repl 作为具有管理 InnoDB ReplicaSet 所需的权限集合
MySQL JS > dba.configureReplicaSetInstance('root@localhost:3306', {clusterAdmin: "'repl'@'%'", clusterAdminPassword: 'repl'});

3.2 创建 InnoDB 副本集
- 连接到第一个 MySQL 实例 3306,创建命名为 renzy 的副本集
MySQL JS > \connect root@localhost:3306
MySQL localhost:3306 ssl JS > var rs = dba.createReplicaSet("renzy")

- 查看副本集状态,默认第一个实例 3306 选举为 Primary 节点
MySQL localhost:3306 ssl JS > rs.status()
{
"replicaSet": {
"name": "renzy",
"primary": "127.0.0.1:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"127.0.0.1:3306": {
"address": "127.0.0.1:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}
3.3 添加节点到副本集
MySQL localhost:3306 ssl JS > rs.addInstance('localhost:3307')
MySQL localhost:3306 ssl JS > rs.addInstance('localhost:3308')
- 添加节点 3307 和 3308 到副本集涉及到数据拷贝有两种方式:Clone 全量同步和 Inremental recovery 增量同步,本文使用 Clone 全量同步方式从 Primary 节点全量同步数据

- 查看副本集状态,已添加到副本集的实例 3307 和 3308 的角色为 Secondary ,并自动与 Primary 节点 3306 建立复制关系
MySQL localhost:3306 ssl JS > rs.status()
{
"replicaSet": {
"name": "renzy",
"primary": "127.0.0.1:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"127.0.0.1:3306": {
"address": "127.0.0.1:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
},
"127.0.0.1:3307": {
"address": "127.0.0.1:3307",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Slave has read all relay log; waiting for more updates",
"receiverStatus": "ON",
"receiverThreadState": "Waiting for master to send event",
"replicationLag": null
},
"status": "ONLINE"
},
"127.0.0.1:3308": {
"address": "127.0.0.1:3308",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Slave has read all relay log; waiting for more updates",
"receiverStatus": "ON",
"receiverThreadState": "Waiting for master to send event",
"replicationLag": null
},
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}
3.4 副本集在线主从切换
- 手工在线将实例 3308 切换为 Primary 节点
MySQL localhost:3306 ssl JS > rs.setPrimaryInstance('127.0.0.1:3308')
- 实例 3308 被提升为 Primary 后,副本集将自动将 实例 3306 降级为 Secondary 并与 3308 建立复制关系,副本集中其它实例 3307 也将自动与 3308 建立复制与同步
MySQL localhost:3306 ssl JS > rs.status()
{
"replicaSet": {
"name": "renzy",
"primary": "127.0.0.1:3308",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"127.0.0.1:3306": {
"address": "127.0.0.1:3306",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Slave has read all relay log; waiting for more updates",
"receiverStatus": "ON",
"receiverThreadState": "Waiting for master to send event",
"replicationLag": null
},
"status": "ONLINE"
},
"127.0.0.1:3307": {
"address": "127.0.0.1:3307",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Slave has read all relay log; waiting for more updates",
"receiverStatus": "ON",
"receiverThreadState": "Waiting for master to send event",
"replicationLag": null
},
"status": "ONLINE"
},
"127.0.0.1:3308": {
"address": "127.0.0.1:3308",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}
3.5 副本集 Primary 节点故障
- 手工杀掉 Primary 节点进程
# ps -ef | grep 3308 root 18975 1 0 16:52 ? 00:01:23 /root/mysql-sandboxes/3308/bin/mysqld --defaults-file=/root/mysql-sandboxes/3308/my.cnf --user=root # kill -9 18975
- 副本集 无法自动进行故障转移,需要人工介入修复

- 手工将 Secondary 节点 3306 强制提升为 Primary
MySQL localhost:3306 ssl JS > rs.forcePrimaryInstance("127.0.0.1:3306")
- 副本集恢复后,因 3308 不可用副本集状态显示为 AVAIABLE_PARITAL (部分可用)

四、通过 MySQL Router 使用副本集
- 与使用 MySQL Router 连接 MGR 或 InnoDB Cluster 一样,副本集也可以通过 MySQL Router 访问,首先通过 --bootstrap 选项引导副本集
mysqlrouter --user=root --bootstrap root@localhost:3308

五、MySQL Router 通过 R/W 自动连接到 Primary
- 启动 MySQL Router
mysqlrouter -c /usr/local/mysql-router-8.0.19-linux-glibc2.12-x86_64/mysqlrouter.conf & // 通过 MySQL Router R/W 端口可以自动识别并连接到 Primary # mysql -h10.186.63.158 -P6446 -uroot -proot -e 'select @@port;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------+ | @@port | +--------+ | 3308 | +--------+ # mysql -h10.186.63.158 -P6446 -uroot -proot -e 'show slave hosts;' mysql: [Warning] Using a password on the command line interface can be insecure. +------------+-----------+------+------------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +------------+-----------+------+------------+--------------------------------------+ | 1587786506 | 127.0.0.1 | 3307 | 2457151498 | cb569215-3b29-11ea-80f4-02000aba3f9e | | 3616586416 | 127.0.0.1 | 3306 | 2457151498 | c23ee4be-3b29-11ea-815a-02000aba3f9e | +------------+-----------+------+------------+--------------------------------------+
- 副本集主从切换后,MySQL Router R/W 自动指向被选举出来的新的 Primary


六、结论
- MySQL Router 可以很好的兼容 InnoDB ReplicaSet,可自动识别到副本集主从切换,将新的 R/W 连接指向 Primary。
- InnoDB ReplicaSet 当前还不完善,可作为新特性在测试环境试用,但因为不支持自动故障转移,Primary 宕机整个副本集将不可用。
- InnoDB ReplicaSet 目前仅支持基于 GTID 的异步复制,哪怕支持自动切换,数据也有丢失风险,所以离真正部署到生产环境还有一段路要走。
- InnoDB ReplicaSet 暂时还没有类似芒果 DB 完善的投票选举机制,故障切换时也会存在脑裂风险。
总之,InnoDB 副本集虽存在诸多不足之处,但作为 2020 年 Oracle 的开餐甜点其带来的效果也让众多 MySQL DBA 眼前一亮,既然有了副本集,相信 Sharding 也是未来可期,你觉得呢?
好了,本文到此结束,带大家了解了《新特性解读 | InnoDB ReplicaSet:MySQL 副本集初体验》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
117 收藏
-
411 收藏
-
420 收藏
-
264 收藏
-
266 收藏
-
392 收藏
-
333 收藏
-
234 收藏
-
448 收藏
-
416 收藏
-
225 收藏
-
145 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习