# 演習 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. 仮想マシンにログイン

+ ここをクリック - [ターミナル 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. スキーマの準備

  ```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-lightning` をバックグラウンドで動作させます):

  ```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 の出力の表示

+ `TiDB Lightning` が終了していることを確認するために `nohup.out` ファイルの内容を表示します。:

  ```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 チェックポイントを削除する必要があるかもしれません。以下のコマンドは、チェックポイントが `/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
```

+ データを再度インポートする前に、ライトニングのチェックポイントを削除する必要があるかもしれません。次のコマンドは、チェックポイントが`/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` テーブルをクエリします

  ```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)
  ```

## 演習終了