预发布版本:2024-07-17
示例任务使用带有 AdminAPI 的沙盒部署来部署 InnoDB Cluster。
部署和使用 MySQL 的本地沙盒实例,您可以在将其部署到生产服务器之前在本地测试功能。AdminAPI 具有内置功能,可创建预先配置为在本地部署方案中使用 InnoDB Cluster、InnoDB ClusterSet 和 InnoDB ReplicaSet 的沙盒实例。
沙盒实例仅适用于在您的本地机器上部署和运行以进行测试。
为了提供对一次故障的容忍度,请创建一个包含三个实例的 InnoDB Cluster。在本例中,您将使用在同一台机器上运行的三个沙盒实例。在实际设置中,这三个实例可以在网络上的不同主机上运行。
打开 MySQL Shell 控制台会话。
-
将活动语言更改为 Python
\py
-
要创建并启动 MySQL 沙盒实例,请使用
dba.deploy_sandbox_instance()
方法。在 MySQL Shell 中发出以下三个语句,并为每个实例输入 root 密码dba.deploy_sandbox_instance(3310) dba.deploy_sandbox_instance(3320) dba.deploy_sandbox_instance(3330)
注意对所有实例使用相同的 root 密码。
要创建 InnoDB Cluster,请完成以下步骤
-
连接到您希望作为 InnoDB Cluster 中主实例的 MySQL 实例。
shell.connect('root@localhost:3310')
-
发出
dba.create_cluster()
方法以创建一个名为devCluster
的 InnoDB Cluster,并使用分配的变量cluster
来保存输出的值cluster = dba.create_cluster('devCluster')
此命令输出
A new InnoDB cluster will be created on instance 'localhost:3310'. Validating instance configuration at localhost:3310... 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:3310 Instance configuration is suitable. NOTE: Group Replication will communicate with other members using '127.0.0.1:33101'. Use the localAddress option to override. Creating InnoDB cluster 'devCluster' on '127.0.0.1:3310'... 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:devCluster>
-
通过使用分配的变量
cluster
以及cluster.status()
方法验证创建是否成功cluster.status()
将输出以下状态
{ “clusterName”: “devCluster”, “defaultReplicaSet”: { "name": "default", "primary": "127.0.0.1:3310", "ssl": "REQUIRED", "status": "OK_NO_TOLERANCE", "statusText": "Cluster is NOT tolerant to any failures.", "topology": { "127.0.0.1:3310": { "address": "127.0.0.1:3310", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" } }, "topologyMode": "Single-Primary" }, “groupInformationSourceMember”: “127.0.0.1:3310” }
-
集群已启动并运行,但尚不能容忍故障。使用
add_instance()
方法将其他 MySQL 服务器实例添加到集群{ cluster.add_instance('root@localhost:3320')
输出为
NOTE: The target instance '127.0.0.1:3320' 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:3320' 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): ) }
-
从提示中选择恢复方法。选项包括
Clone
:克隆您要添加到主集群的实例,删除该实例包含的任何事务。MySQL Clone 插件将自动安装。假设您要添加一个空实例(尚未处理任何事务)或一个包含您不想保留的事务的实例,请选择 Clone 选项。Incremental recovery
:使用异步复制将集群处理的所有事务恢复到加入的实例。如果您确定集群处理的所有更新都已在启用全局事务 ID (GTID
) 的情况下完成,则增量恢复是合适的。没有已清除的事务,并且新实例包含与集群相同或子集的GTID
集。
在本例中,选择 C 表示
Clone
选择
Clone
选项后,输出为Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone): C Validating instance configuration at localhost:3320... 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:3320 Instance configuration is suitable. NOTE: Group Replication will communicate with other members using '127.0.0.1:33201'. 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: 127.0.0.1:3320 is being cloned from 127.0.0.1:3310 ** Stage DROP DATA: Completed ** Clone Transfer FILE COPY ############################################################ 100% Completed PAGE COPY ############################################################ 100% Completed REDO COPY ############################################################ 100% Completed NOTE: 127.0.0.1:3320 is shutting down... * Waiting for server restart... ready * 127.0.0.1:3320 has restarted, waiting for clone to finish... ** Stage RESTART: Completed * Clone process has finished: 72.61 MB transferred in about 1 second (~72.61 MB/s) State recovery already finished for '127.0.0.1:3320' The instance '127.0.0.1:3320' was successfully added to the cluster.
-
添加创建的第三个实例,并再次为克隆恢复方法选择
C
cluster.add_instance('root@localhost:3330')
-
通过输入以下内容来检查集群的状态
cluster.status()
这将输出以下内容
{ "clusterName": "devCluster", "defaultReplicaSet": { "name": "default", "primary": "127.0.0.1:3310", "ssl": "REQUIRED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "127.0.0.1:3310": { "address": "127.0.0.1:3310", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" }, "127.0.0.1:3320": { "address": "127.0.0.1:3320", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" }, "127.0.0.1:3330": { "address": "127.0.0.1:3330", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "127.0.0.1:3310" } The setup of the InnoDB Cluster was successful!
该集群现在可以容忍最多一次故障。
停止主实例,检查 InnoDB Cluster 的状态,最后重新启动该实例。
-
使用
dba.kill_sandbox_instance()
方法停止 MySQL 服务器实例dba.kill_sandbox_instance(3310)
输出为
Killing MySQL instance... Instance localhost:3310 successfully killed.
-
检查 InnoDB Cluster 的状态
cluster.status()
输出为
{ "clusterName": "devCluster", "defaultReplicaSet": { "name": "default", "primary": "127.0.0.1:3320", "ssl": "REQUIRED", "status": "OK_NO_TOLERANCE", "statusText": "Cluster is NOT tolerant to any failures. 1 member is not active.", "topology": { "127.0.0.1:3310": { "address": "127.0.0.1:3310", "memberRole": "SECONDARY", "mode": "n/a", "readReplicas": {}, "role": "HA", "shellConnectError": "MySQL Error 2003: Could not open connection to '127.0.0.1:3310': Can't connect to MySQL server on '127.0.0.1:3310' (10061)", "status": "(MISSING)" }, "127.0.0.1:3320": { "address": "127.0.0.1:3320", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" }, "127.0.0.1:3330": { "address": "127.0.0.1:3330", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "127.0.0.1:3320" }
以前在端口 3310 上运行的 MySQL 服务器实例为
MISSING
。 -
通过发出带有端口号的
dba.start_sandbox_instance()
方法重新启动此丢失的实例dba.start_sandbox_instance(3310)
检查集群的状态显示该实例已在集群中恢复为活动状态,但作为辅助成员
cluster.status()
输出为
{ "clusterName": "devCluster", "defaultReplicaSet": { "name": "default", "primary": "127.0.0.1:3320", "ssl": "REQUIRED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "127.0.0.1:3310": { "address": "127.0.0.1:3310", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" }, "127.0.0.1:3320": { "address": "127.0.0.1:3320", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" }, "127.0.0.1:3330": { "address": "127.0.0.1:3330", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.28" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "127.0.0.1:3320" }
所有实例都已恢复在线,并且 InnoDB Cluster 再次可以容忍一次故障。