MySQL 官方高可用方案 InnoDB Cluster

前言

不知不觉 LTS 长期支持版本从 8.0 调整为了 8.4 版本,8.0.x 只会修复 bug 不再有新功能发布,而我们维护的数据库中 5.7 依然占大多数。8.0 版本 MySQL 官方发布了三套高可用方案,分别是 ReplicaSet、InnoDB Cluster、InnoDB ClusterSet 都提供切换和透明路由能力,用户可根据业务高可用的需求,选择对应的高可用方案。今天介绍的是 InnoDB Cluster 官方推出的一套高可用方案。

1. 方案构成

下图为 MySQL 官方提供的 InnoDB Cluster 架构图。

在这里插入图片描述

InnoDB Cluster 由三部分组成,数据节点是基于 MySQL 组复制构建,组复制可以提供成员资格管理、容错、自动故障转移等功能。

MySQL Shell 提供了很多自动化接口,集群部署和维护的大部分操作都可以实现自动化。并且支持 Python 脚本,意味着用户可以基于它提供的 AdminAPI 补充研发适合本地环境的自动化程序,实现一套程序管理多条集群。

MySQL Router 是 MySQL 官方提供的一个轻量化代理中间价,可以在服务器和 MySQL 集群之间提供连接路由,实现透明切换和读写分离。在 8.2 版本之前 Router 读/写 和 只读 分为两个端口,应用使用要使用读写分离,需要额外适配,也是很多用户不愿意使用它的原因。在 8.2 之后提供了一个额外的端口,支持透明的读写分离,这样就没理由不使用它了。

2. 使用要求和限制

3. 集群部署

3.1 环境说明

由于是本地测试,所以把 Shell 和 Router 和 Primary 节点部署在一起,如果是生产环境 Shell 没有特别要求,Router 可以单独部署到一台服务器或者与应用部署在一起。

IPhostnameRole
172.16.104.55172-16-104-55MySQL 8.4、MySQL Shell、MySQL Router
172.16.104.56172-16-104-56MySQL 8.4
172.16.104.57172-16-104-57MySQL 8.4

三个节点需要添加一下映射关系:

vi /etc/hosts
172.16.104.57 172-16-104-57
172.16.104.56 172-16-104-56
172.16.104.55 172-16-104-55

操作系统:CentOS Linux release 7.9.2009 (Core)

3.2 软件下载

MySQL Shell 下载地址:

https://downloads.mysql.com/archives/shell/

MySQL Server 下载地址:

https://dev.mysql.com/downloads/mysql/

在这里插入图片描述
下载 tar 包,里面包含 router 组件。

3.2 MySQL Server 安装

MySQL Server 安装参考之前到一篇文章,不过脚本只兼容 5.7 和 8.0 版本。

安装步骤参考:MySQL 自动化部署

程序优化了一下,如果是 8.4 版本可以使用新的脚本。

兼容 8.4 版本的程序:MySQL 8.4 自动化部署脚本下载地址

MySQL Server 安装完成后,需要创建一个 MySQL Shell 管理账号:

CREATE USER 'rw_shell'@'%' IDENTIFIED BY 'admin123';

GRANT CLONE_ADMIN, CONNECTION_ADMIN, CREATE USER, EXECUTE, FILE, GROUP_REPLICATION_ADMIN, PERSIST_RO_VARIABLES_ADMIN, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, REPLICATION_APPLIER, REPLICATION_SLAVE_ADMIN, ROLE_ADMIN, SELECT, SHUTDOWN, SYSTEM_VARIABLES_ADMIN ON *.* TO 'rw_shell'@'%' WITH GRANT OPTION;
GRANT DELETE, INSERT, UPDATE ON mysql.* TO 'rw_shell'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata.* TO 'rw_shell'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_bkp.* TO 'rw_shell'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_previous.* TO 'rw_shell'@'%' WITH GRANT OPTION;

3.3 MySQL Shell 安装

MySQL Shell 是一个自动化管理工具,可以部署在任何一个节点,一个 MySQL Shell 可以管理多套集群。

