## 模块 4：使用 Dumpling 从 MySQL 导出数据

### 步骤 1. 通过 SSH 连接到 `PD3`

  ```bash
  ./ssh-to-pd3.sh
  ```

  ```output
  The authenticity of host '10.80.0.215 (10.80.0.215)' can't be established.
  ...

        __|  __|_  )
        _|  (     /   Amazon Linux 2 AMI
     ___|\___|___|

  https://aws.amazon.com/amazon-linux-2/
  ```

### 步骤 2. 启动 MySQL server 实例

  ```bash
  sudo service mysqld start
  ```

### 步骤 3. 在 PD3 上解压数据

  ```bash
  unzip -u stage/tidb-admin-dataset-v2.zip -d stage/
  ```

### 步骤 4. 登录 MySQL Server

  ```bash
  mysql -u root -p -h 127.0.0.1 -P 3306 -pq1w2e3R4_
  ```

### 步骤 5. 为导出任务创建一个用户

  ```sql
  CREATE USER exp@'%' IDENTIFIED BY 'q1w2e3R4_';
  ```

  ```output
  mysql> CREATE USER exp@'%' IDENTIFIED BY 'q1w2e3R4_';
  Query OK, 0 rows affected (0.00 sec)
  ```

  ```sql
  GRANT ALL PRIVILEGES on *.* to exp@'%';
  ```

  ```output
  mysql> GRANT ALL PRIVILEGES on *.* to exp@'%';
  Query OK, 0 rows affected (0.00 sec)
  ```

### 步骤 6. 将数据导入 MySQL

   ```sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp-schema-create.sql
   ```

   ```sql
   USE emp;
   ```

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

   ```sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.dep-schema.sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.dep.0000000010000.sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.t-schema.sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.t.0000000010000.sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.emp-schema.sql
   SOURCE ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling/emp.emp.0000000010000.sql
   ```

   ```output
   ...
   Query OK, 16843 rows affected (0.14 sec)
   Records: 16843  Duplicates: 0  Warnings: 0

   Query OK, 12161 rows affected (0.10 sec)
   Records: 12161  Duplicates: 0  Warnings: 0
   ```

### 步骤 7. 查看 MySQL 中 `emp` 数据库的表

  ```sql
  SHOW TABLES;
  ```

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

### 步骤 8. 返回 `monitor1`

  ```sql
  EXIT
  ```

  ```output
  mysql> EXIT;
  Bye
  ```

  ```bash
  exit
  ```

  ```output
  logout
  Connection to 10.90.1.78 closed.
  ```

### 步骤 9. 从 MySQL 导出 `emp` 数据库

  ```bash
  tiup dumpling:v7.5.1 -uexp -P 3306 -h ${HOST_PD3_PRIVATE_IP} -pq1w2e3R4_ --filetype sql -t 8 -o /tmp/empmysql -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"]
  ```

### 步骤 10. 查看输出目录中的文件

  ```bash
  ls /tmp/empmysql
  ```

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