基于mysqlsh部署mysql8.0的MGR

基于mysqlsh部署mysql8.0的MGR

1、系统配置

1.1、设置/etc/hosts

cat >>/etc/hosts<<"EOF"
################################################
10.100.19.214 k8s-02-19-214-bigdata-test
10.100.19.215 k8s-03-19-215-bigdata-test
10.100.19.216 k8s-04-19-216-bigdata-test
10.100.19.212 k8s-01-19-212-bigdata-test

################################################
EOF

cat /etc/hosts
/etc/hosts配置

1.2、关闭防火墙

# 关闭防火墙
systemctl disable --now firewalld
systemctl disable --now dnsmasq
systemctl disable --now NetworkManager
systemctl disable --now iptables

setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

systemctl stop firewalld.service
systemctl stop iptables.service
echo "systemctl stop iptables.service" >>/etc/rc.local
echo "systemctl stop firewalld.service" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
关闭防火墙

1.3、关闭SeLinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce  0

1.4、配置yum源

mv /etc/yum.repos.d/CentOS-Base.repo{,.orig}
#阿里云
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#腾讯云
#wget -O /etc/yum.repos.d/CentOS-Base.repo #http://mirrors.cloud.tencent.com/repo/centos7_base.repo
#替换完后,更新缓存
yum clean all
yum makecache

1.5、安装系统依赖包

yum -y install make gcc-c++ cmake bison-devel ncurses-devel  readline-devel  libaio-devel perl libaio wget lrzsz vim libnuma* 
yum -y install gcc glibc glibc-devel pcre pcre-devel openssl  openssl-devel systemd-devel net-tools  iotop bc iftop zip 
yum -y install unzip zlib-devel  screen lsof tcpdump  epel-release telnet ntpdate yum-utils python-pip iostat bzip2 xz tree

2、下载mysql-shell

# 下载
cd /opt/
wget https://cdn.mysql.com//Downloads/MySQL-Shell/mysql-shell-8.0.24-linux-glibc2.12-x86-64bit.tar.gz
tar -zxvf mysql-shell-8.0.24-linux-glibc2.12-x86-64bit.tar.gz

for i in node-2 node-3; do scp mysql-shell-8.0.24-linux-glibc2.12-x86-64bit.tar.gz $i:/opt/; done