tar -zxvf mysql-shell-8.4.0-linux-glibc2.17-x86-64bit.tar.gz
mv mysql-shell-8.4.0-linux-glibc2.12-x86-64bit /usr/local/mysqlsh
export PATH=/usr/local/mysqlsh/bin/:$PATH
echo 'PATH=$PATH:/usr/local/mysqlsh/bin/' >> /etc/profile

通过 MySQL Shell 连接 57 节点,

mysqlsh --uri rw_shell@172-16-104-57 -p'admin123' --py

请添加图片描述

3.4 检测实例是否符合资格

MySQL Shell 提供的 AdminAPI 可以检测 MySQL Server 当前的配置是否符合组件集群的条件。

mysqlsh --uri rw_shell@172-16-104-57 -p'admin123' --py
dba.check_instance_configuration('rw_shell@172-16-104-55:3306')
dba.check_instance_configuration('rw_shell@172-16-104-56:3306')
dba.check_instance_configuration('rw_shell@172-16-104-57:3306')

请添加图片描述

三个 MySQL Server 都要依次检测,检测无误后执行下方命令,会自动创建一些元数据库和开启组复制相关的配置。

dba.configure_instance('rw_shell@172-16-104-55:3306')
dba.configure_instance('rw_shell@172-16-104-56:3306')
dba.configure_instance('rw_shell@172-16-104-57:3306')

请添加图片描述

3.5 创建集群

rs = dba.create_cluster('test_cluster')
A new InnoDB Cluster will be created on instance '172-16-104-57:3306'.

Validating instance configuration at 172-16-104-57:3306...

This instance reports its own address as 172-16-104-57:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using '172-16-104-57:3306'. Use the localAddress option to override.

* Checking connectivity and SSL configuration...

Creating InnoDB Cluster 'test_cluster' on '172-16-104-57:3306'...

Adding Seed Instance...
Cluster successfully created. Use Cluster.add_instance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.

<Cluster:test_cluster>
rs.status()

