## 模块 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
  ```