# 配置
unlink /usr/bin/mysql-secret-store-login-path
unlink /usr/bin/mysqlsh
ln -s /opt/mysql-shell-8.0.24-linux-glibc2.12-x86-64bit/bin/* /usr/bin/

3、准备账号

set  global  general_log=0;

-- 提前准备好账号
set sql_log_bin=0;
create user 'mgradmin'@'127.0.0.1' identified by '1SIrjg41xBfxe1yxuLVf';
create user 'mgradmin'@'10.100.19.214' identified by '1SIrjg41xBfxe1yxuLVf';
create user 'mgradmin'@'10.100.19.215' identified by '1SIrjg41xBfxe1yxuLVf'; 
create user 'mgradmin'@'10.100.19.216' identified by '1SIrjg41xBfxe1yxuLVf';
create user 'mgradmin'@'10.100.19.212' identified by '1SIrjg41xBfxe1yxuLVf';
create user 'mgradmin'@'10.100.19.250' identified by '1SIrjg41xBfxe1yxuLVf'; 

grant all privileges on *.* to  'mgradmin'@'127.0.0.1' WITH GRANT OPTION;
grant all privileges on *.* to  'mgradmin'@'10.100.19.214' WITH GRANT OPTION;
grant all privileges on *.* to  'mgradmin'@'10.100.19.215' WITH GRANT OPTION;
grant all privileges on *.* to  'mgradmin'@'10.100.19.216' WITH GRANT OPTION;
grant all privileges on *.* to  'mgradmin'@'10.100.19.212' WITH GRANT OPTION;
grant all privileges on *.* to  'mgradmin'@'10.100.19.250' WITH GRANT OPTION;
set sql_log_bin=1;

4、登录mysqlsh

# 1SIrjg41xBfxe1yxuLVf
mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
mysqlsh 'mgradmin'@'10.100.19.215':3306 --js --quiet-start=2 -C required
mysqlsh 'mgradmin'@'10.100.19.216':3306 --js --quiet-start=2 -C required
mysqlsh 'mgradmin'@'10.100.19.212':3306 --js --quiet-start=2 -C required
mysqlsh 'mgradmin'@'10.100.19.250':3306 --js --quiet-start=2 -C required
mysqlsh 'mgradmin'@'127.0.0.1':3306     --js --quiet-start=2 -C required

5、配置vip

-- 配置 vip
# 添加vip
/sbin/ip addr add 10.100.19.250/255.255.255.0 dev eth0
# 删除vip
#/sbin/ip addr del 10.100.19.250/255.255.255.0 dev eth0

cat >>/etc/rc.local<<"EOF"
# 添加vip
#/sbin/ip addr add 10.100.19.250/255.255.255.0 dev eth0
# 删除vip
#/sbin/ip addr del 10.100.19.250/255.255.255.0 dev eth0
EOF

6、预检查

# 预检查
mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
dba.checkInstanceConfiguration('mgradmin@10.100.19.214:3306')

# mysqlsh 'mgradmin'@'10.100.19.215':3306 --js --quiet-start=2 -C required
dba.checkInstanceConfiguration('mgradmin@10.100.19.215:3306')

# mysqlsh 'mgradmin'@'10.100.19.216':3306 --js --quiet-start=2 -C required
dba.checkInstanceConfiguration('mgradmin@10.100.19.216:3306')

# mysqlsh 'mgradmin'@'10.100.19.212':3306 --js --quiet-start=2 -C required --password='1SIrjg41xBfxe1yxuLVf'
dba.checkInstanceConfiguration('mgradmin@10.100.19.212:3306')

6.1、10.100.19.214预检查结果

 MySQL  10.100.19.214:3306 ssl  JS > dba.checkInstanceConfiguration('mgradmin@10.100.19.214:3306')
Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-02-19-214-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'k8s-02-19-214-bigdata-test:3306' is valid to be used in an InnoDB cluster.

{
    "status": "ok"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.214

6.2、10.100.19.215预检查结果

MySQL  10.100.19.214:3306 ssl  JS > dba.checkInstanceConfiguration('mgradmin@10.100.19.215:3306')
Validating MySQL instance at k8s-03-19-215-bigdata-test:3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-03-19-215-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'k8s-03-19-215-bigdata-test:3306' is valid to be used in an InnoDB cluster.

{
    "status": "ok"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.215预检查结果

6.3、10.100.19.216预检查结果

 MySQL  10.100.19.214:3306 ssl  JS > dba.checkInstanceConfiguration('mgradmin@10.100.19.216:3306')
Validating MySQL instance at k8s-04-19-216-bigdata-test:3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-04-19-216-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'k8s-04-19-216-bigdata-test:3306' is valid to be used in an InnoDB cluster.

{
    "status": "ok"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.216预检查结果

7、再次确认结果

# 执行dba.configureInstance()后再次检查实例配置的输出如下:1SIrjg41xBfxe1yxuLVf
dba.configureInstance('mgradmin@10.100.19.214:3306')
dba.configureInstance('mgradmin@10.100.19.215:3306')
dba.configureInstance('mgradmin@10.100.19.216:3306')
dba.configureInstance('mgradmin@10.100.19.212:3306')

7.1、10.100.19.214配置检查结果

MySQL  10.100.19.214:3306 ssl  JS > dba.configureInstance('mgradmin@10.100.19.214:3306')
Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-02-19-214-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

applierWorkerThreads will be set to the default value of 4.

The instance 'k8s-02-19-214-bigdata-test:3306' is valid to be used in an InnoDB cluster.
The instance 'k8s-02-19-214-bigdata-test:3306' is already ready to be used in an InnoDB cluster.

Successfully enabled parallel appliers.
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.214配置检查结果

7.2、10.100.19.215配置检查结果

 MySQL  10.100.19.214:3306 ssl  JS > dba.configureInstance('mgradmin@10.100.19.215:3306')
Configuring MySQL instance at k8s-03-19-215-bigdata-test:3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-03-19-215-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

applierWorkerThreads will be set to the default value of 4.

The instance 'k8s-03-19-215-bigdata-test:3306' is valid to be used in an InnoDB cluster.
The instance 'k8s-03-19-215-bigdata-test:3306' is already ready to be used in an InnoDB cluster.

Successfully enabled parallel appliers.
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.215配置检查结果

7.3、10.100.19.216配置检查结果

 MySQL  10.100.19.214:3306 ssl  JS > dba.configureInstance('mgradmin@10.100.19.216:3306')
Configuring MySQL instance at k8s-04-19-216-bigdata-test:3306 for use in an InnoDB cluster...

This instance reports its own address as k8s-04-19-216-bigdata-test:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

applierWorkerThreads will be set to the default value of 4.

The instance 'k8s-04-19-216-bigdata-test:3306' is valid to be used in an InnoDB cluster.
The instance 'k8s-04-19-216-bigdata-test:3306' is already ready to be used in an InnoDB cluster.

Successfully enabled parallel appliers.
 MySQL  10.100.19.214:3306 ssl  JS > 
10.100.19.216配置检查结果

8、创建MGR

# 创建MGR集群:1SIrjg41xBfxe1yxuLVf
# 连接主节点(my.cnf中不要设置过滤同步)
mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
dba.createCluster('clsmgr')
var cluster = dba.getCluster()

# 添加节点
cluster.addInstance('mgradmin@10.100.19.215:3306')
cluster.addInstance('mgradmin@10.100.19.216:3306')
cluster.addInstance('mgradmin@10.100.19.212:3306')

 8.1、创建MGR细节

MySQL  10.100.19.214:3306 ssl  JS > dba.createCluster('clsmgr')
A new InnoDB cluster will be created on instance '10.100.19.214:3306'.

Validating instance configuration at 10.100.19.214:3306...

This instance reports its own address as k8s-02-19-214-bigdata-test:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'k8s-02-19-214-bigdata-test:33061'. Use the localAddress option to override.

Creating InnoDB cluster 'clsmgr' on 'k8s-02-19-214-bigdata-test:3306'...

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

<Cluster:clsmgr>
 MySQL  10.100.19.214:3306 ssl  JS > 
 MySQL  10.100.19.214:3306 ssl  JS > var cluster = dba.getCluster()
创建MGR细节

8.2、添加节点

8.2.1、添加10.100.19.215

 MySQL  10.100.19.214:3306 ssl  JS > cluster.addInstance('mgradmin@10.100.19.215:3306')

WARNING: A GTID set check of the MySQL instance at 'k8s-03-19-215-bigdata-test:3306' determined that it contains transactions that do not originate from the cluster, which must be discarded before it can join the cluster.

k8s-03-19-215-bigdata-test:3306 has the following errant GTIDs that do not exist in the cluster:
5df0cfa5-af03-11eb-a20f-fa163ee8385c:1-25

WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of k8s-03-19-215-bigdata-test:3306 with a physical snapshot from an existing cluster 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
Validating instance configuration at 10.100.19.215:3306...

This instance reports its own address as k8s-03-19-215-bigdata-test:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'k8s-03-19-215-bigdata-test:33061'. Use the localAddress option to override.

A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster...

Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: k8s-03-19-215-bigdata-test:3306 is being cloned from k8s-02-19-214-bigdata-test:3306
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: k8s-03-19-215-bigdata-test:3306 is shutting down...

* Waiting for server restart... ready
* k8s-03-19-215-bigdata-test:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 1.13 GB transferred in 13 sec (87.26 MB/s)

State recovery already finished for 'k8s-03-19-215-bigdata-test:3306'

The instance 'k8s-03-19-215-bigdata-test:3306' was successfully added to the cluster.

 MySQL  10.100.19.214:3306 ssl  JS > 
添加10.100.19.215

8.2.2、添加10.100.19.216

 MySQL  10.100.19.214:3306 ssl  JS > cluster.addInstance('mgradmin@10.100.19.216:3306')

WARNING: A GTID set check of the MySQL instance at 'k8s-04-19-216-bigdata-test:3306' determined that it contains transactions that do not originate from the cluster, which must be discarded before it can join the cluster.

k8s-04-19-216-bigdata-test:3306 has the following errant GTIDs that do not exist in the cluster:
77c33faa-af03-11eb-b24e-fa163e244eb1:1-25

WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of k8s-04-19-216-bigdata-test:3306 with a physical snapshot from an existing cluster 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
Validating instance configuration at 10.100.19.216:3306...

This instance reports its own address as k8s-04-19-216-bigdata-test:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'k8s-04-19-216-bigdata-test:33061'. Use the localAddress option to override.

A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster...

Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: k8s-04-19-216-bigdata-test:3306 is being cloned from k8s-02-19-214-bigdata-test:3306
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: k8s-04-19-216-bigdata-test:3306 is shutting down...

* Waiting for server restart... ready
* k8s-04-19-216-bigdata-test:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 1.13 GB transferred in 13 sec (87.29 MB/s)

State recovery already finished for 'k8s-04-19-216-bigdata-test:3306'

The instance 'k8s-04-19-216-bigdata-test:3306' was successfully added to the cluster.

 MySQL  10.100.19.214:3306 ssl  JS > 
添加10.100.19.216

8.2.3、添加10.100.19.212

MySQL  10.100.19.214:3306 ssl  JS > cluster.addInstance('mgradmin@10.100.19.212:3306')

WARNING: A GTID set check of the MySQL instance at 'k8s-01-19-212-bigdata-test:3306' determined that it contains transactions that do not originate from the cluster, which must be discarded before it can join the cluster.

k8s-01-19-212-bigdata-test:3306 has the following errant GTIDs that do not exist in the cluster:
7bbc9352-af02-11eb-a74f-fa163e213bcc:1-25

WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of k8s-01-19-212-bigdata-test:3306 with a physical snapshot from an existing cluster 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
Validating instance configuration at 10.100.19.212:3306...

This instance reports its own address as k8s-01-19-212-bigdata-test:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'k8s-01-19-212-bigdata-test:33061'. Use the localAddress option to override.

A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster...

Monitoring recovery process of the new cluster member. Press ^C to stop monitoring and let it continue in background.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: k8s-01-19-212-bigdata-test:3306 is being cloned from k8s-04-19-216-bigdata-test:3306
** Stage DROP DATA: Completed
** Clone Transfer  
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  Completed
    REDO COPY  ############################################################  100%  Completed

NOTE: k8s-01-19-212-bigdata-test:3306 is shutting down...

* Waiting for server restart... ready
* k8s-01-19-212-bigdata-test:3306 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 1.13 GB transferred in 55 sec (20.63 MB/s)

Incremental state recovery is now in progress.

* Waiting for distributed recovery to finish...
NOTE: 'k8s-01-19-212-bigdata-test:3306' is being recovered from 'k8s-04-19-216-bigdata-test:3306'
* Distributed recovery has finished

The instance 'k8s-01-19-212-bigdata-test:3306' was successfully added to the cluster.

 MySQL  10.100.19.214:3306 ssl  JS > 
添加10.100.19.212

9、查看状态

 MySQL  10.100.19.214:3306 ssl  JS > cluster.status()
{
    "clusterName": "clsmgr", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "k8s-02-19-214-bigdata-test:3306", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "k8s-01-19-212-bigdata-test:3306": {
                "address": "k8s-01-19-212-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-02-19-214-bigdata-test:3306": {
                "address": "k8s-02-19-214-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-03-19-215-bigdata-test:3306": {
                "address": "k8s-03-19-215-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-04-19-216-bigdata-test:3306": {
                "address": "k8s-04-19-216-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "k8s-02-19-214-bigdata-test:3306"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
查看状态

10、查看成员角色

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+----------------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST                | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+----------------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 5df0cfa5-af03-11eb-a20f-fa163ee8385c | k8s-03-19-215-bigdata-test |        3306 | ONLINE       | SECONDARY   | 8.0.24         |
| group_replication_applier | 77c33faa-af03-11eb-b24e-fa163e244eb1 | k8s-04-19-216-bigdata-test |        3306 | ONLINE       | SECONDARY   | 8.0.24         |
| group_replication_applier | 7bbc9352-af02-11eb-a74f-fa163e213bcc | k8s-01-19-212-bigdata-test |        3306 | ONLINE       | SECONDARY   | 8.0.24         |
| group_replication_applier | c1a0a1c1-af03-11eb-b1ed-fa163ea9cdf0 | k8s-02-19-214-bigdata-test |        3306 | ONLINE       | PRIMARY     | 8.0.24         |
+---------------------------+--------------------------------------+----------------------------+-------------+--------------+-------------+----------------+
4 rows in set (0.00 sec)

mysql> 

 

11、切换方式一:单主切换多主

mysql> SELECT * FROM performance_schema.replication_group_members;
mysql> select group_replication_switch_to_multi_primary_mode();
mysql> SELECT * FROM performance_schema.replication_group_members;

 

 

 

12、切换方式一:多主切换单主

SELECT * FROM performance_schema.replication_group_members;
select group_replication_switch_to_single_primary_mode();
SELECT * FROM performance_schema.replication_group_members;

 

 

 13、切换方式二:单主切换多主

mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
var cluster = dba.getCluster()
cluster.status()
cluster.switchToMultiPrimaryMode()
# mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
 MySQL  10.100.19.214:3306 ssl  JS > var cluster = dba.getCluster()
 MySQL  10.100.19.214:3306 ssl  JS > cluster.status()
{
    "clusterName": "clsmgr", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "k8s-03-19-215-bigdata-test:3306", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "k8s-01-19-212-bigdata-test:3306": {
                "address": "k8s-01-19-212-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-02-19-214-bigdata-test:3306": {
                "address": "k8s-02-19-214-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-03-19-215-bigdata-test:3306": {
                "address": "k8s-03-19-215-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-04-19-216-bigdata-test:3306": {
                "address": "k8s-04-19-216-bigdata-test:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "k8s-03-19-215-bigdata-test:3306"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
 MySQL  10.100.19.214:3306 ssl  JS > cluster.switchToMultiPrimaryMode()
Switching cluster 'clsmgr' to Multi-Primary mode...

Instance 'k8s-03-19-215-bigdata-test:3306' remains PRIMARY.
Instance 'k8s-04-19-216-bigdata-test:3306' was switched from SECONDARY to PRIMARY.
Instance 'k8s-01-19-212-bigdata-test:3306' was switched from SECONDARY to PRIMARY.
Instance 'k8s-02-19-214-bigdata-test:3306' was switched from SECONDARY to PRIMARY.

The cluster successfully switched to Multi-Primary mode.
 MySQL  10.100.19.214:3306 ssl  JS > 
 MySQL  10.100.19.214:3306 ssl  JS > cluster.status()
{
    "clusterName": "clsmgr", 
    "defaultReplicaSet": {
        "name": "default", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "k8s-01-19-212-bigdata-test:3306": {
                "address": "k8s-01-19-212-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-02-19-214-bigdata-test:3306": {
                "address": "k8s-02-19-214-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-03-19-215-bigdata-test:3306": {
                "address": "k8s-03-19-215-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-04-19-216-bigdata-test:3306": {
                "address": "k8s-04-19-216-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }
        }, 
        "topologyMode": "Multi-Primary"
    }, 
    "groupInformationSourceMember": "k8s-03-19-215-bigdata-test:3306"
}
 MySQL  10.100.19.214:3306 ssl  JS > 
切换细节

14、切换方式二:多主切换单主

mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
var cluster = dba.getCluster()
cluster.status()
cluster.switchToSinglePrimaryMode();
# mysqlsh 'mgradmin'@'10.100.19.214':3306 --js --quiet-start=2 -C required
 MySQL  10.100.19.214:3306 ssl  JS > var cluster = dba.getCluster()
 MySQL  10.100.19.214:3306 ssl  JS > cluster.status()
{
    "clusterName": "clsmgr", 
    "defaultReplicaSet": {
        "name": "default", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "k8s-01-19-212-bigdata-test:3306": {
                "address": "k8s-01-19-212-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-02-19-214-bigdata-test:3306": {
                "address": "k8s-02-19-214-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-03-19-215-bigdata-test:3306": {
                "address": "k8s-03-19-215-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }, 
            "k8s-04-19-216-bigdata-test:3306": {
                "address": "k8s-04-19-216-bigdata-test:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.24"
            }
        }, 
        "topologyMode": "Multi-Primary"
    }, 
    "groupInformationSourceMember": "k8s-02-19-214-bigdata-test:3306"
}
 MySQL  10.100.19.214:3306 ssl  JS > cluster.switchToSinglePrimaryMode();
Switching cluster 'clsmgr' to Single-Primary mode...

Instance 'k8s-03-19-215-bigdata-test:3306' remains PRIMARY.
Instance 'k8s-04-19-216-bigdata-test:3306' was switched from PRIMARY to SECONDARY.
Instance 'k8s-01-19-212-bigdata-test:3306' was switched from PRIMARY to SECONDARY.
Instance 'k8s-02-19-214-bigdata-test:3306' was switched from PRIMARY to SECONDARY.

WARNING: The cluster internal session is not the primary member anymore. For cluster management operations please obtain a fresh cluster handle using dba.getCluster().

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  10.100.19.214:3306 ssl  
切换细节

15、新增成员方法

# 新增成员,授权后
mysqlsh 'mgradmin'@'10.100.19.216':3306 --js --quiet-start=2 -C required
var cluster = dba.getCluster()
dba.checkInstanceConfiguration('mgradmin@10.100.19.212:3306')
dba.configureInstance('mgradmin@10.100.19.212:3306')
cluster.addInstance('mgradmin@10.100.19.212:3306')
cluster.status()

 

posted @ 2021-10-27 14:46  davie2020  阅读(91)  评论(0编辑  收藏  举报