{
    "clusterName": "test_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "172-16-104-57:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures.", 
        "topology": {
            "172-16-104-57:3306": {
                "address": "172-16-104-57:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "172-16-104-57:3306"
}

3.6 向集群中添加节点

上一步连接的是 57 创建集群,所以默认主节点是 57 现在需要把另外两个 replica 节点也加入到集群中。

rs.add_instance('rw_shell@172-16-104-55:3306')
rs.add_instance('rw_shell@172-16-104-56:3306')

加入完成后,执行下方命令查询集群状态。

 rs.status()
{
    "clusterName": "test_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "172-16-104-57:3306", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "172-16-104-55:3306": {
                "address": "172-16-104-55:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }, 
            "172-16-104-56:3306": {
                "address": "172-16-104-56:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }, 
            "172-16-104-57:3306": {
                "address": "172-16-104-57:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": "applier_queue_applied", 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.4.0"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "172-16-104-57:3306"
}

3.7 配置 Router

官方下载地址:MySQL Router Donwload

xz -d mysql-router-8.4.0-linux-glibc2.17-x86_64.tar.xz
tar -xvf mysql-router-8.4.0-linux-glibc2.17-x86_64.tar

解压后就可以使用,现在需要创建一个 router 引导使用的账号,用户状态检测。

rs.setup_router_account('op_router')

创建完成后,进入的 bin 目录,引导 router 自动配置。

./mysqlrouter \
   --bootstrap rw_shell@172-16-104-57:3306 \
   --directory /data/myrouter \
   --conf-use-sockets \
   --account op_router \
   --user=mysql

bootstrap:引导实例的连接串 router 会通过该实例获取集群元数据信息。
directory:mysql-router 数据目录,需要创建好,并设置 mysql 属组。
conf-use-sockets:确认生成 socket 文件。
account:router 连接 MySQL 的账号,上一步已使用 mysql shell 已创建。

- 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 /data/myrouter/mysqlrouter.conf

# MySQL Router configured for the InnoDB Cluster 'test_cluster'

After this MySQL Router has been started with the generated configuration

    $ ./mysqlrouter -c /data/myrouter/mysqlrouter.conf

InnoDB Cluster 'test_cluster' can be reached by connecting to:

## MySQL Classic protocol

- Read/Write Connections: localhost:6446, /data/myrouter/mysql.sock
- Read/Only Connections:  localhost:6447, /data/myrouter/mysqlro.sock
- Read/Write Split Connections: localhost:6450, /data/myrouter/mysqlsplit.sock

## MySQL X protocol

- Read/Write Connections: localhost:6448, /data/myrouter/mysqlx.sock
- Read/Only Connections:  localhost:6449, /data/myrouter/mysqlxro.sock

MySQL Router 会自动生成配置文件,启动命令也打印在终端中。

./mysqlrouter -c /data/myrouter/mysqlrouter.conf &

3.8 测试验证

使用 root 账号连接主节点,创建测试库和账号。

create database test;
create user rw_test@'%' identified by 'admin123';
grant ALL on test.* to rw_test@'%'

使用读写分离代理连接数据库:

mysql -h172.16.104.55 -urw_test -padmin123 -P6450

写入测试数据,然后可以再连接 replica 节点查看是否同步完成。

use test;
create table rep_test(id int primary key, a varchar(10));
insert into rep_test values(1, 'a'),(2, 'b'),(3, 'c');

mysql shell 中可以通过下方命令,查看是否配置 router。

rs.list_routers()
{
    "clusterName": "test_cluster", 
    "routers": {
        "172-16-104-55::": {
            "hostname": "172-16-104-55", 
            "lastCheckIn": "2024-08-14 16:27:55", 
            "roPort": "6447", 
            "roXPort": "6449", 
            "rwPort": "6446", 
            "rwSplitPort": "6450", 
            "rwXPort": "6448", 
            "version": "8.4.0"
        }
    }
}

4. 集群运维

4.1 Router 服务管理

通过服务的方式管理 mysqlrouter 下面介绍配置方法:

vi /etc/systemd/system/mysqlrouter.service

写入到配置中:

[Unit]
Description=mysqlrouter
After=network.target
 
[Service]
User=mysql
Group=mysql
Type=sample
ExecStart=/opt/mysqlrouter/bin/mysqlrouter -c /data/myrouter/mysqlrouter.conf
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

加载配置:

systemctl daemon-reload

常用命令:

# 查看 router 状态
systemctl status mysqlrouter

# 关闭 router
systemctl stop mysqlrouter

# 启动 router
systemctl start mysqlrouter

开机自启:

# 关闭开机自启
systemctl disable mysqlrouter.service
# 查看确认
systemctl list-unit-files|grep mysqlrouter

4.2 MySQL 服务管理

MySQL 服务管理配置,可以参考之前的文章:使用 systemctl 管理 MySQL 服务

4.3 集群配置调整

MySQL Shell 可以修改组复制相关的配置,支持单独修改或者全局修改,可以通过下方命令查看可修改的参数。

rs = dba.get_cluster()
rs.options()

输出有两部分,全局配置和拓扑配置,都可以对应到 MySQL 底层到参数,比如 autoRejoinTries 是节点意外断开后重试的配置,在 MySQL Server 中的参数是 group_replication_autorejoin_tries 在 MySQL Shell 集群中使用 autoRejoinTries 来配置。

{ 
	"option" : "autoRejoinTries", 
	"value" : "3", 
	"variable" : "group_replication_autorejoin_tries" 
}

组复制相关的配置调整,就不在此介绍了,大家可以使用 options() 方法列出来查看。可以修改选主权重、集群离开后加入集群的配置、集群一致性的级别等。

下面是一个修改示例,将 55 节点的选主权重调整为 80 分:

rs.set_instance_option("172-16-104-55:3306", "memberWeight", 80)

4.4 添加节点

MySQL Shell 中可以使用 add_instance() 方法添加一个新节点。使用的格式如下:

-- 待定
rs.add_instance('rw_shell@172-16-104-56:3306', label='node1', recoveryMethod = 'auto', password = 'xxxxx', memberWeight = 80)

下面介绍一下,方法内常用参数的作用:

  • label:该节点的标签,就是为该节点起一个名字。

  • recoveryMethod:加入集群时,如果主节点有数据,那么需要给该节点传输数据。

    • incremental:基于 binlog 的增量恢复。
    • clone:基于 clone 插件的全量恢复。
    • auto:基于目标实例是否支持 clone 插件,以及 group_replication_clone_threshold 的参数设置。
  • password:该节点的密码。

  • memberWeight:选举过程中,该节点的权重,默认为 50。

4.5 添加只读实例

MySQL Shell 可以使用 add_replica_instance() 方法添加一个只读副本。该副本可以添加到集群中任何一个成员上面进行复制,如果该成员异常退出集群,那么会自动切换到存活的节点上继续接收复制。

该功能可以用于在线业务和离线业务分离的场景,在线业务对高可用和性能要求很高 SQL 一般都是 短、平、快 的类型,离线业务 SQL 一般都非常复杂,所以两个业务不能混合使用,否则会影响在线业务的吞吐量。如果把离线业务加入到 InnoDB Cluster 集群中,增加代理配置的复杂度,干脆挂在一个只读节点,只需要能持续复制即可,不参与集群的选主切换以及读写分离。

现在测试将 172-16-121-152 新的 MySQL 节点,作为只读实例挂到集群中。

首先,需要创建用户,可以参考 MySQL Server 部署那小节。

# 进入 MySQL Shell
mysqlsh --uri rw_shell@172-16-104-55 -p'admin123' --py

检测该节点是否符合要求:

dba.check_instance_configuration('rw_shell@172-16-121-152:3306')
dba.configure_instance('rw_shell@172-16-121-152:3306')

执行完成后,可以使用下方命令,将 152 作为只读节点加入到集群中:

rs.add_replica_instance('rw_shell@172-16-121-152:3306', {'label': 'RReplica1', 'replicationSources': ['172-16-104-56:3306']})

这里指定 replicationSources 选项,表示从哪个实例获取 clone 全量数据以及建立复制通道,这里选择的是一个 SECONDARY 节点。执行成功后,可以使用下方命令查看:

{
    "clusterName": "test_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "topology": [
            {
                "address": "172-16-104-57:3306", 
                "label": "172-16-104-57:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-104-55:3306", 
                "label": "172-16-104-55:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-104-56:3306", 
                "label": "172-16-104-56:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-121-152:3306", 
                "label": "RReplica1", 
                "replicationSources": [
                    "172-16-104-56:3306"
                ], 
                "role": "READ_REPLICA"
            }
        ], 
        "topologyMode": "Single-Primary"
    }
}

只读实例,默认是不会加入到 router 的读写分离配置中的,如果有离线业务,可以直连只读实例。

如果只读实例连接的节点出现异常,会自动连接到健康的节点,新节点恢复后,只读实例不会自动再连接到老节点,如果想要把它重新挂在老节点上面,可以使用下面的命令。

rs.set_instance_option("172-16-121-152:3306", 'replicationSources', ["172-16-104-56:3306"])

如果需要移除只读节点,可以使用如下方法:

rs.remove_instance("172-16-121-152:3306")

4.6 主动切换

手动切换主实例,可以使用 set_primary_instance 方法切换。

cluster.set_primary_instance('172-16-104-57:3306')
Setting instance '172-16-104-57:3306' as the primary instance of cluster 'pre_cluster'...

Instance '172-16-104-56:3306' remains SECONDARY.
Instance '172-16-104-55:3306' was switched from PRIMARY to SECONDARY.
Instance '172-16-104-57:3306' was switched from SECONDARY to PRIMARY.

The instance '172-16-104-57:3306' was successfully elected as primary.

5. dba 对象方法介绍

通过 dba.get_cluster() 方法获取操作对象后,可以看到支持很多自动化的方法,这里会介绍使用方法和场景。
请添加图片描述
需要注意的是 MySQL Shell 可以管理多套集群,所以在使用 get_cluster(‘集群名称’) 可以指定集群名称。

5.1 add_instance()

向集群中添加新的实例,这里给出一个示例,详细可参考 4.4 小节。

-- 待定
rs.add_instance('rw_shell@172-16-104-56:3306', label='node1', recoveryMethod = 'auto', password = 'xxxxx', memberWeight = 80)

5.2 add_replica_instance()

添加只读实例,该方法已在 4.5 小节中介绍,只读实例不参与集群的切换和选举,适用于离线业务。

rs.add_replica_instance('rw_shell@172-16-121-152:3306', {'label': 'RReplica1', 'replicationSources': ['172-16-104-56:3306']})

5.3 create_cluster_set()

该方法用于部署 ClusterSet 架构,将在后面的文章中介绍。

5.4 describe()

属于集群的监控方法,列出当前集群的拓扑信息。

{
    "clusterName": "test_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "topology": [
            {
                "address": "172-16-104-57:3306", 
                "label": "172-16-104-57:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-104-55:3306", 
                "label": "172-16-104-55:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-104-56:3306", 
                "label": "172-16-104-56:3306", 
                "role": "HA"
            }, 
            {
                "address": "172-16-121-152:3306", 
                "label": "RReplica1", 
                "replicationSources": [
                    "172-16-104-56:3306"
                ], 
                "role": "READ_REPLICA"
            }
        ], 
        "topologyMode": "Single-Primary"
    }
}

