## モジュール 2: サンプルデータベースを MySQL から TiDB に移行する

### 手順 1. TiUP コントロールマシンに接続する

+ ここをクリック - [ターミナル2](ssh://monitor1-ssh#2) して TiUP コントロールマシンに接続します。

### 手順 2. 移行目的のために MySQL で作成する

+ 移行目的のためにMySQLで専用ユーザー `dm_user` を作成します。SSHコマンドでプロンプトが表示された場合は、`yes` と答えてください。

   ```bash
   ssh -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem rocky@${HOST_MYSQL1_PRIVATE_IP}
   ```

   ```output
   [rocky@ip-10-90-1-38 ~]$
   ```

   ```bash
   X=q1w2e3R4_
   mysql -h localhost -P 3306 -uroot -p$X --connect-expired-password << EOF
   DROP USER IF EXISTS dm_user@'%';
   CREATE USER dm_user@'%' IDENTIFIED BY 'q1w2e3R4_';
   GRANT ALL PRIVILEGES ON *.* TO dm_user@'%';
   EXIT
   EOF
   ```

   ```output
   mysql: [Warning] Using a password on the command line interface can be insecure.
   ```

+ TiUPコントロールマシンへの退出。

   ```bash
   exit
   ```

   ```output
   logout
   Connection to 10.90.1.38 closed.
   ```

### 手順 2. TiDB クラスタをデプロイして開始します

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

   ```bash
   cat topology.yaml
   ```

   ```output
   global:
     user: "ec2-user"
     ssh_port: 22
     deploy_dir: "/tidb-deploy"
     data_dir: "/tidb-data"
     arch: "amd64"
   server_configs:
     pd:
       replication.max-replicas: 3
   pd_servers:
     - host: 10.90.2.11
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://34.221.120.15:2379"
   tidb_servers:
     - host: 10.90.1.173
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
   tikv_servers:
     - host: 10.90.1.233
       port: 20160
       status_port: 20180
     - host: 10.90.1.79
       port: 20160
       status_port: 20180
     - host: 10.90.1.242
       port: 20160
       status_port: 20180
   monitoring_servers:
     - host: 10.90.4.90
   grafana_servers:
     - host: 10.90.4.90
   alertmanager_servers:
     - host: 10.90.4.90
   tiproxy_servers:
     - host: 10.90.4.90
   ```

+ TiDBクラスタをデプロイするために、システム構成を修正します。

   ```bash
   ~/.tiup/bin/tiup mirror set tidb-community-server-tidb_version-linux-amd64
   ~/.tiup/bin/tiup cluster check topology.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
     - Applying changes on 10.90.2.239 ... ⠴ Sysctl: host=10.90.2.239 vm.swappiness = 0
     - Applying changes on 10.90.1.190 ... ⠴ Sysctl: host=10.90.1.190 vm.swappiness = 0
     - Applying changes on 10.90.1.233 ... ⠴ Sysctl: host=10.90.1.233 vm.swappiness = 0
   + Try to apply changes to fix failed checks
   + Try to apply changes to fix failed checks
     - Applying changes on 10.90.2.239 ... Done
     - Applying changes on 10.90.1.190 ... Done
     - Applying changes on 10.90.1.233 ... Done
     - Applying changes on 10.90.1.136 ... Done
     - Applying changes on 10.90.1.42 ... Done
     - Applying changes on 10.90.4.231 ... Done
   ```

+ TiDB クラスタをデプロイします。

   ```bash
   ~/.tiup/bin/tiup cluster deploy tidb-demo tidb_version ./topology.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`
   ```

+ デプロイされたTiDBクラスタを開始します。

   ```bash
   ~/.tiup/bin/tiup cluster start tidb-demo
   ```

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

+ TiDBクラスタのステータスを確認します。`tidb-demo` TiDBクラスタには1つのTiDBサーバーと3つのTiKVサーバーがあり、`TiProxy`は`Alert Manager`、`Grafana`、および`Prometheus`と同じノードにデプロイされています。

   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```

   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v6.5.9
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://34.221.120.15:2379/dashboard
   Grafana URL:        http://10.90.4.90:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.90:9093    alertmanager  10.90.4.90   9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.90:3000    grafana       10.90.4.90   3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.2.11:2379    pd            10.90.2.11   2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.90:9090    prometheus    10.90.4.90   9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.173:4000   tidb          10.90.1.173  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.1.233:20160  tikv          10.90.1.233  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.242:20160  tikv          10.90.1.242  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.79:20160   tikv          10.90.1.79   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.90:6000    tiproxy       10.90.4.90   6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 9
   ```

### 手順 3. 移行目的のために TiDB を準備する

+ 移行目的のために TiDB に専用ユーザー `dm_user` を作成します。

   ```bash
   mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -uroot << 'EOF'
   DROP USER IF EXISTS dm_user@'%';
   CREATE USER dm_user@'%' identified by 'q1w2e3R4_';
   GRANT ALL PRIVILEGES ON *.* TO dm_user@'%';
   EXIT
   EOF
   ```

+ TiDBでターゲットのリレーショナルデータベース`sbtest`とその空のテーブルを作成します。カラム`id`が`AUTO_INCREMENT`ではなく`AUTO_RANDOM`に設定されていることに注意してください。`AUTO_RANDOM`はホットスポットパフォーマンスの問題を回避するためのTiDBの特徴機能です。`AUTO_RANDOM`について詳しく学びたい方は、自己ペースで進めるワークショップ[TiDB Workshop for MySQL Users](https://eng.edu.pingcap.com/catalog/info/id:231)にご参加ください。

   ```bash
   mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -uroot << 'EOF'
   CREATE DATABASE sbtest;
   CREATE TABLE `sbtest`.`sbtest1` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   CREATE TABLE `sbtest`.`sbtest2` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   CREATE TABLE `sbtest`.`sbtest3` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   CREATE TABLE `sbtest`.`sbtest4` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   CREATE TABLE `sbtest`.`sbtest5` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   CREATE TABLE `sbtest`.`sbtest6` (
     `id` bigint NOT NULL AUTO_RANDOM,
     `k` int NOT NULL DEFAULT '0',
     `c` char(120) NOT NULL DEFAULT '',
     `pad` char(60) NOT NULL DEFAULT '',
     PRIMARY KEY (`id`),
     KEY `k_1` (`k`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
   SET GLOBAL allow_auto_random_explicit_insert = true;
   USE sbtest;
   SHOW TABLES;
   EXIT
   EOF
   ```

   ```output
   Tables_in_sbtest
   sbtest1
   sbtest2
   sbtest3
   sbtest4
   sbtest5
   sbtest6
   ```

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

+ TiDB Data Migration コンポーネントをインストールします。

   ```bash
   ~/.tiup/bin/tiup install dm dmctl:vtidb_version dm-master:vtidb_version dm-worker:vtidb_version
   ```

   ```bash
   ~/.tiup/bin/tiup list --verbose | grep dm
   ```

   ```output
   dm                 pingcap  v1.15.1,v1.16.0                     linux/amd64  Data Migration Platform manager
   dm-master          pingcap  v8.0.0,vtidb_version                       linux/amd64  dm-master component of Data Migration Platform.
   dm-worker          pingcap  v8.0.0,vtidb_version                       linux/amd64  dm-worker component of Data Migration Platform.
   dmctl              pingcap  v6.5.1,v6.5.9,v7.5.0,vtidb_version  linux/amd64  dmctl component of Data Migration Platform.
   ```

+ TiDB Data Migration クラスタのトポロジー構成ファイルは既にプロビジョニングされています。構成を確認してください、変更は必要ありません。

   ```bash
   cat dm-topology.yaml 
   ```

   ```output
   global:
     user: "ec2-user"
     ssh_port: 22
     deploy_dir: "/tidb-deploy"
     data_dir: "/tidb-data"
     arch: "amd64"
   master_servers:
     - host: 10.90.4.191
   worker_servers:
     - host: 10.90.3.49
     - host: 10.90.3.71
     - host: 10.90.3.135
   monitoring_servers:
     - host: 10.90.1.7
   grafana_servers:
     - host: 10.90.1.7
   alertmanager_servers:
     - host: 10.90.1.7
   ```

+ TiDB Data Migration クラスタをデプロイして開始する - `dm-test`。

   ```bash
   ~/.tiup/bin/tiup dm deploy dm-test vtidb_version ./dm-topology.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --yes
   ```

   ```output
   Starting component dm: /home/ec2-user/.tiup/components/dm/v1.16.0/tiup-dm deploy dm-test vtidb_version ./dm-topology.yaml --user ec2-user -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem --yes
   + Detect CPU Arch Name
     - Detecting node 10.90.4.191 Arch info ... Done
     - Detecting node 10.90.3.49 Arch info ... Done
     - Detecting node 10.90.3.71 Arch info ... Done
     - Detecting node 10.90.3.135 Arch info ... Done
     - Detecting node 10.90.1.7 Arch info ... Done
   ...
   Cluster `dm-test` deployed successfully, you can start it with command: `tiup dm start dm-test`
   ```

+ TiDB Data Migration クラスタを開始します - `dm-test`。

   ```bash
   ~/.tiup/bin/tiup dm start dm-test
   ```

   ```output
   Starting component dm: /home/ec2-user/.tiup/components/dm/v1.16.0/tiup-dm start dm-test
   Starting cluster dm-test...
   + [ Serial ] - SSHKeySet: privateKey=/home/ec2-user/.tiup/storage/dm/clusters/dm-test/ssh/id_rsa, publicKey=/home/ec2-user/.tiup/storage/dm/clusters/dm-test/ssh/id_rsa.pub
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.7
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.3.71
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.7
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.191
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.7
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.3.49
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.3.135
   + [ Serial ] - StartCluster
   Starting component dm-master
   	Starting instance 10.90.4.191:8261
   	Start instance 10.90.4.191:8261 success
   Starting component dm-worker
   	Starting instance 10.90.3.135:8262
   	Starting instance 10.90.3.49:8262
   	Starting instance 10.90.3.71:8262
   	Start instance 10.90.3.135:8262 success
   	Start instance 10.90.3.71:8262 success
   	Start instance 10.90.3.49:8262 success
   Starting component prometheus
   	Starting instance 10.90.1.7:9090
   	Start instance 10.90.1.7:9090 success
   Starting component grafana
   	Starting instance 10.90.1.7:3000
   	Start instance 10.90.1.7:3000 success
   Starting component alertmanager
   	Starting instance 10.90.1.7:9093
   	Start instance 10.90.1.7:9093 success
   Started cluster `dm-test` successfully
   ```

+ TiDB Data Migration クラスタの状態が稼働していることを確認します。

   ```bash
   tiup dm display dm-test
   ```

   ```output
   Starting component dm: /home/ec2-user/.tiup/components/dm/v1.16.0/tiup-dm display dm-test
   Cluster type:       dm
   Cluster name:       dm-test
   Cluster version:    vtidb_version
   Deploy user:        ec2-user
   SSH type:           builtin
   Grafana URL:        http://10.90.3.188:3000
   ID                Role          Host         Ports      OS/Arch       Status     Data Dir                      Deploy Dir
   --                ----          ----         -----      -------       ------     --------                      ----------
   10.90.3.188:9093  alertmanager  10.90.3.188  9093/9094  linux/x86_64  Up         /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.196:8261  dm-master     10.90.4.196  8261/8291  linux/x86_64  Healthy|L  /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
   10.90.4.196:8262  dm-worker     10.90.4.196  8262       linux/x86_64  Free       /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
   10.90.3.188:3000  grafana       10.90.3.188  3000       linux/x86_64  Up         -                             /tidb-deploy/grafana-3000
   10.90.3.188:9090  prometheus    10.90.3.188  9090       linux/x86_64  Up         /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   Total nodes: 5
   ```

### 手順 5. 移行タスクのソース設定を行う

+ タスクソースの設定を作成します。

   ```bash
   cat << EOF > mysql-source-conf1.yaml
   source-id: "mysql-replica-01"
   from:
     host: "${HOST_MYSQL1_PRIVATE_IP}"
     port: 3306
     user: "dm_user"
     password: "q1w2e3R4_"
   EOF
   cat mysql-source-conf1.yaml
   ```

   ```output
   source-id: "mysql-replica-01"
   from:
     host: "10.90.1.135"
     port: 3306
     user: "dm_user"
     password: "q1w2e3R4_"
   ```

+ MySQLインスタンスを移行ソースとして設定し、そのソースがTiDB Data Migrationクラスタによって認識できることを確認します。

   ```bash
   ~/.tiup/bin/tiup dmctl:vtidb_version \
    --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 operate-source create mysql-source-conf1.yaml
   ```

   ```output
   Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.66:8261 operate-source create mysql-source-conf1.yaml
   {
       "result": true,
       "msg": "",
       "sources": [
           {
               "result": true,
               "msg": "",
               "source": "mysql-replica-01",
               "worker": "dm-10.90.3.187-8262"
           }
       ]
   }
   ```

   ```bash
   ~/.tiup/bin/tiup dmctl:vtidb_version \
    --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 get-config source mysql-replica-01
   ```

   ```output
   Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.66:8261 get-config source mysql-replica-01
   {
       "result": true,
       "msg": "",
       "cfg": "enable: true\nenable-gtid: false\nauto-fix-gtid: false\nrelay-dir: relay-dir\nmeta-dir: \"\"\nflavor: mysql\ncharset: \"\"\nenable-relay: false\nrelay-binlog-name: \"\"\nrelay-binlog-gtid: \"\"\nsource-id: mysql-replica-01\nfrom:\n  host: 10.90.1.135\n  port: 3306\n  user: dm_user\n  password: '******'\n  max-allowed-packet: null\n  session: {}\n  security: null\npurge:\n  interval: 3600\n  expires: 0\n  remain-space: 15\nchecker:\n  check-enable: true\n  backoff-rollback: 5m0s\n  backoff-max: 5m0s\n  check-interval: 5s\n  backoff-min: 1s\n  backoff-jitter: true\n  backoff-factor: 2\nserver-id: 429530419\ntracer: {}\ncase-sensitive: false\nfilters: []\n"
   }
   ```

   ```bash
   ~/.tiup/bin/tiup dmctl:vtidb_version \
    --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 operate-source show
   ```

   ```output
   Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.66:8261 operate-source show
   {
       "result": true,
       "msg": "",
       "sources": [
           {
               "result": true,
               "msg": "",
               "source": "mysql-replica-01",
               "worker": "dm-10.90.3.187-8262"
           }
       ]
   }
   ```

### 手順 6. 移行タスクの設定を作成し、移行タスクを開始します

+ 移行タスクは、MySQLサーバーからTiDBクラスタにデータベース`sbtest`を移行します。

+ 移行タスク設定を生成します。

   ```bash
   cat << EOF > dm-task.yaml
   name: "dm-task"
   task-mode: all
   ignore-checking-items: ["auto_increment_ID"]

   target-database:
     host: "${HOST_DB1_PRIVATE_IP}"
     port: 4000
     user: "dm_user"
     password: "q1w2e3R4_"

   mysql-instances:
     - source-id: "mysql-replica-01"
       route-rules: ["instance-1-user-schema-rule"]
       block-allow-list: "rule_1"
       mydumper-config-name: "global"
       loader-config-name: "global"

   routes:
     instance-1-user-schema-rule:
       schema-pattern: "sbtest"
       target-schema: "sbtest"

   block-allow-list:
     rule_1:
       do-dbs: ["sbtest"]

   mydumpers:
     global:
       threads: 32
       chunk-filesize: 64

   loaders:
     global:
       pool-size: 8
       import-mode: "physical"
       pd-addr: "${HOST_PD1_PRIVATE_IP}:2379"
   EOF
   cat dm-task.yaml
   ```

   ```output
   name: "dm-task"
   task-mode: all
   ignore-checking-items: ["auto_increment_ID"]
   ...
   loaders:
     global:
       pool-size: 32
       import-mode: "physical"
   ```

+ 移行タスクを事前に確認してください。メッセージは `pre-check is passed` である必要があります。

   ```bash
   ~/.tiup/bin/tiup dmctl:vtidb_version \
    --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 check-task ./dm-task.yaml
   ```

   ```output
   Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.235:8261 check-task ./dm-task.yaml
   {
    "result": true,
    "msg": "pre-check is passed. "
   }
   ```

+ マイグレーションタスクを開始し、マイグレーションの進行状況を確認します。

+ マイグレーションタスクは、3つのサブタスクユニットで構成されます：`Dump`、`Load`、`Sync`。

   ```bash
   ~/.tiup/bin/tiup dmctl:vtidb_version \
    --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 start-task ./dm-task.yaml
   ```

   ```output
   Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.66:8261 start-task ./dm-task.yaml
   {
       "result": true,
       "msg": "",
       "sources": [
           {
               "result": true,
               "msg": "",
               "source": "mysql-replica-01",
               "worker": "dm-10.90.3.187-8262"
           }
       ],
       "checkResult": "pre-check is passed. "
   }
   ```

### 手順 7. 移行の進捗を確認する

+ `query-status` アクションを何度か使って、移行の進捗を確認します。`subTaskStatus` の `stage`、`unit`、`progress` 属性に注目してください。

+ `watch` コマンドを使用して、全体のタスクの進捗を監視します。演習環境の設定に基づいて（PoCではないため、ハンズオン演習には低スペックなサーバーを使用しています）、約8分かかる可能性があります（Dump: 4分、Load: 4分）。コーヒーを一杯飲む時間があります。

+ ユニット `Dump`、段階 `Running`

    ```bash
    watch -n 1 -d ./.tiup/bin/tiup dmctl:vtidb_version --master-addr=${HOST_MONITOR1_PRIVATE_IP}:8261 query-status ./dm-task.yaml
    ```

    ```output
    Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.235:8261 query-status ./dm-task.yaml
    {
    "result": true,
    "msg": "",
    "sources": [
      {
          "result": true,
          "msg": "",
          "sourceStatus": {
              "source": "mysql-replica-01",
              "worker": "dm-10.90.1.147-8262",
              "result": null,
              "relayStatus": null
          },
          "subTaskStatus": [
              {
                  "name": "dm-task",
                  "stage": "Running",
                  "unit": "Dump",
                  "result": null,
                  "unresolvedDDLLockID": "",
                  "dump": {
                      "totalTables": "6",
                      "completedTables": 3,
                      "finishedBytes": 7949200595,
                      "finishedRows": 39359277,
                      "estimateTotalRows": 59224808,
                      "bps": "57472325",
                      "progress": "64.63 %"
                  },
                  "validation": null
              }
          ]
      }
    ]
    }
    ```

+ ユニット`Load`、段階`Running`:

    ```output
    Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.196:8261 query-status ./dm-task.yaml
    {
     "result": true,
     "msg": "",
     "sources": [
         {
             "result": true,
             "msg": "",
             "sourceStatus": {
                 "source": "mysql-replica-01",
                 "worker": "dm-10.90.4.196-8262",
                 "result": null,
                 "relayStatus": null
             },
             "subTaskStatus": [
                 {
                     "name": "dm-task",
                     "stage": "Running",
                     "unit": "Load",
                     "result": null,
                     "unresolvedDDLLockID": "",
                     "load": {
                         "finishedBytes": "5307344924",
                         "totalBytes": "8161057816",
                         "progress": "65.03 %",
                         "metaBinlog": "(binlog.000008, 157240317)",
                         "metaBinlogGTID": "",
                         "bps": "62882221"
                     },
                     "validation": null
                 }
             ]
         }
     ]
    }
    ```

+ ユニット `Sync`、段階 `Running`。ターゲットTiDBとソースMySQLが同期されていることに注意してください。

    ```output
    Starting component dmctl: /home/ec2-user/.tiup/components/dmctl/vtidb_version/dmctl/dmctl --master-addr=10.90.4.196:8261 query-status ./dm-task.yaml
    {
        "result": true,
        "msg": "",
        "sources": [
            {
                "result": true,
                "msg": "",
                "sourceStatus": {
                    "source": "mysql-replica-01",
                    "worker": "dm-10.90.4.196-8262",
                    "result": null,
                    "relayStatus": null
                },
                "subTaskStatus": [
                    {
                        "name": "dm-task",
                        "stage": "Running",
                        "unit": "Sync",
                        "result": null,
                        "unresolvedDDLLockID": "",
                        "sync": {
                            "totalEvents": "19767046",
                            "totalTps": "21118",
                            "recentTps": "0",
                            "masterBinlog": "(binlog.000011, 706201359)",
                            "masterBinlogGtid": "",
                            "syncerBinlog": "(binlog.000011, 706201359)",
                            "syncerBinlogGtid": "00000000-0000-0000-0000-000000000000:0",
                            "blockingDDLs": [
                            ],
                            "unresolvedGroups": [
                            ],
                            "synced": true,
                            "binlogType": "remote",
                            "secondsBehindMaster": "0",
                            "blockDDLOwner": "",
                            "conflictMsg": "",
                            "totalRows": "19767046",
                            "totalRps": "21118",
                            "recentRps": "0"
                        },
                        "validation": null
                    }
                ]
            }
        ]
    }
    ```  

+ `Sync` ユニットが `"synced": true` に達した場合、<kbd>Ctrl</kbd>+<kbd>C</kbd> キーを押して `watch` コマンドを停止します。

### 手順 8. TiDB のストレージレイヤーのサイズ推定を確認する

+ TiDBで`sbtest`データベースのデータボリュームを取得する方法は3つあります。

+ 方法1: SQLを実行してデータサイズを取得します。結果には`sbtest`データベースのデータボリュームのみが含まれます。

  + MySQLに接続し、データサイズのクエリを実行します。

   ```bash
   mysql -h ${HOST_MONITOR1_PRIVATE_IP} -P 6000 -uroot
   ```

   ```output
   mysql>
   ```

  + `sbtest`データベースのすべてのテーブルを分析します。

   ```sql
   ANALYZE TABLE sbtest.sbtest1, sbtest.sbtest2, sbtest.sbtest3, sbtest.sbtest4, sbtest.sbtest5, sbtest.sbtest6;
   ```

   ```output
   Query OK, 0 rows affected, 6 warnings (22.93 sec)
   ```

  + システムテーブルを取得してデータサイズを確認します。

   ```sql
   SELECT db_name, SUM(approximate_size)/1024 size_GB
   FROM information_schema.TIKV_REGION_STATUS
   WHERE db_name='sbtest'
   GROUP BY db_name;
   ```

   ```output
   +---------+---------+
   | db_name | size_GB |
   +---------+---------+
   | sbtest  | 17.3232 |
   +---------+---------+
   1 row in set (0.01 sec)
   ```

+ 方法2：OSコマンドを使用してデータファイルのストレージを確認します。データボリュームの結果には、`sbtest`データベースだけでなく、他のメタデータも含まれています。

  + OSプロンプトに戻ります。

   ```sql
   EXIT
   ```

   ```output
   logout
   Connection to 10.90.1.132 closed.
   ```

+ TiKVノードのデータディレクトリを取得します。

   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```

   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    vtidb_version
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://34.217.88.220:2379/dashboard
   Grafana URL:        http://10.90.4.32:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.32:9093    alertmanager  10.90.4.32   9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.32:3000    grafana       10.90.4.32   3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.2.124:2379   pd            10.90.2.124  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.32:9090    prometheus    10.90.4.32   9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.176:4000   tidb          10.90.1.176  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.3.108:20160  tikv          10.90.3.108  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.32:20160   tikv          10.90.3.32   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.93:20160   tikv          10.90.3.93   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 8
   ```

  + TiKVのノードの1つに接続して、データのストレージサイズを計算します。

   ```bash
   ssh -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP}
   ```

   ```output
   Last login: Tue Jul 23 05:45:15 2024

   8 package(s) needed for security, out of 27 available
   Run "sudo yum update" to apply all updates.
   ```

   ```bash
   sudo du -h /tidb-data/tikv-20160/db --max-depth=0
   ```

   ```output
   6.0G	/tidb-data/tikv-20160/db
   ```

+ 方法 3: Grafanaをチェックしてデータボリュームを取得します。データボリュームの結果には、`sbtest`データベースだけでなく、他のメタデータも含まれています。

  + ユーザー名`admin`とパスワード`admin`を使用してGrafanaダッシュボードにログインし、`tidb-demo-TiKV-Summary - Server - Store size`を見つけます。

  + TiKV クラスタのデータサイズは`6.6GB x 3 = 19.8GB`であることが分かります。

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

ステップ8では、3つの方法全ての結果がモジュール1での推定値に近いです。
