## 模块 1: 设置具有 DR Auto-Sync 配置的 TiDB 集群

在本实践中，我们将设置一个启用 DR Auto-Sync 配置的集群。以下节点分布在两个数据中心：

+ 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 作为应用程序部署在每个数据中心（对应下图中的 sysbench）。注意：测试简化了负载均衡服务，DC1 中 sysbench 直接连接 DC1 中的 tidb server，DC2 同理。

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

### 步骤 1: 连接到第一个数据中心的 Linux 主机

+ 打开 [终端 1](ssh://az1monitor1-ssh#1)，连接到 DC1 的远程堡垒机 Linux。

### 步骤 2. 查看 TiDB 集群拓扑配置。它跨 2 个数据中心（可用区）

+ 查看已配置的集群拓扑 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. 设置 TiDB 镜像并执行部署前预检查

  ```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 个子网中( DC1 的 10.90.1.0/24，DC2 的 10.90.2.0/24)，分别位于 2 个可用区。每个 DC 都有 2 个 PD 服务器(DC2 包含一个备用节点)、2 个 TiKV 服务器和 1 个 TiDB 服务器。因此，我们可以使用这种部署来模拟双数据中心环境。

  ```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. 确保 Placement Driver Leader 位于 DC1

+ 如果 PD Leader 不在 DC1 中（IP 属于 10.90.1.0/24 子网），将 Leader 转移到 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
  ```

+ 您可以执行此命令，无论上一步的结果如何，都尝试将 PD Leader 转移到 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 Leader 的位置，确认其已切换到 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 的集群 meta 文件复制到位于灾难恢复数据中心 (DC2) 的 monitor 2 服务器的 tiup 目录。如果提示，请回答 `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)，连接到 monitor 2，验证 tiup 的 meta 文件是否正常工作。

+ 在 [终端2](ssh://az2monitor1-ssh#2) 上，确认 monitor 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), 检查当前集群的数据复制模式，应为默认的 `majority` 模式。

  ```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 Auto-Sync 配置准备的 Placement Rules 文件。集群包含两个数据中心，voter 角色被限制在 DC1。在 DC2 中，有 follower 和 learner 角色。

  ```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)

+ 加载 Placement Rules 配置，期间 `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.tidb.io/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."
  ```

+ 检查 Placement Rules 配置是否已加载。

  ```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!
  ```

+ 检查没有 learner peer 的 regions。验证所有 regions 都包含 learner peer，并确保 voter 和 learner 角色之间的数据同步。此步骤确保集群的数据在所有角色中一致复制，这对维护高可用性和故障切换准备至关重要。

  ```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 Auto-Sync 配置。

  ```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 节点处于 learner 角色。
  + set replication-mode dr-auto-sync primary-replicas 2：设置主数据中心（DC1）的副本数量为 2。
  + set replication-mode dr-auto-sync dr-replicas 1：设置灾备数据中心（DC2）的副本数量为 1（不包含 learner 角色）。

+ 验证配置是否生效。

  ```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，两个数据中心（两个 commit group）严格按照同步复制模式工作.

### 步骤 10. 创建 sysbench 数据库

+ 登录到 TiDB。

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

  ```output
  mysql> 
  ```

+ 为 sysbench 创建数据库。

  ```bash
  CREATE DATABASE sbtest;
  ```

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

+ 退出到 monitor 的操作系统命令行。

  ```bash
  exit
  ```

  ```output
  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 Auto-Sync 部署方案并验证其状态。
  + 准备了用于模拟应用行为的 sysbench 配置。