5.5 disconnect()

断开 cluster 对象与组复制节点之间的连接,在 Python 模式下,相当于销毁了操作对象,适用于管理多套集群的场景。

-- 创建对象
cluster = dba.get_cluster()
-- 销毁对象
cluster.disconnect()

5.6 dissolve()

该方法用于解散集群,需要连接到主实例,执行该命令后将 InnoDB Cluster 的元数据配置删除,实例中的用户数据不会删除。

cluster.dissolve() 

5.7 fence_all_traffic()

将集群与所有流量隔离,关闭所有节点的复制并禁止写入,属于优雅的关闭组复制的操作。

cluster.fence_all_traffic()
The Cluster 'test_cluster' will be fenced from all traffic

* Enabling super_read_only on the primary '172-16-104-55:3306'...
* Enabling offline_mode on the primary '172-16-104-55:3306'...
* Enabling offline_mode on '172-16-104-57:3306'...
* Stopping Group Replication on '172-16-104-57:3306'...
* Enabling offline_mode on '172-16-104-56:3306'...
* Stopping Group Replication on '172-16-104-56:3306'...
* Enabling offline_mode on '172-16-121-152:3306'...
* Stopping replication on '172-16-121-152:3306'...
* Stopping Group Replication on the primary '172-16-104-55:3306'...

