# 实验 6：使用 TiDB Lightning 将数据导入 TiDB

## 概述
在本实验中，你将使用 `TiDB Lightning` 将数据导入 TiDB。以下展示了 TiDB 集群的部署拓扑图。

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


> [!TIP]
> 点击 **开始** 启动实验环境。
> 环境准备就绪后，点击底部工具栏中的 **前进** 进入 **模块 1**。
> 然后，按照实验指南的步骤连接到实践环境并开始你的任务。

## 模块 1：将 CSV 文件导入 TiDB

### 步骤 1：登录远程 Linux

+ 点击这里 - [终端 1](ssh://monitor1-ssh#1) 打开一个新的终端，连接到为你的实践实验分配的虚拟机。

+ 登录后显示的横幅：

    ```output
     ,     #_
    ~\_  ####_        Amazon Linux 2
    ~~  \_#####\
    ~~     \###|      
    ~~       \#/ ___
    ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
     ~~._.   _/
        _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
      _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
    ```

### 步骤 2. 为 Lightning 任务创建一个用户
   
  ```bash
  source hosts-env.sh
  ```

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} -uroot -P4000
  ```

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

  ```sql
  GRANT ALL PRIVILEGES ON *.* TO ltask@'%';
  ```
  ```output
  mysql> GRANT ALL PRIVILEGES ON *.* TO ltask@'%';
  Query OK, 0 rows affected (0.05 sec)
  ```

  ```sql
  EXIT;
  ```
  ```output
  mysql> EXIT;
  ```

### 步骤 3. 在 `monitor1` 上安装 `TiDB Lightning`

  ```bash
  tiup install tidb-lightning
  ```

### 步骤 4. 解压数据

  ```bash
  unzip -u stage/tidb-admin-dataset-v2.zip -d stage/
  ```
  ```output
  Archive:  tidb-admin-dataset-v2.zip
     creating: TiDB-Administration-exercise-data-v2/
     ...
  ```

### 步骤 5. 列出 CSV 数据目录中的文件

  ```bash
  ls ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-CSV-File
  ```
  ```output
  emp.sal.000000000.csv  emp.sal.000000002.csv  emp.sal.000000004.csv  emp.sal.000000006.csv  emp.sal.000000008.csv  emp.sal.000000010.csv
  emp.sal.000000001.csv  emp.sal.000000003.csv  emp.sal.000000005.csv  emp.sal.000000007.csv  emp.sal.000000009.csv  metadata
  ```

### 步骤 6. 查看 `emp.sal.000000000.csv` 文件

  ```bash
  head -n 10 ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-CSV-File/emp.sal.000000000.csv
  ```
  ```output
  "emp_no","salary","from_date","to_date"
  10001,60117,"1986-06-26","1987-06-26"
  10001,62102,"1987-06-26","1988-06-25"
  10001,66074,"1988-06-25","1989-06-25"
  10001,66596,"1989-06-25","1990-06-25"
  10001,66961,"1990-06-25","1991-06-25"
  10001,71046,"1991-06-25","1992-06-24"
  10001,74333,"1992-06-24","1993-06-24"
  10001,75286,"1993-06-24","1994-06-24"
  10001,75994,"1994-06-24","1995-06-24"
  ```

  ```bash
  wc -l ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-CSV-File/emp.sal.000000000.csv
  ```
  ```output
  275696 /home/ec2-user/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-CSV-File/emp.sal.000000000.csv
  ```

### 步骤 7. 查看 lightning 的配置文件

+ 查看 `solution-lightning-csv.toml`。下面显示的 IP 地址可能会与你的不同。
  
  ```bash
  cd
  ```

  ```bash
  cat solution-lightning-csv.toml
  ```
  ```output
  [lightning]

  # The concurrency number of data. It is set to the number of logical CPU
  # cores by default. When deploying together with other components, you can
  # set it to 75% of the size of logical CPU cores to limit the CPU usage.
  # region-concurrency = 1

  # Logging
  level = "info"
  file = "tidb-lightning-csv.log"


  [tikv-importer]
  # Sets the backend to the "local" mode.
  backend = "local"
  # Sets the directory of temporary local storage.
  sorted-kv-dir = "/tmp/sorted-kv-dir"

  [mydumper]
  # Local source data directory
  data-source-dir = "/home/ec2-user/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-CSV-File"
  # Configures the wildcard rule. By default, all tables in the mysql, sys, INFORMATION_SCHEMA, PERFORMANCE_SCHEMA, METRICS_SCHEMA, and INSPECTION_SCHEMA system databases are filtered.
  # If this item is not configured, the "cannot find schema" error occurs when system tables are imported.
  filter = ['*.*', '!mysql.*', '!sys.*', '!INFORMATION_SCHEMA.*', '!PERFORMANCE_SCHEMA.*', '!METRICS_SCHEMA.*', '!INSPECTION_SCHEMA.*']

  [[mydumper.files]]
  # Import only 00001~05000 data files and ignore other files
  pattern = '(?i)(?:[/]*/)*emp\.sal\.(00000000[0-1]|000000001)\.csv'
  schema = "emp"
  table = "sal"
  type = "csv"

  [mydumper.csv]
  # Separator between fields. Must not be empty.
  separator = ','
  # Quoting delimiter. Empty value means no quoting.
  delimiter = '"'
  # Line terminator. Empty value means both "\n" (LF) and "\r\n" (CRLF) are line terminators.
  terminator = ""
  # Whether the CSV files contain a header.
  # If `header` is true, the first line will be skipped.
  header = true
  # Whether the CSV contains any NULL value.
  # If `not-null` is true, all columns from CSV cannot be NULL.
  not-null = false
  # When `not-null` is false (that is, CSV can contain NULL),
  # fields equal to this value will be treated as NULL.
  null = '\N'
  # Whether to interpret backslash escapes inside fields.
  backslash-escape = true
  # If a line ends with a separator, remove it.
  trim-last-separator = false

  [tidb]
  # Configuration of any TiDB server from the cluster.
  host = "<HOST_DB1_PRIVATE_IP>"
  port = 4000
  user = "ltask"
  password = "q1w2e3R4_"
  # Table schema information is fetched from TiDB via this status-port.
  status-port = 10080
  # Address of any PD server from the cluster.
  pd-addr = "10.90.2.84:2379"
  ```

### 步骤 8. 连接到 TiDB

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

### 步骤 9. 准备 schema

  ```sql
  DROP DATABASE IF EXISTS emp;
  ```
  ```output
  mysql> DROP DATABASE IF EXISTS emp;
  Query OK, 0 rows affected (0.21 sec)
  ```

  ```sql
  CREATE DATABASE emp;
  ```
  ```output
  mysql> CREATE DATABASE emp;
  Query OK, 0 rows affected (0.07 sec)
  ```

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

  ```sql
  CREATE TABLE `sal` ( `emp_no` INT(11) NOT NULL, `salary` INT(11) NOT NULL, `from_date` DATE NOT NULL, `to_date` DATE NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
  ```
  ```output
  mysql> CREATE TABLE `sal` ( `emp_no` INT(11) NOT NULL, `salary` INT(11) NOT NULL, `from_date` DATE NOT NULL, `to_date` DATE NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
  Query OK, 0 rows affected (0.08 sec)
  ```

  ```sql
  SELECT COUNT(*) FROM emp.sal;
  ```
  ```output
  mysql> SELECT COUNT(*) FROM emp.sal;
  +----------+
  | COUNT(*) |
  +----------+
  |        0 |
  +----------+
  1 row in set (0.00 sec)
  ```

  ```sql
  EXIT;
  ```
  ```output
  mysql> EXIT;
  ```

### 步骤 10. 使用 Lightning 导入数据

+ 使用 `nohup` 命令启动 `tidb-lightning` 进程。（你可以按 <kbd>Enter</kbd> `tidb-lighting` 在后台运行）：

  ```bash
  nohup tiup tidb-lightning -config solution-lightning-csv.toml > nohup.out &
  ```
  ```output
  [1] 17481
  nohup: ignoring input and redirecting stderr to stdout
  ...
  [1]+  Done      nohup tiup tidb-lightning -config solution-tidb-lightning-csv.toml > nohup.out
  ```

### 步骤 11. 查看 Lightning 的输出

+ 查看 `nohup.out` 文件的内容，以验证 `TiDB Lightning` 已退出：

  ```bash
  cat nohup.out
  ```
  ```output
  Verbose debug logs will be written to tidb-lightning-csv.log

     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  # | CHECK ITEM                                                                                                            | TYPE        | PASSED |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  1 | Source csv files size is proper                                                                                       | performance | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  2 | checkpoints are valid                                                                                                 | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  3 | table schemas are valid                                                                                               | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  4 | Cluster is available                                                                                                  | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  5 | Lightning has the correct storage permission                                                                          | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  6 | sorted-kv-dir:/tmp/sorted-kv-dir and data-source-dir:/home/ec2-user/sample/csv are in the same disk, may slow down performance | performance | false  |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  7 | local disk resources are rich, estimate sorted data size 20.53MiB, local available is 5.15GiB                         | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  8 | Cluster available is rich, available is 54.48GiB, we need 61.58MiB                                                    | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     |  9 | Cluster doesn't have too many empty regions                                                                           | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+
     | 10 | Cluster region distribution is balanced                                                                               | critical    | true   |
     +----+-----------------------------------------------------------------------------------------------------------------------+-------------+--------+

  tidb lightning exit successfully
  ```

+ 如果发生任何错误，查看 `tidb-lightning-csv.log` 文件的内容，以确定错误原因：

  ```bash
  less tidb-lightning-csv.log
  ```

+ 你可能需要在再次导入数据之前删除 lightning checkpoint 检查点。以下命令假设检查点存储在 `/tmp/` 中。

  ```bash
  rm /tmp/tidb_lightning_checkpoint.pb
  ```

### 步骤 12. 在 TiDB 中查看数据

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

  ```sql
  SELECT COUNT(*) FROM emp.sal;
  ```
  ```output
  mysql> SELECT COUNT(*) FROM emp.sal;
  +----------+
  | COUNT(*) |
  +----------+
  |   551395 |
  +----------+
  1 row in set (0.20 sec)
  ```

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

## 模块 2：并行将数据导入 TiDB

+ 在本练习中，你将使用 `TiDB Lightning` 并行地将多个表导入 TiDB。下图展示了如何使用 `TiDB Lightning` 并行地将分片数据库和表导入 TiDB。在我们的示例中，`my_table_1` 包含所有 ID 为奇数的记录，而 `my_table_2` 包含所有 ID 为偶数的记录

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

### 步骤 1. 列出数据文件目录的文件

  ```bash
  ls ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_1
  ```
  ```output
  metadata  my_db.my_table_1.000000000.sql  my_db.my_table_1-schema.sql  my_db-schema-create.sql
  ```

### 步骤 2. 检查数据源文件

+ 查看 `my_db-schema-create.sql`, `my_db.my_table_1-schema.sql` 和 `my_db.my_table_1.000000000.sql` 文件，了解它们的结构。

  
  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_1/my_db-schema-create.sql
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
  ```

  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_1/my_db.my_table_1-schema.sql 
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  CREATE TABLE `my_table_1` (
  `id` int(11) NOT NULL,
  `name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  ```

  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_1/my_db.my_table_1.000000000.sql
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  > INSERT INTO `my_table_1` VALUES
  (1,'tom'),
  (3,'frank');
  ...
  ```

### 步骤 3. 打开一个新终端（终端 2）

+ 点击这里 - [终端 2](ssh://monitor1-ssh#2) 打开一个新的终端，连接到为你的实践实验分配的虚拟机。

+ 登录后显示的横幅：

    ```output
     ,     #_
    ~\_  ####_        Amazon Linux 2
    ~~  \_#####\
    ~~     \###|      
    ~~       \#/ ___
    ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
     ~~._.   _/
        _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
      _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
    ```

### 步骤 4. 在终端 2 中，登录到 `PD1`

  ```bash
  ./ssh-to-pd1.sh  
  ```
  ```output
  ...
     ,     #_
     ~\_  ####_        Amazon Linux 2
     ~~  \_#####\
     ~~     \###|       
     ~~       \#/ ___
     ~~       V~' '->
        ~~~         /    A newer version of Amazon Linux is available!
        ~~._.   _/
           _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
           _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
  ...
  ```

### 步骤 5. 在终端 2 中，解压数据

  ```bash
  unzip -u stage/tidb-admin-dataset-v2.zip -d stage/
  ```
  ```bash
  ls ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_2
  ```
  ```output
  metadata
  my_db.my_table_2.000000000.sql
  my_db.my_table_2-schema.sql
  my_db-schema-create.sql
  ```

### 步骤 6. 在终端 2 中，检查数据文件

  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_2/my_db-schema-create.sql
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
  ```

  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_2/my_db.my_table_2-schema.sql
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  CREATE TABLE `my_table_2` (
  `id` int(11) NOT NULL,
  `name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  ```

  ```bash
  cat ~/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_2/my_db.my_table_2.000000000.sql
  ```
  ```output
  /*!40101 SET NAMES binary*/;
  INSERT INTO `my_table_2` VALUES
  (2,'jack'),
  (4,'tony');
  ...
  ```

### 步骤 7. 在终端 1 中，查看 `solution-lightning-p1.toml`. 

+ 下面显示的 IP 地址可能会与你的不同。
  
  ```bash
  cat solution-lightning-p1.toml
  ```
  ```output
  [lightning]

  # The concurrency number of data. It is set to the number of logical CPU
  # cores by default. When deploying together with other components, you can
  # set it to 75% of the size of logical CPU cores to limit the CPU usage.
  # region-concurrency = 1

  # Logging
  level = "info"
  file = "tidb-lightning-p1.log"

  [tikv-importer]
  # Whether to allow importing data to tables with data. The default value is `false`.
  # When you use parallel import mode, you must set it to `true`, because multiple TiDB Lightning instances are importing the same table at the same time.
  incremental-import = true
  # Sets the backend to the "local" mode.
  backend = "local"
  # Sets the directory of temporary local storage.
  sorted-kv-dir = "/tmp/sorted-kv-dir"

  [mydumper]
  # Local source data directory
  data-source-dir = "/home/ec2-user/stage/TiDB-Administration-exercise-data-v2/exercise-lightning/Importing-Data-into-TiDB-in-Parallel/my_table_1"
  # Only import tables if these wildcard rules are matched. See the corresponding section for details.
  filter = ['*.*', '!mysql.*', '!sys.*', '!INFORMATION_SCHEMA.*', '!PERFORMANCE_SCHEMA.*', '!METRICS_SCHEMA.*', '!INSPECTION_SCHEMA.*']

  [[routes]]
  schema-pattern = "my_db"
  table-pattern = "my_table_*"
  target-schema = "my_db"
  target-table = "my_table"

  [tidb]
  # Configuration of any TiDB server from the cluster.
  host = "<HOST_DB1_PRIVATE_IP>"
  port = 4000
  user = "ltask"
  password = "q1w2e3R4_"
  # Table schema information is fetched from TiDB via this status-port.
  status-port = 10080
  # Address of any PD server from the cluster.
  pd-addr = "10.90.2.84:2379"                   
  ``` 
  
### 步骤 8. 在终端 2 中，安装 `TiUP` 和 `Lightning`

  ```bash
  ./01-install-tiup.sh
  ```
  ```bash
  source .bash_profile
  ```
  ```bash
  tiup mirror set tidb-community-server-7.5.1-linux-amd64
  tiup install tidb-lightning
  ```

### 步骤 9. 在终端 1 中，将 `solution-lightning-p2.toml` 文件复制到 `pd1`

  ```bash
  scp -i ~/.ssh/pe-class-key-oregon.pem solution-lightning-p2.toml ec2-user@${HOST_PD1_PRIVATE_IP}:/home/ec2-user/
  ```
  ```output
  solution-lightning-p2.toml                                                              100%  720   678.5KB/s   00:00
  ```

### 步骤 10. 在终端 1 中，连接到 TiDB 并删除 `my_db` 数据库

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

  ```bash
  DROP DATABASE IF EXISTS my_db;
  ```
  ```output
  mysql> DROP DATABASE IF EXISTS my_db;
  ```

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

### 步骤 11. 在终端 1 中，使用 Lightning 导入数据

+ 使用 `nohup` 命令启动 `tidb-lightning` 进程（按 <kbd>Enter</kbd> 让 `tidb-lighting` 在后台运行）。
+ 请同时执行步骤 11 和步骤 12。
  
  ```bash
  nohup tiup tidb-lightning -config solution-lightning-p1.toml > nohup.out &
  ```
  ```output
  [1] 4256
  # nohup: ignoring input and redirecting stderr to stdout 
  ...

  [1]+  Done                    nohup tidb-lightning -config solution-lightning-p1.toml > nohup
  ```

### 步骤 12. 在终端 2 中，使用 Lightning 导入数据

  ```bash
  nohup .tiup/bin/tiup tidb-lightning -config solution-lightning-p2.toml > nohup.out &
  ```
  ```output
  [1] 4256
  # nohup: ignoring input and redirecting stderr to stdout
  ...

  [1]+  Done                    nohup tidb-lightning -config solution-lightning-p2.toml > nohup
  ```

### 步骤 13. 在两个终端中，查看 Lightning 的输出

+ 在[终端 1](ssh://monitor1-ssh#1)中，查看 `nohup.out` 文件的内容，以验证 `TiDB Lightning` 已成功退出。 ，然后输入 `:q!` 退出终端：
  
  ```bash
  cat nohup.out
  ```
  ```output
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  # | CHECK ITEM                                                                                                                       | TYPE        | PASSED |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  1 | Source csv files size is proper                                                                                                  | performance | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  2 | checkpoints are valid                                                                                                            | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  3 | table schemas are valid                                                                                                          | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  4 | Cluster is available                                                                                                             | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  5 | Lightning has the correct storage permission                                                                                     | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  6 |sorted-kv-dir:/tmp/sorted-kv-dir and data-source-dir:/home/ec2-user/sample/sql/my_table_1 are in the same disk, may slow down performance  | performance | false  |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  7 | local disk resources are rich, estimate sorted data size 85B, local available is 5.149GiB                                        | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  8 | Cluster available is rich, available is 54.46GiB, we need 255B                                                                   | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  9 | Cluster doesn't have too many empty regions                                                                                      | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  | 10 | Cluster region distribution is balanced                                                                                          | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  tidb lightning exit successfully
  ```

+ 如果发生任何错误，查看 `tidb-lightning-p1.log` 文件的内容，以确定错误原因。

  ```bash
  cat tidb-lightning-p1.log
  ```

+ 你可能需要在再次导入数据之前删除 lightning 检查点。以下命令假设检查点存储在 `/tmp/` 中。

  ```bash
  rm /tmp/tidb_lightning_checkpoint.pb
  ```

+ 在[终端 2](ssh://monitor1-ssh#2)中，查看 `nohup.out` 文件的内容，以验证 `TiDB Lightning` 已成功退出。
  
  ```bash
  cat nohup.out
  ```
  ```output
  Verbose debug logs will be written to tidb-lightning-p2.log

  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  # | CHECK ITEM                                                                                                                       | TYPE        | PASSED |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  1 | Source csv files size is proper                                                                                                  | performance | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  2 | checkpoints are valid                                                                                                            | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  3 | table schemas are valid                                                                                                          | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  4 | Cluster is available                                                                                                             | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  5 | Lightning has the correct storage permission                                                                                     | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  6 |sorted-kv-dir:/tmp/sorted-kv-dir and data-source-dir:/home/ec2-user/sample/sql/my_table_2 are in the same disk, may slow down performance  | performance | false  |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  7 | local disk resources are rich, estimate sorted data size 85B, local available is 7.173GiB                                        | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  8 | Cluster available is rich, available is 54.46GiB, we need 255B                                                                   | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  9 | Cluster doesn't have too many empty regions                                                                                      | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  | 10 | Cluster region distribution is balanced                                                                                          | critical    | true   |
  +----+----------------------------------------------------------------------------------------------------------------------------------+-------------+--------+

  tidb lightning exit successfully
  ```

### 步骤 14. 在终端 1 中，连接到 TiDB 并查询 `emp.sal` table

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
  ```
  ```bash
  SELECT COUNT(*) FROM my_db.my_table;
  ```
  ```output
  mysql> SELECT COUNT(*) FROM my_db.my_table;
  +----------+
  | COUNT(*) |
  +----------+
  |    40156 |
  +----------+
  1 row in set (0.01 sec)
  ```

## 实验结束