vlambda博客
学习文章列表

使用MySQL Shell搭建MGR环境

本篇基于Red Hat Enterprise Linux release 8.1使用MySQL Shell 8.0.28搭建MGR环境


1)部署 SandBox 环境

[root@serverc local]# mysqlshMySQL Shell 8.0.28-commercialCopyright (c) 2016, 2022, Oracle and/or its affiliates.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.MySQL JS > dba.deploySandboxInstance(3307);MySQL JS > dba.deploySandboxInstance(3308);MySQL JS > dba.deploySandboxInstance(3309);

2)检查实例配置,若满足,则返回OK,否则修复配置项

MySQL localhost:3307 ssl JS > dba.checkInstanceConfiguration();Validating local MySQL instance listening at port 3307 for use in an InnoDB cluster...NOTE: Instance detected as a sandbox.Please note that sandbox instances are only suitable for deploying test clusters for use within the same host.This instance reports its own address as 127.0.0.1:3307Checking whether existing tables comply with Group Replication requirements...No incompatible tables detectedChecking instance configuration...Instance configuration is compatible with InnoDB clusterThe instance '127.0.0.1:3307' is valid to be used in an InnoDB cluster.{"status": "ok"}

3)配置实例

MySQL localhost:3307 ssl JS > dba.configureInstance('localhost:3307');Configuring local MySQL instance listening at port 3307 for use in an InnoDB cluster...NOTE: Instance detected as a sandbox.Please note that sandbox instances are only suitable for deploying test clusters for use within the same host.This instance reports its own address as 127.0.0.1:3307applierWorkerThreads will be set to the default value of 4.The instance '127.0.0.1:3307' is valid to be used in an InnoDB cluster.The instance '127.0.0.1:3307' is already ready to be used in an InnoDB cluster.Successfully enabled parallel appliers.

4)创建 MGR

MySQL localhost:3307 ssl JS > var cluster=dba.createCluster('testMGR');A new InnoDB cluster will be created on instance 'localhost:3307'.Validating instance configuration at localhost:3307...NOTE: Instance detected as a sandbox.Please note that sandbox instances are only suitable for deploying test clusters for use within the same host.This instance reports its own address as 127.0.0.1:3307Instance configuration is suitable.NOTE: Group Replication will communicate with other members using '127.0.0.1:33071'. Use the localAddress option to override.Creating InnoDB cluster 'testMGR' on '127.0.0.1:3307'...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 toone server failure.

5)添加成员

MySQL localhost:3307 ssl JS > cluster.addInstance('localhost:3308');NOTE: The target instance '127.0.0.1:3308' has not been pre-provisioned (GTID set is empty). The Shell is unable to decide whether incremental state recovery can correctly provision it.The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of '127.0.0.1:3308' with a physical snapshot from an existing cluster member. To use this method by default, set the 'recoveryMethod' option to 'clone'.The incremental state recovery may be safely used if you are sure all updates ever executed in the cluster were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the cluster or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'.Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone):Validating instance configuration at localhost:3308...NOTE: Instance detected as a sandbox.Please note that sandbox instances are only suitable for deploying test clusters for use within the same host.This instance reports its own address as 127.0.0.1:3308Instance configuration is suitable.NOTE: Group Replication will communicate with other members using '127.0.0.1:33081'. Use the localAddress option to override.A new instance will be added to the InnoDB cluster. Depending on the amount ofdata 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 theserver does not support the RESTART command or does not come back after awhile, you may need to manually start it back.Waiting for clone to finish...NOTE: 127.0.0.1:3308 is being cloned from 127.0.0.1:3307** Stage DROP DATA: Completed** Clone TransferFILE COPY ############################################################ 100% CompletedPAGE COPY ############################################################ 100% CompletedREDO COPY ############################################################ 100% CompletedNOTE: 127.0.0.1:3308 is shutting down...Waiting for server restart... ready127.0.0.1:3308 has restarted, waiting for clone to finish...** Stage RESTART: CompletedClone process has finished: 72.61 MB transferred in about 1 second (~72.61 MB/s)State recovery already finished for '127.0.0.1:3308'The instance '127.0.0.1:3308' was successfully added to the cluster.MySQL localhost:3307 ssl JS > cluster.addInstance('localhost:3309');

6)查看集群状态

MySQL localhost:3307 ssl JS > cluster.status();{"clusterName": "testMGR","defaultReplicaSet": {"name": "default","primary": "127.0.0.1:3307","ssl": "REQUIRED","status": "OK","statusText": "Cluster is ONLINE and can tolerate up to ONE failure.","topology": {"127.0.0.1:3307": {"address": "127.0.0.1:3307","memberRole": "PRIMARY","mode": "R/W","readReplicas": {},"replicationLag": null,"role": "HA","status": "ONLINE","version": "8.0.28"},"127.0.0.1:3308": {"address": "127.0.0.1:3308","memberRole": "SECONDARY","mode": "R/O","readReplicas": {},"replicationLag": null,"role": "HA","status": "ONLINE","version": "8.0.28"},"127.0.0.1:3309": {"address": "127.0.0.1:3309","memberRole": "SECONDARY","mode": "R/O","readReplicas": {},"replicationLag": null,"role": "HA","status": "ONLINE","version": "8.0.28"}},"topologyMode": "Single-Primary"},"groupInformationSourceMember": "127.0.0.1:3307"}

7)使用SQL查看集群状态

MySQL localhost:3307 ssl JS > \sqlSwitching to SQL mode... Commands end with ;MySQL localhost:3307 ssl SQL > select * from performance_schema.replication_group_members\G;*************************** 1. row ***************************CHANNEL_NAME: group_replication_applierMEMBER_ID: 56c5f854-c95b-11ec-b051-080027f0b1d1MEMBER_HOST: 127.0.0.1MEMBER_PORT: 3308MEMBER_STATE: ONLINEMEMBER_ROLE: SECONDARYMEMBER_VERSION: 8.0.28MEMBER_COMMUNICATION_STACK: XCom*************************** 2. row ***************************CHANNEL_NAME: group_replication_applierMEMBER_ID: 5af915c7-c930-11ec-a32c-080027f0b1d1MEMBER_HOST: 127.0.0.1MEMBER_PORT: 3307MEMBER_STATE: ONLINEMEMBER_ROLE: PRIMARYMEMBER_VERSION: 8.0.28MEMBER_COMMUNICATION_STACK: XCom*************************** 3. row ***************************CHANNEL_NAME: group_replication_applierMEMBER_ID: 634aa2f6-c95b-11ec-b438-080027f0b1d1MEMBER_HOST: 127.0.0.1MEMBER_PORT: 3309MEMBER_STATE: ONLINEMEMBER_ROLE: SECONDARYMEMBER_VERSION: 8.0.28MEMBER_COMMUNICATION_STACK: XCom3 rows in set (0.0006 sec)ERROR: 1065 (42000): Query was empty

由此可见,使用MySQL Shell来搭建MGR则更为方便。