Cluster successfully fenced from all traffic

恢复集群使用下面的方法:

dba.reboot_cluster_from_complete_outage('test_cluster')

5.8 get_name()

该方法用于获取集群的名称。

cluster.get_name()

5.9 list_routers()

列出 Router 代理实例信息。

cluster.list_routers() 
{
    "clusterName": "test_cluster", 
    "routers": {
        "172-16-104-55::": {
            "hostname": "172-16-104-55", 
            "lastCheckIn": "2024-08-28 15:34:14", 
            "roPort": "6447", 
            "roXPort": "6449", 
            "rwPort": "6446", 
            "rwSplitPort": "6450", 
            "rwXPort": "6448", 
            "version": "8.4.0"
        }
    }
}

5.10 options()

该方法用于列出集群的配置信息。

cluster.options()

5.11 remove_instance()

从集群中移除一个节点,也可用于移除复制节点。

cluster.remove_instance('172-16-104-55:3306')

5.12 rejoin_instance()

用于将一个集群中已有的实例,状态异常的实例,重新加入到集群。

cluster.rejoin_instance('172-16-104-55:3306')

5.13 remove_router_metadata()

Router 代理集机器如果要迁移,可以使用该命令清理到元数据,重新引导 Router 即可。

cluster.remove_router_metadata("172-16-104-55::")

