广告
返回顶部
首页 > 资讯 > 数据库 >新特性解读 | MySQL 8.0 新特性-副本集(replicaset)
  • 476
分享到

新特性解读 | MySQL 8.0 新特性-副本集(replicaset)

新特性解读|MySQL8.0新特性-副本集(replicaset) 2021-10-20 09:10:42 476人浏览 才女
摘要

引言 之前,我介绍过一篇《Mysql Innodb Cluster 扫盲篇》。 mysql Innodb Cluster = Mysql shell + MySQL Router + MySQL Group Replication(MGR

新特性解读 | MySQL 8.0 新特性-副本集(replicaset)

引言

之前,我介绍过一篇《Mysql Innodb Cluster 扫盲篇》。

mysql Innodb Cluster = Mysql shell + MySQL Router + MySQL Group Replication(MGR)

全程由 MySQL Shell 来管理操作 MGR 的聚合套件。MySQL 8.0.19 发布后,这种组合延伸到 MySQL Replication(主从复制),也就是 MySQL Shell + MySQL Router + MySQL Replication。

MySQL 主从复制,相信大家已经非常了解了,原理大概如下图:

一、环境准备

举个例子,搭建主从的基本步骤大概如下:

  • 主机 A:192.168.2.171:4010

  • 从机 B/C:192.168.2.171:4011 / 192.168.2.171:4012

主机和从机经过简单的配置,后期其他配置自己任意搭配(不同的主机要将 server-id 分别改成不同的就行了

# my.cnf
server-id = 4010

log-bin=ytt

enforce_gtid_consistency=on
gtid_mode=on

log_slave_updates=on

master-info-repository=table
relay-log-info-repository=table
slave_parallel_type=logical
slave_parallel_workers=4

重启所有实例

systemctl restart mysql@{4010,4011,4012}

然后在 A 上创建主从需要的用户,并且分别在 B 和 C 上执行 change master to 命令配置从机即可。

我上面只是涉及到主从从零开始的场景,如果说主机 A 有数据,还得重加步骤。关于这点资料很多,我这里也就不多介绍了。那么对主从的管理没有搭建这么简单,稍微要繁琐些,譬如要实现如下的功能点:

  1. 主从级别互换:主机降级为从机;从机升级为主机。
  2. 从机由于异常,无法恢复正常;或者恢复正常了,发现数据和主机有差异,必须要重做从机,恢复主从关系。
  3. 列出主从关系,可能要自己写个小脚本或者是利用 MySQL Utility 工具集或者是其他的工具集合

那接下来来看看 MySQL 8.0.19 中 MySQL Shell 新特性:副本集以及管理。

二、副本集

副本集也就是极度简化了对 MySQL 主从的相关管理操作。

那接下来还是用刚才那三个实例举例说明 MySQL Shell 怎么简化了相关操作。

进入 MySQL X 端口 40100

root@ytt-pc:/home/ytt/scripts# mysqlsh mysqlx://ytt@ytt-pc:40100
MySQL Shell 8.0.19

Copyright (c) 2016, 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 "?" for help; "quit" to exit.
Creating an X protocol session to "ytt@ytt-pc:40100"
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 12 (X protocol)
Server version: 8.0.19 MySQL CommUnity Server - GPL
No default schema selected; type use  to set one.

2.1 创建副本集 ytt_replicaset

MySQL  ytt-pc:40100+ ssl  JS > var c1 = dba.createReplicaSet("ytt_replicaset")
A new replicaset with instance "ytt-pc:4010" will be created.

* Checking MySQL instance at ytt-pc:4010

This instance reports its own address as ytt-pc:4010
ytt-pc:4010: Instance configuration is suitable.

* Updating metadata...

ReplicaSet object successfully created for ytt-pc:4010.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.

查看帮助

可以看到所有的方法:c1.help()

查看当前副本集的状态

默认 RW,单机运行

 MySQL  ytt-pc:40100+ ssl  JS > c1.status()
{
    "replicaSet": {
        "name": "ytt_replicaset", 
        "primary": "ytt-pc:4010", 
        "status": "AVaiLABLE", 
        "statusText": "All instances available.", 
        "topology": {
            "ytt-pc:4010": {
                "address": "ytt-pc:4010", 
                "instanceRole": "PRIMARY", 
                "mode": "R/W", 
                "status": "ONLINE"
            }
        }, 
        "type": "ASYNC"
    }
}

添加从机

默认用克隆的方式复制数据

  MySQL  ytt-pc:40100+ ssl  JS > c1.addInstance("ytt@ytt-pc:4011")
Adding instance to the replicaset...

* PerfORMing validation checks

This instance reports its own address as ytt-pc:4011
ytt-pc:4011: Instance configuration is suitable.

* Checking async replication topology...

* Checking transaction state of the instance...

WARNING: A GTID set check of the MySQL instance at "ytt-pc:4011" determined that it contains transactions that do not originate from the replicaset, which must be discarded before it can join the replicaset.

ytt-pc:4011 has the following errant GTIDs that do not exist in the replicaset:
a19a4ac4-3a98-11ea-8f47-9cb6d0e27d15:1-7

WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of ytt-pc:4011 with a physical snapshot from an existing replicaset member. To use this method by default, set the "recoveryMethod" option to "clone".

Having extra GTID events is not expected, and it is recommended to investigate this further and ensure that the data can be removed prior to choosing the clone recovery method.

Please select a recovery method [C]lone/[A]bort (default Abort): C
* Updating topology
Waiting for clone process of the new member to complete. Press ^C to abort the operation.
* Waiting for clone to finish...
NOTE: ytt-pc:4011 is being cloned from ytt-pc:4010
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: ytt-pc:4011 is shutting down...

* Waiting for server restart... ready
* ytt-pc:4011 has restarted, waiting for clone to finish...
* Clone process has finished: 59.64 MB transferred in about 1 second (~1.00 B/s)

** Configuring ytt-pc:4011 to replicate from ytt-pc:4010
** Waiting for new instance to synchronize with PRIMARY...

The instance "ytt-pc:4011" was added to the replicaset and is replicating from ytt-pc:4010.

用同样的方法添加实例 C

MySQL  ytt-pc:40100+ ssl  JS > c1.addInstance("ytt@ytt-pc:4012")

接下来看看新的主从状态,此时 ytt-pc:4010 为主机,ytt-pc:4011 和 ytt-pc:4012 为从机,并且状态都正常

 MySQL  ytt-pc:40100+ ssl  JS > c1.status()
{
    "replicaSet": {
        "name": "ytt_replicaset", 
        "primary": "ytt-pc:4010", 
        "status": "AVAILABLE", 
        "statusText": "All instances available.", 
        "topology": {
            "ytt-pc:4010": {
                "address": "ytt-pc:4010", 
                "instanceRole": "PRIMARY", 
                "mode": "R/W", 
                "status": "ONLINE"
	           }, 
            "ytt-pc:4011": {
                "address": "ytt-pc:4011", 
                "instanceRole": "SECONDARY", 
                "mode": "R/O", 
                "replication": {
                    "applierStatus": "APPLIED_ALL", 
                    "applierThreadState": "Waiting for an event from Coordinator", 
                    "applierWorkerThreads": 4, 
                    "receiverStatus": "ON", 
                    "receiverThreadState": "Waiting for master to send event", 
                    "replicationLag": null
                }, 
                "status": "ONLINE"
            }, 
            "ytt-pc:4012": {
                "address": "ytt-pc:4012", 
                "instanceRole": "SECONDARY", 
                "mode": "R/O", 
                "replication": {
                    "applierStatus": "APPLIED_ALL", 
                    "applierThreadState": "Waiting for an event from Coordinator", 
                    "applierWorkerThreads": 4, 
                    "receiverStatus": "ON", 
                    "receiverThreadState": "Waiting for master to send event", 
                    "replicationLag": null
                }, 
                "status": "ONLINE"
            }
        }, 
        "type": "ASYNC"
    }
}
 MySQL  ytt-pc:40100+ ssl  JS > 

2.2 配置 MySQLRouter 路由实例

先用 MySQLrouter 来配置读写分离,并且分配虚拟端口

root@ytt-pc:/home/ytt/scripts# mysqlrouter --bootstrap ytt@ytt-pc:4010 --user=mysqlrouter --force-passWord-validation --report-host ytt-pc
Please enter MySQL password for ytt: 

# Bootstrapping system MySQL Router instance...

- Creating account(s) (only those that are needed, if any)
- Verifying account (using it to run SQL queries that would be run by Router)
- Storing account in keyring
- Adjusting permissions of generated files
- Creating configuration /etc/mysqlrouter/mysqlrouter.conf

Existing configuration backed up to "/etc/mysqlrouter/mysqlrouter.conf.bak"

# MySQL Router configured for the InnoDB ReplicaSet "ytt_replicaset"

After this MySQL Router has been started with the generated configuration

重启 mysqlrouter

$ /etc/init.d/mysqlrouter restart
or
$ systemctl start mysqlrouter
or
$ mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf
//重启完毕
the cluster "ytt_replicaset" can be reached by connecting to:

## MySQL Classic protocol

- Read/Write Connections: ytt-pc:6446
- Read/Only Connections:  ytt-pc:6447

## MySQL X protocol

- Read/Write Connections: ytt-pc:64460
- Read/Only Connections:  ytt-pc:64470

重启mysqlrouter
root@ytt-pc:/home/ytt/scripts# systemctl restart mysqlrouter

查看 router 路由

用 listRouters 方法详细的展示出 mysqlrouter 的路由信息,写端口为 6446/64460,读端口为 6447/64470

 MySQL  ytt-pc:40100+ ssl  JS > c1.listRouters()
{
    "replicaSetName": "ytt_replicaset", 
    "routers": {
        "ytt-pc::system": {
            "hostname": "ytt-pc", 
            "lastCheckIn": "2020-01-19 17:26:08", 
            "roPort": 6447, 
            "roXPort": 64470, 
            "rwPort": 6446, 
            "rwXPort": 64460, 
            "version": "8.0.19"
        }
    }
}

2.3 测试读写端口

连接 6446,路由到实例 4010

root@ytt-pc:/home/ytt/scripts# mysql -uytt -p -hytt-pc -P6446 -e "select @@port" -ss
Enter password: 
4010

连接到 6447,随机路由到实例 4011 或者 4012

root@ytt-pc:/home/ytt/scripts# mysql -uytt -p -hytt-pc -P6447 -e "select @@port" -ss
Enter password: 
4011
root@ytt-pc:/home/ytt/scripts# mysql -uytt -p -hytt-pc -P6447 -e "select @@port" -ss
Enter password: 
4012

以上简单的演示下 mysql shell 操作主从复制的基本方法,下来分别看看主从级别互换、从实例删除、从实例加入场景。

2.4 场景实验

断开连接

MySQL  ytt-pc:40100+ ssl  JS > c1.disconnect();

重新得到副本集的名称

MySQL  ytt-pc:40100+ ssl  JS > var c1 = dba.getReplicaSet()
You are connected to a member of replicaset "ytt_replicaset".

提升从机为新主机,这里操作的实例是 ytt-pc:4012

 MySQL  ytt-pc:40100+ ssl  JS > c1.setPrimaryInstance("ytt@ytt-pc:4012");
ytt-pc:4012 will be promoted to PRIMARY of "ytt_replicaset".
The current PRIMARY is ytt-pc:4010.

- Connecting to replicaset instances
  ** Connecting to ytt-pc:4010
  ** Connecting to ytt-pc:4011
  ** Connecting to ytt-pc:4012
  ** Connecting to ytt-pc:4010
  ** Connecting to ytt-pc:4011
  ** Connecting to ytt-pc:4012
- Performing validation checks
  ** Checking async replication topology...
  ** Checking transaction state of the instance...
- Synchronizing transaction backlog at ytt-pc:4012
- Updating metadata
- Acquiring locks in replicaset instances
  ** Pre-synchronizing SECONDARIES
  ** Acquiring global lock at PRIMARY
  ** Acquiring global lock at SECONDARIES
- Updating replication topology
  ** Configuring ytt-pc:4010 to replicate from ytt-pc:4012
  ** Changing replication source of ytt-pc:4011 to ytt-pc:4012

ytt-pc:4012 was promoted to PRIMARY.

查看新状态,4012 为新的主机,4011 和 4010 为从机

 MySQL  ytt-pc:40100+ ssl  JS > c1.status()
{
    "replicaSet": {
        "name": "ytt_replicaset", 
        "primary": "ytt-pc:4012", 
        "status": "AVAILABLE", 
        "statusText": "All instances available.", 
        "topology": {
            "ytt-pc:4010": {
                "address": "ytt-pc:4010", 
                "instanceRole": "SECONDARY", 
                "mode": "R/O", 
                "replication": {
                    "applierStatus": "APPLIED_ALL", 
                    "applierThreadState": "Waiting for an event from Coordinator", 
                    "applierWorkerThreads": 4, 
                    "receiverStatus": "ON", 
                    "receiverThreadState": "Waiting for master to send event", 
                    "replicationLag": null
                }, 
                "status": "ONLINE"
            }, 
            "ytt-pc:4011": {
                "address": "ytt-pc:4011", 
                "instanceRole": "SECONDARY", 
                "mode": "R/O", 
                "replication": {
                    "applierStatus": "APPLIED_ALL", 
                    "applierThreadState": "Waiting for an event from Coordinator", 
                    "applierWorkerThreads": 4, 
                    "receiverStatus": "ON", 
                    "receiverThreadState": "Waiting for master to send event", 
                    "replicationLag": null
                }, 
                "status": "ONLINE"
            }, 
            "ytt-pc:4012": {
                "address": "ytt-pc:4012", 
                "instanceRole": "PRIMARY", 
                "mode": "R/W", 
                "status": "ONLINE"
            }
        }, 
        "type": "ASYNC"
    }
}
 MySQL  ytt-pc:40100+ ssl  JS > 

从副本集中移除实例 4010

  MySQL  ytt-pc:40100+ ssl  JS > c1.removeInstance("ytt@ytt-pc:4010");
The instance "ytt-pc:4010" was removed from the replicaset.

从结果看到,4010 被成功移除,接下来再把 4010 添加进去

 MySQL  ytt-pc:40100+ ssl  JS > c1.addInstance("ytt@ytt-pc:4010")
Adding instance to the replicaset...
* Updating topology
** Configuring ytt-pc:4010 to replicate from ytt-pc:4012
** Waiting for new instance to synchronize with PRIMARY...

The instance "ytt-pc:4010" was added to the replicaset and is replicating from ytt-pc:4012.

三、总结

这篇介绍了 MySQL Shell 的新特性,管理 MySQL 主从复制,可以看到比传统的方式要简单的多。

您可能感兴趣的文档:

--结束END--

本文标题: 新特性解读 | MySQL 8.0 新特性-副本集(replicaset)

本文链接: https://www.lsjlt.com/news/4572.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 新特性解读 | MySQL 8.0 新特性-副本集(replicaset)
    引言 之前,我介绍过一篇《MySQL Innodb Cluster 扫盲篇》。 MySQL Innodb Cluster = MySQL Shell + MySQL Router + MySQL Group Replication(MGR...
    99+
    2021-10-20
    新特性解读 | MySQL 8.0 新特性-副本集(replicaset)
  • 新特性解读 | mysql 8.0 memcached api 新特性
    作者:杨涛涛 资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MyS...
    99+
    2017-07-06
    新特性解读 | mysql 8.0 memcached api 新特性
  • 新特性解读 | MySQL 8.0 之原子 DDL
    作者:杨奇龙 网名“北在南方”,8 年 DBA 老兵,目前任职于杭州有赞科技 DBA,主要负责数据库架构设计和运维平台开发工作,擅长数据库性能调优、故障诊断。 本文来源:转载自公众号-yangyidba *爱可生开源社区出品,原创内容未经...
    99+
    2014-05-18
    新特性解读 | MySQL 8.0 之原子 DDL
  • MySQL 8.0 新增特性
    1. 数据字典 新增了事务型的数据字典,用来存储数据库对象信息 之前,字典数据是存储在元数据文件和非事务型表中的 2. 账号权限管理 添加了对 “角色” 的支持,角色会对应一套权限,角色可以被添加、删除...
    99+
    2022-10-18
  • 新特性解读 | InnoDB ReplicaSet:MySQL 副本集初体验
    一、InnoDB ReplicaSet 介绍 MySQL 副本集(官方名称:MySQL InnoDB ReplicaSet)在 MySQL 8.0.19 版本(2020-01-13 Released)之后开始支持,本质还是是基于 GTI...
    99+
    2020-04-27
    新特性解读 | InnoDB ReplicaSet:MySQL 副本集初体验
  • MySQL 8.0新特性--CTE(一)
    1、CTE简介MySQL从8.0开始支持CTE,慢慢地向Oracle学习,CTE确实是个很好用的东西,特别是针对OLAP类型的SQL,可以大大简化,优化SQL.那么什么是CTE呢?个人理解:CTE(comm...
    99+
    2022-10-18
  • MySQL 8.0新特性概览
    1.事务性数据字典,完全脱离了MyISAM存储引擎 真正将数据字典放到了InnoDB中的一些表中,从此不再需要FRM、TRG、PAR文件啦!Information Schema现在以数据字典表的一个视图出现...
    99+
    2022-10-18
  • MySQL 8.0新特性--CTE Recurive(二)
    上一篇介绍了CTE的基本用法,参考MySQL 8.0新特性--CTE(一),本篇再来介绍一下CTE Recurive递归。1、什么是CTE RecuriveA recursive common table ...
    99+
    2022-10-18
  • MySQL 8.0新特性有哪些
    这篇文章主要为大家展示了“MySQL 8.0新特性有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“MySQL 8.0新特性有哪些”这篇文章吧。 ...
    99+
    2022-10-18
  • MySQL 8.0有哪些新特性
    这期内容当中小编将会给大家带来有关MySQL 8.0有哪些新特性,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。MySQL被Sun收购后,搞了个过渡的6.0版本,没多久就下...
    99+
    2022-10-19
  • 新特性解读 | 从 wireshark 看 MySQL 8.0 加密连接
    作者:秦福朗 爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。热爱 IT,喜欢在互联网里畅游,擅长摄影、厨艺,不会厨艺的 DBA 不是好司机,didi~ 本文来源:原创投稿 *爱可生开源社区出品,原创内容未经授权不得随意...
    99+
    2019-10-02
    新特性解读 | wireshark MySQL 8.0 加密连接
  • 新特性解读 | 8.0 新增 DML 语句(TABLE & VALUES)
    作者:杨涛涛 资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MyS...
    99+
    2015-03-09
    新特性解读 | 8.0 新增 DML 语句(TABLE & VALUES)
  • MySQL 8.0新特性完整列表
    下面简要列出了 MySQL 8.0 中的新功能 -事务数据字典用于存储有关对象信息的事务数据字典。原子数据定义语言原子数据定义语言 (DDL) 语句,用于组合对数据字典、存储引擎操作等进行的更新。安全性增强安全级别得到了提高,DBA(数据库...
    99+
    2023-10-22
  • MySQL 8.0新增特性有哪些
    这篇文章主要讲解了“MySQL 8.0新增特性有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL 8.0新增特性有哪些”吧! ...
    99+
    2022-10-19
  • MySQL 8.0的新特性有哪些
    小编给大家分享一下MySQL 8.0的新特性有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、地理信息系统 GIS8.0 版本提供对地形的支持,其中包括了对空间参照系的数据源信息的支...
    99+
    2022-10-19
  • MySQL 8.0新特性之INTERSECT和EXCEPT
    最近几年,MySQL 不断致力于兼容 SQL 标准。例如 MySQL 8.0 中的窗口函数、通用表表达式、检查约束等等。 最新发布的 MySQL 8.0.31 继续对 SQL 语句进行了增强,提供了缺失已久的两个集合操作符:INTERSEC...
    99+
    2023-09-01
    mysql 数据库
  • MySQL 8.0新特性-不可见索引
    官方文档: https://dev.mysql.com/doc/refman/8.0/en/invisible-indexes.html MySQL 8.0 invisible index学习 MySQ...
    99+
    2022-10-18
  • MySQL 8.0新特性之集合操作符INTERSECT和EXCEPT
    目录前言交集操作符(INTERSECT)差集操作符(EXCEPT)总结前言 最近几年,MySQL 不断致力于兼容 SQL 标准。例如 MySQL 8.0 中的窗口函数、通用表表达式、...
    99+
    2022-11-13
    mysql8.0 intersect mysql8.0 except mysql8.0集合操作符
  • MySQL 8.0新特性之集合操作符INTERSECT和EXCEPT
    目录前言交集操作符(INTERSECT)差集操作符(EXCEPT)总结前言 最近几年,mysql 不断致力于兼容 SQL 标准。例如 MySQL 8.0 中的窗口函数、通用表表达式、检查约束等等。 最新发布的 MySQL...
    99+
    2022-10-24
  • MySQL 5.6, 5.7, 8.0版本的新特性汇总大全
    MySQL 5.6 1).支持GTID复制 2).支持无损复制 3).支持延迟复制 4).支持基于库级别的并行复制 5).mysqlbinlog命令支持远程备份b...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作