## 模块 2：使用 TiDB Lightning 将数据导入 TiDB

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

  ```bash
  ls ./stage/TiDB-Administration-exercise-data-v2/exercise-dumpling
  ```

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

### 步骤 2. 连接到 TiDB 并删除 `emp` 数据库

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

```sql
SHOW DATABASES;
```

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

```sql
DROP DATABASE IF EXISTS emp;
```

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

```sql
EXIT
```

```output
mysql> EXIT
Bye
```

### 步骤 3. 查看 `solution-lightning-sql.toml`

+ 以下显示的 IP 地址可能会与你的不同。

  ```bash
  cat solution-lightning-sql.toml
  ```

  ```output
  [lightning]
  # Logging
  level = "info"
  file = "tidb-lightning.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-dumpling"

  # 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.*']

  [tidb]
  # Configuration of any TiDB server from the cluster.
  host = "10.90.2.124"
  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"
  ```

### 步骤 4. 将数据导入 TiDB

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

  ```bash
  nohup tiup tidb-lightning:v7.5.1 -config solution-lightning-sql.toml > nohup.out &
  ```

  ```output
  [1] 13135
  nohup: ignoring input and redirecting stderr to stdout
  ...
  [1]+  Done      nohup tidb-lightning:v7.5.1 -config solution-lightning-sql.toml > nohup.out
  ```

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

  ```bash
  cat nohup.out
  ```

  ```output
  Verbose debug logs will be written to tidb-lightning.log

  +----+------------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  # | CHECK ITEM                                                                                                                         | TYPE        | PASSED |
  +----+------------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  1 | Source csv files size is proper                                                                                                    | performance | true   |
  +----+------------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  ...
  +-----------------------------------------------------------------------------------------------------------------------------------------+-------------+--------+
  |  8 | Cluster available is rich, available is 51.79GiB, we need 1.007GiB                                                                 | 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
  ```
