## モジュール 3: Dumpling を使用して TiDB からデータをエクスポート

### 手順 1. TiDB に接続し、`emp` データベースのテーブルを確認

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
  ```

  ```sql
  SHOW DATABASES;
  ```

  ```output
  +--------------------+
  | Database           |
  +--------------------+
  | INFORMATION_SCHEMA |
  | METRICS_SCHEMA     |
  | PERFORMANCE_SCHEMA |
  | emp                |
  | mysql              |
  | test               |
  +--------------------+
  6 rows in set (0.00 sec)
  ```

  ```sql
  USE emp;
  ```

  ```output
  mysql> USE emp;
  Database changed
  ```

  ```sql
  SHOW TABLES;
  ```

  ```output
  mysql> SHOW TABLES;
  +---------------+
  | Tables_in_emp |
  +---------------+
  | dep           |
  | emp           |
  | sal           |
  | t             |
  +---------------+
  4 rows in set (0.00 sec)
  ```

  ```sql
  EXIT
  ```

  ```output
  mysql> EXIT
  Bye
  ```

### 手順 2. TiDB から `emp` データベースの `dept` テーブルをエクスポート

+ 以下はパラメータの説明です:
  + `-uroot`: ユーザーはroot。
  + `-P4000`: ポートは4000。
  + `-h ${HOST_DB1_PRIVATE_IP}`: TiDBのIPアドレスは`${HOST_DB1_PRIVATE_IP}`。
  + `--filetype sql`: データをSQLファイルにエクスポート。
  + `-t 8`: 同時に8スレッドを使用してエクスポート。
  + `-o /tmp/dept`: オプション `/tmp/dept` をエクスポートディレクトリとして指定。
  + `-r 200000`: 単一ファイルの最大行数200000を指定。
  + `-F 256MiB`: 単一ファイルの最大サイズ256 MiBを指定（ここでの単位はMiBです; 5GiBや8KBのような入力も許容されます）。

  ```bash
  tiup dumpling:v7.5.1 -uroot -P4000 -h ${HOST_DB1_PRIVATE_IP} --filetype sql -t 8 -o /tmp/dep -r 200000 -F 256MiB -T emp.dep
  ```

  ```output
  Release version: v7.5.1
  Git commit hash: 1a89decdb192cbdce6a7b0020d71128bc964d30f
  Git branch:      heads/refs/tags/v7.5.1
  Build timestamp: 2022-06-05 05:09:15Z
  Go version:      go version go1.18.2 linux/amd64

  [2022/11/08 15:09:37.456 +08:00] [INFO] [versions.go:55] ["Welcome to dumpling"] ["Release Version"=v7.5.1] ["Git Commit Hash"=1a89decdb192cbdce6a7b0020d71128bc964d30f] ["Git Branch"=heads/refs/tags/v7.5.1] ["Build timestamp"="2022-06-05 05:09:15"] ["Go Version"="go version go1.18.2 linux/amd64"]
  ...
  [2022/11/08 15:09:37.496 +08:00] [INFO] [main.go:80] ["dump data successfully, dumpling will exit now"]
  ```

### 手順 3. `/tmp/dep` ディレクトリの内容を確認

  ```bash
  ls /tmp/dep
  ```

  ```output
  emp.dep.0000000010000.sql  emp.dep-schema.sql  emp-schema-create.sql  metadata
  ```

### 手順 4. Dumpling の出力を確認

  ```bash
  cat /tmp/dep/emp-schema-create.sql
  ```

  ```output
  /*!40101 SET NAMES binary*/;
  CREATE DATABASE `emp` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
  ```

  ```bash
  cat /tmp/dep/emp.dep-schema.sql
  ```

  ```output
  /*!40101 SET NAMES binary*/;
  CREATE TABLE `dep` (
  `dept_no` char(4) NOT NULL,
  `dept_name` varchar(40) NOT NULL,
  PRIMARY KEY (`dept_no`) /*T![clustered_index] NONCLUSTERED */,
  UNIQUE KEY `dept_name` (`dept_name`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
  ```

  ```bash
  cat /tmp/dep/emp.dep.0000000010000.sql
  ```

  ```output
  /*!40101 SET NAMES binary*/;
  INSERT INTO `dep` VALUES
  ('d001','Marketing'),
  ('d002','Finance'),
  ('d003','Human Resources'),
  ('d004','Production'),
  ('d005','Development'),
  ('d006','Quality Management'),
  ('d007','Sales'),
  ('d008','Research'),
  ('d009','Customer Service');
  ```

  ```bash
  cat /tmp/dep/metadata
  ```

  ```output
  Started dump at: 2022-06-25 15:44:18
  SHOW MASTER STATUS:
        Log: tidb-binlog
        Pos: 434147965850615813
        GTID:

  Finished dump at: 2022-06-25 15:44:20
  ```

### 手順 5. TiDB から `emp` データベースをエクスポート

  ```bash
  tiup dumpling:v7.5.1 -uroot -P4000 -h ${HOST_DB1_PRIVATE_IP} --filetype sql -t 8 -o /tmp/emp -r 200000 -F 256MiB -B emp
  ```

  ```output
  Release version: v7.5.1
  Git commit hash: 1a89decdb192cbdce6a7b0020d71128bc964d30f
  Git branch:      heads/refs/tags/v7.5.1
  Build timestamp: 2022-06-05 05:09:15Z
  Go version:      go version go1.18.2 linux/amd64

  [2022/11/08 15:18:17.316 +08:00] [INFO] [versions.go:55] ["Welcome to dumpling"] ["Release Version"=v7.5.1] ["Git Commit Hash"=1a89decdb192cbdce6a7b0020d71128bc964d30f] ["Git Branch"=heads/refs/tags/v7.5.1] ["Build timestamp"="2022-06-05 05:09:15"] ["Go Version"="go version go1.18.2 linux/amd64"]
  ...
  [2022/11/08 15:18:19.398 +08:00] [INFO] [main.go:80] ["dump data successfully, dumpling will exit now"]
  ```

### 手順 6. `/tmp/emp` ディレクトリの内容を確認

  ```bash
  ls /tmp/emp
  ```

  ```output
  emp.dep.0000000010000.sql  emp.emp.0000000010000.sql  emp.sal.0000000010000.sql  emp.sal.0000000030000.sql  emp.sal-schema.sql     emp.t.0000000010000.sql  metadata
  emp.dep-schema.sql         emp.emp-schema.sql         emp.sal.0000000020000.sql  emp.sal.0000000040000.sql  emp-schema-create.sql  emp.t-schema.sql
  ```
