Documentation Home
MySQL 外壳 8.0  / 第 9 章 MySQL InnoDB 副本集  / 9.4 添加实例到 ReplicaSet  /  9.4.2 向ReplicaSet添加实例示例

9.4.2 向ReplicaSet添加实例示例

要将实例添加到 ReplicaSet,请完成以下步骤:

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

    例如,要在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选项是因为我们在加入 ReplicaSet 的实例上没有任何现有事务。因此,不存在从加入实例中删除数据的风险。有关更多信息,请参阅 InnoDB ReplicaSet 的恢复方法

    	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 ReplicaSet 使用有效,恢复继续。在这种情况下,新加入的实例使用 MySQL Clone 从 primary 复制它尚未应用的所有事务,然后它作为在线实例加入 ReplicaSet。要验证,请使用以下 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"
    		}
    	}

    此输出显示名为 examplenow 的 ReplicaSet 由两个 MySQL 实例组成,并且主实例是rs-1. 当前,在 有一个辅助实例 rs-2,它是主实例的副本。ReplicaSet在线,即主备同步。此时,ReplicaSet 已准备好处理事务。

  4. 要覆盖交互式 MySQL Shell 模式,请选择最合适的恢复方法。使用该 recoveryMethod选项配置实例如何恢复能够加入 ReplicaSet 所需的数据。有关详细信息,请参阅 第 7.4.6 节,“将 MySQL 克隆与 InnoDB Cluster 一起使用”