## Module 2: Importing the Data into TiDB Using TiDB Lightning

### Step 1. List the contents of the data files directory

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

### Step 2. Connect to the TiDB and drop the `emp` database

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

### Step 3. Read `solution-lightning-sql.toml`

+ The IP addresses shown below will be different from yours.

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

### Step 4. Import data into TiDB

+ Use a `nohup` command to start the `tidb-lightning` process (press <kbd>Enter</kbd> to make `tidb-lighting` work in the background).

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

### Step 5. View the output of 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
  ```
