MySQL Shell 8.4  /  ...  /  將執行個體新增至 ReplicaSet 的範例

9.5.2 將執行個體新增至 ReplicaSet 的範例

若要將執行個體新增至 ReplicaSet,請完成下列步驟

  1. 使用 ReplicaSet.addInstance(instance) 操作將次要執行個體新增至 ReplicaSet。您將 instance 指定為類似 URI 的連線字串。您指定的用戶必須具有必要的權限,且在 ReplicaSet 中的所有執行個體上必須相同。如需更多資訊,請參閱 第 9.2 節「設定 InnoDB ReplicaSet 執行個體」

    例如,若要新增位於 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 的執行個體上沒有任何現有的交易。因此,不會有從加入的執行個體中刪除資料的風險。如需更多資訊,請參閱 第 9.5.1 節「為 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 從主要執行個體複製所有尚未套用的交易,然後以線上執行個體的身分加入 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"
    		}
    	}

    此輸出顯示名為 example 的 ReplicaSet 現在包含兩個 MySQL 執行個體,且主要執行個體是 rs-1。目前,在 rs-2 有一個次要執行個體,這是主要執行個體的複本。ReplicaSet 處於線上狀態,表示主要執行個體和次要執行個體同步。此時,ReplicaSet 已準備好處理交易。

  4. 若要覆寫互動式 MySQL Shell 模式,請選擇最適合的復原方法。使用 recoveryMethod 選項設定執行個體如何復原加入 ReplicaSet 所需的資料。如需更多資訊,請參閱 第 7.4.6 節「搭配 InnoDB Cluster 使用 MySQL Clone」