## モジュール 1: DR 自動同期構成で TiDB クラスタをセットアップ

この演習では、DR自動同期構成を有効にしたクラスターを設定します。次のノードが2つのデータセンターに分散しています:

+ DC1はプライマリデータセンターで、AWSのAZ1に位置しています: `pd x2, tidb x1, tikv x2, tiup(monitor) x1`
+ DC2は災害復旧データセンターで、AWSのAZ2に位置しています: `pd x1(さらに予備の1ノード待機), tidb x1, tikv x2, tiup(monitor) x1`
+ Sysbenchはアプリケーションとして機能し、各データセンターにデプロイされています（下の図の`client`に対応）。注: テストはロードバランシングサービスを簡略化し、DC1のsysbenchはDC1のTiDBサーバーに直接接続し、DC2のsysbenchも同様です。

![60%](https://lab-static.pingcap.com/quick-demo/demo-303-4-en.png)

### 手順 1. 最初のデータセンターでモニターノードに接続する

+ [ターミナル 1](ssh://az1monitor1-ssh#1) を開き、DC1 のリモートバスチオン Linux に接続します。

### 手順 2. マルチアベイラビリティゾーンの TiDB クラスタトポロジ構成を確認する

+ プロビジョニングされたクラスタトポロジーのYAMLを確認します。

  ```bash
  cat dr-auto-sync.yaml
  ```

  ```output
  global:
   user: "ec2-user"
   ssh_port: 22
   deploy_dir: "/tidb-deploy"
   data_dir: "/tidb-data"
   arch: "amd64"
  monitored:
   node_exporter_port: 9100
   blackbox_exporter_port: 9115
   deploy_dir: "/tidb-deploy/monitored-9100"
   data_dir: "/tidb-data/monitor_9115"
   log_dir: "/tidb-deploy/monitor-9100/log"
  server_configs:
   pd:
     replication.location-labels: ["dc","logic","rack","host"]
  tidb_servers:
   - host: 10.90.1.163
     port: 4000
     status_port: 10080
     deploy_dir: "/tidb-deploy/tidb-4000"
     log_dir: "/tidb-deploy/tidb-4000/log"
   - host: 10.90.2.111
     port: 4000
     status_port: 10080
     deploy_dir: "/tidb-deploy/tidb-4000"
     log_dir: "/tidb-deploy/tidb-4000/log"
  tikv_servers:
   - host: 10.90.1.80
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc1",logic: "logic1",rack: "r1",host: "10.90.1.80" }
   - host: 10.90.1.45
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc1",logic: "logic2",rack: "r1",host: "10.90.1.45" }
   - host: 10.90.2.33
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc2",logic: "logic3",rack: "r1",host: "10.90.2.33" }
   - host: 10.90.2.113
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc2",logic: "logic4",rack: "r1",host: "10.90.2.113" }
  pd_servers:
   - host: 10.90.1.19
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://35.88.184.77:2379"
   - host: 10.90.1.44
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://34.219.233.52:2379"
   - host: 10.90.2.108
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://18.236.155.129:2379"
  monitoring_servers:
   - host: 10.90.1.147
     port: 10090
     deploy_dir: "/tidb-deploy/prometheus-10090"
     data_dir: "/tidb-data/prometheus_data"
   - host: 10.90.2.82
     port: 10090
     deploy_dir: "/tidb-deploy/prometheus-10090"
     data_dir: "/tidb-data/prometheus_data"
  grafana_servers:
   - host: 10.90.1.147
     port: 3000
     deploy_dir: "/tidb-deploy/grafana-3000"
   - host: 10.90.2.82
     port: 3000
     deploy_dir: "/tidb-deploy/grafana-3000"
  ```

### 手順 3. TiUP のミラーを設定して `precheck` コマンドを実行します

  ```bash
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check dr-auto-sync.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --apply
  ```

  ```output
  ...
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  - Applying changes on 10.90.2.33 ... Done
  - Applying changes on 10.90.2.113 ... Done
  - Applying changes on 10.90.1.163 ... Done
  - Applying changes on 10.90.2.111 ... Done
  - Applying changes on 10.90.1.147 ... Done
  - Applying changes on 10.90.1.44 ... Done
  - Applying changes on 10.90.2.108 ... Done
  - Applying changes on 10.90.1.45 ... Done
  - Applying changes on 10.90.2.82 ... Done
  - Applying changes on 10.90.1.19 ... Done
  - Applying changes on 10.90.1.80 ... Done
  ```

### 手順 4. TiDB クラスタをデプロイします

  ```bash
  tiup cluster deploy tidb-demo v${LAB:TIDB_VERSION} ./dr-auto-sync.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --yes
  ```

  ```output
  ...
  Cluster `tidb-demo` deployed successfully, you can start it with command: `tiup cluster start tidb-demo --init`
  ```

### 手順 5. TiDB クラスタを開始します

  ```bash
  tiup cluster start tidb-demo
  ```

  ```output
  ...
  Started cluster `tidb-demo` successfully
  ```

### 手順 6. TiDB クラスタのステータスを確認します

+ クラスタのステータスを取得します。主要なクラスタコンポーネントは2つのサブネット（10.90.1.0/24 for DC1, 10.90.2.0/24 for DC2）に分布しており、2つのアベイラビリティゾーンに位置しています。各データセンターには2つのPDサーバー（DC2には待機中のpdノードが1つ含まれています）、2つのTiKVサーバー、および1つのTiDBサーバーがあります。したがって、このデプロイメントを使用して、2データセンター環境をシミュレートすることができます。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://54.188.60.25:2379/dashboard
  Grafana URL:        http://10.90.1.114:3000,http://10.90.2.122:3000
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.114:3000   grafana     10.90.1.114  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.122:3000   grafana     10.90.2.122  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.1.106:2379   pd          10.90.1.106  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.19:2379    pd          10.90.1.19   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.162:2379   pd          10.90.2.162  2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.114:10090  prometheus  10.90.1.114  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.122:10090  prometheus  10.90.2.122  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.1.81:4000    tidb        10.90.1.81   4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.2.207:4000   tidb        10.90.2.207  4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.1.163:20160  tikv        10.90.1.163  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.1.83:20160   tikv        10.90.1.83   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.17:20160   tikv        10.90.2.17   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.19:20160   tikv        10.90.2.19   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 13
  ```

### 手順 7. プレースメントドライバーのリーダーがDC1にあることを確認します

+ プレースメントドライバー（PD）リーダーがDC1にない場合（IPが10.90.1.0/24サブネットに属しない場合）、リーダーをDC1に移行します。

  ```output
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.106:2379   pd          10.90.1.106  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.19:2379    pd          10.90.1.19   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.162:2379   pd          10.90.2.162  2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  ```

+ 前のステップの結果にかかわらず、リーダーをDC1に移行するためにこのコマンドを実行できます。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 member leader transfer "pd-${HOST_AZ1PD1_PRIVATE_IP}-2379"
  ```

  ```output
  The component `ctl` version v${LAB:TIDB_VERSION} is not installed; downloading from repository.
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.1.19:2379 member leader transfer pd-10.90.1.106-2379
  Success!
  ```

+ PDリーダーの場所を確認し、DC1に移行していることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ...
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  ...
  10.90.1.106:2379   pd          10.90.1.106  2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.19:2379    pd          10.90.1.19   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.162:2379   pd          10.90.2.162  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  ...
  ```

### 手順 8. DC2 にピアリング TiUP モニターを作成します

+ TiUPのクラスタメタファイルをモニター2（Tiupディレクトリ）にコピーします。これは災害復旧データセンター（DC2）にあります。メッセージが表示されたら`yes`と答えてください。

  ```bash
  scp -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem -r .tiup/storage/ ec2-user@${LAB:HOST_AZ2MONITOR1_PRIVATE_IP}:/home/ec2-user/.tiup/
  ```

  ```output
  ...
  run_blackbox_exporter_10.90.1.163.sh                                              100%  519   356.8KB/s   00:00    
  run_blackbox_exporter_10.90.2.82.sh                                               100%  519   414.9KB/s   00:00    
  meta.yaml                                                                         100% 4481     3.2MB/s   00:00
  ```

+ 新しいターミナル - [ターミナル2](ssh://az2monitor1-ssh#2) を開き、モニター2に接続して tiup のメタファイルが正しく機能しているか確認します。

+ [ターミナル2](ssh://az2monitor1-ssh#2) 上で、モニター2の `tiup` が正常に動作することを確認します。

  ```bash
  tiup cluster display tidb-demo 
  ```

  ```output
  Checking updates for component cluster... 
  A new version of cluster is available: v1.15.1 -> v1.16.1

     To update this component:   tiup update cluster
     To update all components:   tiup update --all

  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://35.88.184.77:2379/dashboard
  Grafana URL:        http://10.90.1.147:3000,http://10.90.2.82:3000
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.147:3000   grafana     10.90.1.147  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.82:3000    grafana     10.90.2.82   3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.1.19:2379    pd          10.90.1.19   2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.44:2379    pd          10.90.1.44   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.108:2379   pd          10.90.2.108  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.147:10090  prometheus  10.90.1.147  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.82:10090   prometheus  10.90.2.82   10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.1.163:4000   tidb        10.90.1.163  4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.2.111:4000   tidb        10.90.2.111  4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.1.45:20160   tikv        10.90.1.45   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.1.80:20160   tikv        10.90.1.80   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.113:20160  tikv        10.90.2.113  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.33:20160   tikv        10.90.2.33   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 13
  ```

### 手順 9. TiDB クラスタで DR Auto-Sync を有効にします

+ [ターミナル1](ssh://az1monitor1-ssh#1) に戻ります, 現在のクラスタデータのレプリケーションモードを確認します。デフォルトでは`過半数`でのはずです。注: 結果が`"acid_consistent": false`である場合、プライマリデータセンターと災害復旧データセンター間のデータレプリケーションが`同期`モードではないことを意味します。したがって、プライマリデータセンターが完全に障害を起こした場合、トランザクションの`ACID`特性は絶対的に保証されません。

  ```bash
  curl ${HOST_AZ1PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  {
   "mode": "majority",
   "dr-auto-sync": {
     "label_key": "",
     "state": "",
     "acid_consistent": false
   }
  }
  ```

+ DR自動同期構成用に準備したプレースメントルールファイルを確認します。データセンターが2つあり、投票者ロールはDC1に限定されています。DC2にはフォロワーとラーナーロールがあります。

  ```bash
  cat dr-anto-sync-rules.json
  ```

  ```output
  [
   {
     "group_id": "pd",
     "group_index": 0,
     "group_override": false,
     "rules": [
       {
         "group_id": "pd",
         "id": "dc1",
         "start_key": "",
         "end_key": "",
         "role": "voter",
         "count": 2,
         "location_labels": ["dc", "logic", "rack", "host"],
         "label_constraints": [{"key": "dc", "op": "in", "values": ["dc1"]}]
       },
       {
         "group_id": "pd",
         "id": "logic3",
         "start_key": "",
         "end_key": "",
         "role": "follower",
         "count": 1,
         "location_labels": ["dc", "logic", "rack", "host"],
         "label_constraints": [{"key": "logic", "op": "in", "values": ["logic3"]}]
       },
       {
         "group_id": "pd",
         "id": "logic4",
         "start_key": "",
         "end_key": "",
         "role": "learner",
         "count": 1,
         "location_labels": ["dc", "logic", "rack", "host"],
         "label_constraints": [{"key": "logic", "op": "in", "values": ["logic4"]}]
       }
     ]
   }
  ]
  ```

![60%](https://lab-static.pingcap.com/quick-demo/demo-303-41-en.png)

+ 上記のプレースメントルールをインポートすることで、PDは異なるノードの分配設計を認識することができます（Learnerに関する詳細：[PD Scheduling Best Practices - クラスタトポロジーの認識](https://docs.pingcap.com/tidb/stable/pd-scheduling-best-practices#cluster-topology-awareness)）。これにより、クラスタ内のリージョンのレプリカが可能な限り分散されるようになります。PDはlocation-labelsの設定に基づいてスケジュールを作成します。設定の["dc", "logic", "rack", "host"]は4レベルのトポロジー構造を定義しています:
  + クラスタは複数のdc（データセンター）に分割されています。
  + 各dcは複数のlogic（論理ゾーン）を含んでいます。
  + 各logicは複数のrackユニットを含んでいます。
  + 各rackは複数のhostユニットを含んでいます。
+ スケジューリング時にPDは:
  + リージョンのピアを異なるdcに優先的に配置します。
  + これが不可能な場合（例えば、3つのレプリカが構成されているが、利用可能なデータセンターが2つしかない場合）、PDは異なる論理ゾーンに配置を保証します。
  + 論理ゾーンの数も分離を保証するには不十分な場合、ラックレベルでの隔離を試みます。
  + ラックユニットの数も不十分である場合、ホストレベルでの隔離を試みます。

+ プレースメントルール構成をロードします。`ctl`ツールは自動的にダウンロードおよびインストールされます。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd config placement-rules rule-bundle -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 save --in="/home/ec2-user/dr-anto-sync-rules.json"
  ```

  ```output
  The component `ctl` version v${LAB:TIDB_VERSION} is not installed; downloading from repository.
  download https://tiup-mirrors.pingcap.com/ctl-v${LAB:TIDB_VERSION}-linux-amd64.tar.gz 408.65 MiB / 408.65 MiB 100.00% 103.24 MiB/s 
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd config placement-rules rule-bundle -u http://10.90.2.162:2379 save --in=/home/ec2-user/dr-anto-sync-rules.json
  "Update rules and groups successfully."
  ```

+ プレースメントルール構成がロードされているか確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd config placement-rules show -u ${HOST_AZ1PD1_PRIVATE_IP}:2379
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd config placement-rules show -u http://10.90.2.162:2379
  [
   {
     "group_id": "pd",
     "id": "dc1",
     "start_key": "",
     "end_key": "",
     "role": "voter",
     "is_witness": false,
     "count": 2,
     "label_constraints": [
       {
         "key": "dc",
         "op": "in",
         "values": [
           "dc1"
         ]
       }
     ],
     "location_labels": [
       "dc",
       "logic",
       "rack",
       "host"
     ],
     "version": 2,
     "create_timestamp": 1733292613
   },
   {
     "group_id": "pd",
     "id": "logic3",
     "start_key": "",
     "end_key": "",
     "role": "voter",
     "is_witness": false,
     "count": 1,
     "label_constraints": [
       {
         "key": "logic",
         "op": "in",
         "values": [
           "logic3"
         ]
       }
     ],
     "location_labels": [
       "dc",
       "logic",
       "rack",
       "host"
     ],
     "version": 2,
     "create_timestamp": 1733292613
   },
   {
     "group_id": "pd",
     "id": "logic4",
     "start_key": "",
     "end_key": "",
     "role": "learner",
     "is_witness": false,
     "count": 1,
     "label_constraints": [
       {
         "key": "logic",
         "op": "in",
         "values": [
           "logic4"
         ]
       }
     ],
     "location_labels": [
       "dc",
       "logic",
       "rack",
       "host"
     ],
     "version": 2,
     "create_timestamp": 1733292613
   }
  ]
  ```

+ `jq`ツールをインストールします。

  ```bash
  sudo yum install jq -y
  ```

  ```output
  ...
  Installed:
   jq.x86_64 0:1.5-1.amzn2.0. 2  

  Dependency Installed:
   oniguruma.x86_64 0:5.9.6-1.amzn2.0. 7

  Complete!
  ```

+ `Leaner`ピアがないTiKVリージョンがないかを確認します。`Leaner`ピアのないリージョンがないことを確認し、投票者と`Leaner`の役割を持つ間でデータが同期されていることを保証します。このステップは、高可用性とフェイルオーバーの準備を維持するために重要なデータの整合性のある複製を保証します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 region --jq='.regions[] | select(.peers | any(.role_name=="Learner") | not) |  {id: .id, peers: [.peers]}'
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://34.215.73.186:2379 region --jq=.regions[] | select(.peers | any(.role_name=="Learner") | not) |  {id: .id, peers: [.peers]}
  ```

+ DR自動同期構成を有効にします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync label-key dc
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync primary dc1
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync dr dc2
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync primary-replicas 2
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync dr-replicas 1
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync label-key dc
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync primary dc1
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync dr dc2
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync primary-replicas 2
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync dr-replicas 1
  Success! The replication mode config is updated."
  ```

+ これらのパラメータの設定により、クラスタはデータセンターの位置を認識し、データの分配を制御することができます。
  + set replication-mode dr-auto-sync: TiDBのレプリケーションモードをdr-auto-syncに設定します。
  + set replication-mode dr-auto-sync label-key dc: PDがノードが属するデータセンターをdcラベルに基づいて識別できるようにし、データ分布の制御を達成します。
  + set replication-mode dr-auto-sync primary dc1: DC1をプライマリデータセンターに設定し、運用時にTiDBクラスタの主要な書き込みノードにします。すべてのデータ更新はプライマリデータセンターへの書き込みが優先されます。
  + set replication-mode dr-auto-sync dr dc2: DC2を災害復旧データセンターに設定し、TiKVノードが`Leaner`ロールで存在します。
  + set replication-mode dr-auto-sync primary-replicas 2: プライマリデータセンター（DC1）のレプリカ数を2に設定します。
  + set replication-mode dr-auto-sync dr-replicas 1: 災害復旧データセンター（DC2）のレプリカ数を1に設定します（`Leaner`ロールは含まれません）。

+ 構成が有効であるかどうかを確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 config show replication-mode
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379:2379 config show replication-mode
  {
   "replication-mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label-key": "dc",
     "primary": "dc1",
     "dr": "dc2",
     "primary-replicas": 2,
     "dr-replicas": 1,
     "wait-store-timeout": "1m0s",
     "wait-recover-timeout": "0s",
     "pause-region-split": "false"
   }
  }
  ```

+ 現在のクラスタのレプリケーション状態をチェックします。操作が速かった場合、状態が`sync`モードになる前に`sync_recover`モードと`recover_progress`を確認できます。

  ```bash
  curl ${HOST_AZ1PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  {
   "mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label_key": "dc",
     "state": "sync_recover",
     "state_id": 2003,
     "acid_consistent": false,
     "total_regions": 60,
     "synced_regions": 37,
     "recover_progress": 0.98333335
   }
  }
  ```

+ 最後に、TiDBの`sync`状態を取得します。

  ```output
  {
   "mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label_key": "dc",
     "state": "sync",
     "state_id": 378,
     "acid_consistent": true
   }
  }
  ```

+ 初期化の状態として、クラスタは同期レプリケーションモードにあります。PDはステータス情報をTiKVに送信し、二つのデータセンター（二コミットグループ）は厳密に同期レプリケーションモードに従って動作します。

### 手順 10. `sysbench` データベースを作成します

+ TiDBにログインします。

  ```bash
  mysql -h ${HOST_AZ1DB1_PRIVATE_IP} -P 4000 -u root  --connect-expired-password
  ```

  ```output
  mysql> 
  ```

+ `sysbench`用のデータベースを作成します。

  ```sql
  CREATE DATABASE sbtest;
  EXIT;
  ```

  ```output
  Query OK, 0 rows affected (0.13 sec)
  Bye
  ```

+ DC1用の`sysbench`構成を設定します。

  ```bash
  cat << EOF > sysbench_config
  mysql-host=${HOST_AZ1DB1_PRIVATE_IP}
  mysql-port=4000
  mysql-user=root
  mysql-password=
  mysql-db=sbtest
  time=3600
  threads=6
  report-interval=1
  EOF
  cat sysbench_config
  ```

  ```output
  mysql-host=10.90.1.125
  mysql-port=4000
  mysql-user=root
  mysql-password=
  mysql-db=sbtest
  time=3600
  threads=16
  report-interval=1
  ```

+ 次のモジュールのために`sysbench`データを準備します。

  ```bash
  sysbench --config-file=sysbench_config oltp_point_select --tables=6 --table-size=100 prepare
  ```

  ```output
  sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

  Initializing worker threads...

  Creating table 'sbtest3'...
  Creating table 'sbtest1'...
  Creating table 'sbtest5'...
  Creating table 'sbtest6'...
  Creating table 'sbtest4'...
  Creating table 'sbtest2'...
  Inserting 100 records into 'sbtest3'
  Creating a secondary index on 'sbtest3'...
  Inserting 100 records into 'sbtest1'
  Creating a secondary index on 'sbtest1'...
  Inserting 100 records into 'sbtest4'
  Creating a secondary index on 'sbtest4'...
  Inserting 100 records into 'sbtest6'
  Creating a secondary index on 'sbtest6'...
  Inserting 100 records into 'sbtest2'
  Creating a secondary index on 'sbtest2'...
  Inserting 100 records into 'sbtest5'
  Creating a secondary index on 'sbtest5'...
  ```

+ ターミナル1とターミナル2を閉じます。

### モジュール 1のまとめ

+ ここまでで、次のことを完了しました:
  + TiDBクラスタの作成。
  + TiDBクラスタのDR自動同期デプロイメント計画を有効にし、そのステータスを確認しました。
  + アプリケーションの動作をシミュレートするためにsysbenchの設定を準備しました。
