文件首頁
MySQL Shell for VS Code


MySQL Shell for VS Code  /  Python  /  MySQL Shell 主控台:使用 Python 設定 InnoDB Cluster

預先發行版:2024-07-17

7.1 MySQL Shell 主控台:使用 Python 設定 InnoDB Cluster

範例任務使用具有 AdminAPI 的沙箱部署來部署 InnoDB Cluster。

部署和使用 MySQL 的本機沙箱執行個體,可讓您在本機測試功能,然後再部署到您的生產伺服器。AdminAPI 具有內建功能,可建立預先設定好的沙箱執行個體,以便在本機部署的案例中搭配 InnoDB Cluster、InnoDB ClusterSet 和 InnoDB ReplicaSet 使用。

警告

沙箱執行個體僅適用於在本機電腦上部署和執行以進行測試。

建立 InnoDB Cluster 沙箱組態

為了提供一個容錯能力,請使用三個執行個體建立 InnoDB Cluster。在此範例中,您將使用在同一部電腦上執行的三個沙箱執行個體。在實際設定中,這三個執行個體可以在網路上的不同主機上執行。

  1. 開啟 MySQL Shell 主控台工作階段。

  2. 將作用中的語言變更為 Python

    \py
  3. 若要建立和啟動 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,請完成下列步驟

  1. 連線到您想要作為 InnoDB Cluster 中主要執行個體的 MySQL 執行個體。

    shell.connect('root@localhost:3310')
  2. 發出 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>
    
  3. 使用具有指定變數 clustercluster.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” }
    
  4. 叢集已啟動並執行,但尚未容錯。使用 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):
    )
    }
    
  5. 從提示中選取復原方法。選項如下

    • 複製:複製您新增至主要叢集的執行個體,並刪除執行個體包含的任何交易。將會自動安裝 MySQL 複製外掛程式。假設您要新增的執行個體是空的 (未處理任何交易) 或包含您不希望保留的交易,請選取「複製」選項。

    • 增量復原:使用非同步複製,將叢集處理的所有交易復原到聯結執行個體。如果您確定叢集處理的所有更新都已完成並啟用全域交易 ID (GTID),則增量復原很適合。沒有清除的交易,且新的執行個體包含與叢集相同的 GTID 集或其子集。

    在此範例中,請選取 C 作為 複製

    選取 複製 選項後,輸出為

         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.
    
  6. 新增建立的第三個執行個體,然後再次選取 C 作為複製復原方法

    cluster.add_instance('root@localhost:3330')
  7. 輸入下列內容,檢查叢集的狀態

    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 的狀態,然後重新啟動執行個體。

  1. 使用 dba.kill_sandbox_instance() 方法停止 MySQL 伺服器執行個體

    dba.kill_sandbox_instance(3310)

    輸出為

    Killing MySQL instance...
    
    Instance localhost:3310 successfully killed.
    
  2. 檢查 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 伺服器執行個體為 遺失

  3. 使用 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 可以再次容忍一次失敗。