5.14 rescan()

用户如果没有通过 MySQL Shell 修改或者调整集群的复制通道或者参数等,就会出现集群元数据与实际不匹配的问题,此时可以使用该方法,重新扫描集群收集最新的元数据信息。

cluster.rescan()

5.15 reset_recovery_accounts_password()

MySQL Shell 在建立复制关系的时候,会自动创建复制通道的账号,密码用户是不知道的。基于安全合规要求,账号可能需要定期修改,那么可以执行如下命令,会自动修改密码并重启复制:

cluster.reset_recovery_accounts_password()
The recovery and replication account passwords of all the cluster instances' were successfully reset.

5.16 router_options()

列出 Router 代理的配置信息,其中 extended 表示详细等级 0~2,选项 router 是查看指定代理时使用。

cluster.router_options({'extended':0, 'router':'172-16-104-55::'})

5.17 set_instance_option()

用户修改配置时使用,使用的格式如下:

cluster.set_instance_option("172-16-104-55:3306", "memberWeight", 80)

5.18 set_option()

修改集群级别的参数,比如修改集群的名称:

cluster.set_option('clusterName', 'pre_cluster')
Setting the value of 'clusterName' to 'pre_cluster' in the Cluster ...

Successfully set the value of 'clusterName' to 'pre_cluster' in the Cluster: 'test_cluster'.

5.19 set_primary_instance()

手动切换主实例,可以使用

cluster.set_primary_instance('172-16-104-57:3306')
Setting instance '172-16-104-57:3306' as the primary instance of cluster 'pre_cluster'...

Instance '172-16-104-56:3306' remains SECONDARY.
Instance '172-16-104-55:3306' was switched from PRIMARY to SECONDARY.
Instance '172-16-104-57:3306' was switched from SECONDARY to PRIMARY.

The instance '172-16-104-57:3306' was successfully elected as primary.

5.20 set_routing_option()

5.21 setup_admin_account()

创建或者修改 InnoDB Cluster 的账号:

cluster.setup_admin_account('admin')

5.22 setup_router_account()

创建或者修改 router 监控和引导用户的账号:

cluster.setup_router_account('op_router', {'update':1})
Updating user op_router@%.
Updating .
Account op_router@% was successfully updated.

5.23 status()

查看集群状态,参数 extended 表示输出的详细等级,取值范围是 0~3:

cluster.status({'extended':0})

5.24 switch_to_multi_primary_mode()

将集群从单主模式调整为多主模式。

cluster.switch_to_multi_primary_mode()
Switching cluster 'pre_cluster' to Multi-Primary mode...

Instance '172-16-104-57:3306' remains PRIMARY.
Instance '172-16-104-55:3306' was switched from SECONDARY to PRIMARY.
Instance '172-16-104-56:3306' was switched from SECONDARY to PRIMARY.

The cluster successfully switched to Multi-Primary mode.

5.25 switch_to_multi_primary_mode()

将集群由多主模式调整为单主模式。

cluster.switch_to_single_primary_mode()
Switching cluster 'pre_cluster' to Single-Primary mode...

Instance '172-16-104-55:3306' remains PRIMARY.
Instance '172-16-104-57:3306' was switched from PRIMARY to SECONDARY.
Instance '172-16-104-56:3306' was switched from PRIMARY to SECONDARY.

WARNING: Existing connections that expected a R/W connection must be disconnected, i.e. instances that became SECONDARY.

The cluster successfully switched to Single-Primary mode.

总结

本文介绍 MySQL InnoDB Cluster 部署和维护的方法,基于 MySQL Shell 提供的 Admin API 大大简化运维人员部署和维护集群,并且兼容 Python 语法,个人觉得可玩性还是非常高的,后面如果有亮点功能还会单独介绍。

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值