MySQL Shell 9.0  /  ...  /  将实例添加到副本集的示例

9.5.2 将实例添加到副本集的示例

要将实例添加到副本集,请完成以下步骤

  1. 使用 ReplicaSet.addInstance(instance) 操作将辅助实例添加到 ReplicaSet。您将 instance 指定为类似 URI 的连接字符串。您指定的用户名必须具有所需的权限,并且必须在副本集中的所有实例上相同。有关更多信息,请参阅 第 9.2 节,“配置 InnoDB 副本集实例”

    例如,要添加在 rs-2、端口号为 3306 且用户为 rsadmin 的实例,请执行以下操作

    	mysql-js> rs.addInstance('rsadmin@rs-2:3306')
    
    	Adding instance to the replicaset...
    
    	* Performing validation checks
    
    	This instance reports its own address as rsadmin@rs-2
    	rsadmin@rs-2: Instance configuration is suitable.
    
    	* Checking async replication topology...
    
    	* Checking transaction state of the instance...
    
    	NOTE: The target instance 'rsadmin@rs-2' has not been pre-provisioned (GTID set
    	is empty). The Shell is unable to decide whether replication can completely
    	recover its state.  The safest and most convenient way to provision a new
    	instance is through automatic clone provisioning, which will completely
    	overwrite the state of 'rsadmin@rs-2' with a physical snapshot from an existing
    	replicaset member. To use this method by default, set the 'recoveryMethod'
    	option to 'clone'.
    
    	WARNING: It should be safe to rely on replication to incrementally recover the
    	state of the new instance if you are sure all updates ever processed in the
    	replicaset were done with GTIDs enabled, there are no purged transactions and
    	the new instance contains the same GTID set as the replicaset, 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):
  2. 在这种情况下,我们没有指定恢复方法,因此操作会建议您如何最好地进行。在这个例子中,我们选择 Clone 选项,因为我们没有加入副本集的实例上的任何现有事务。因此,没有从加入实例中删除数据的风险。有关更多信息,请参阅 第 9.5.1 节,“为 InnoDB 副本集配置实例”

    	Please select a recovery method [C]lone/[I]ncremental recovery/[A]bort (default Clone): 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: rsadmin@rs-2 is being cloned from rsadmin@rs-1
    	** Stage DROP DATA: Completed
    	** Clone Transfer
    	FILE COPY  ############################################################  100%  Completed
    	PAGE COPY  ############################################################  100%  Completed
    	REDO COPY  ############################################################  100%  Completed
    	** Stage RECOVERY: \
    	NOTE: rsadmin@rs-2 is shutting down...
    
    	* Waiting for server restart... ready
    	* rsadmin@rs-2 has restarted, waiting for clone to finish...
    	* Clone process has finished: 59.63 MB transferred in about 1 second (~1.00 B/s)
    
    	** Configuring rsadmin@rs-2 to replicate from rsadmin@rs-1
    	** Waiting for new instance to synchronize with PRIMARY...
    
    	The instance 'rsadmin@rs-2' was added to the replicaset and is replicating from rsadmin@rs-1.
  3. 假设该实例对于 InnoDB 副本集使用有效,恢复将继续进行。在这种情况下,新加入的实例使用 MySQL Clone 复制它尚未从主服务器应用的所有事务,然后它作为在线实例加入副本集。要进行验证,请使用 rs.status() 操作

    	mysql-js> rs.status()
    	{
    		"replicaSet": {
    			"name": "example",
    			"primary": "rs-1:3306",
    			"status": "AVAILABLE",
    			"statusText": "All instances available.",
    			"topology": {
    				"rs-1:3306": {
    					"address": "rs-1:3306",
    					"instanceRole": "PRIMARY",
    					"mode": "R/W",
    					"status": "ONLINE"
    				},
    				"rs-2:3306": {
    					"address": "rs-2:3306",
    					"instanceRole": "SECONDARY",
    					"mode": "R/O",
    					"replication": {
    						"applierStatus": "APPLIED_ALL",
    						"applierThreadState": "Replica has read all relay log; waiting for more updates",
    						"receiverStatus": "ON",
    						"receiverThreadState": "Waiting for source to send event",
    						"replicationLag": null
    					},
    					"status": "ONLINE"
    				}
    			},
    			"type": "ASYNC"
    		}
    	}

    此输出显示名为 example 的副本集现在包含两个 MySQL 实例,并且主实例为 rs-1。当前,有一个在 rs-2 的辅助实例,它是主实例的副本。副本集处于联机状态,这意味着主实例和辅助实例已同步。此时,副本集已准备好处理事务。

  4. 要覆盖交互式 MySQL Shell 模式,请选择最合适的恢复方法。使用 recoveryMethod 选项来配置实例恢复加入副本集所需数据的过程。有关更多信息,请参阅 第 7.4.6 节,“将 MySQL Clone 用于 InnoDB 集群”