## Module 1: Initialization of TiDB Cluster

In this module, participants will deploy a TiDB cluster.

### Step 1. Connect to the TiUP control machine

+ Click here - [Terminal 1](ssh://monitor1-ssh#1) to open a new terminal connected to the virtual machine assigned for your hands-on lab.

### Step 2. Create a new TiDB cluster

+ The commands in the following code block will execute a series of operations in sequence. Which are: 1. Change [TiUP mirror](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) setting, 2. Check system settings, 3. Fix the failed checks, 4. Deploy the TiDB cluster, and finally 5. Start the TiDB cluster. It takes approximately 3 minutes to complete.

  ```bash
  export PATH="$PATH:/home/ec2-user/.tiup/bin"
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --apply
  tiup cluster deploy tidb-demo v${LAB:TIDB_VERSION} ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --yes
  tiup cluster start tidb-demo
  sleep 5;
  tiup cluster display tidb-demo
  ```

  ```output
  Successfully set mirror to /home/ec2-user/tidb-community-server-v${LAB:TIDB_VERSION}-linux-amd64
  /home/ec2-user/tidb-community-server-v${LAB:TIDB_VERSION}-linux-amd64
  ...
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://52.27.45.10:2379/dashboard
  Grafana URL:        http://10.90.4.40:3000
  ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----                            -------       ------  --------                      ----------
  10.90.4.40:9093    alertmanager  10.90.4.40   9093/9094                        linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.40:3000    grafana       10.90.4.40   3000                             linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.51:2379    pd            10.90.2.51   2379/2380                        linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.40:9090    prometheus    10.90.4.40   9090/12020                       linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.2.14:4000    tidb          10.90.2.14   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.2.43:4000    tidb          10.90.2.43   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.4.243:9000   tiflash       10.90.4.243  9000/3930/20170/20292/8234/8123  linux/x86_64  Down    /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
  10.90.1.208:20160  tikv          10.90.1.208  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.30:20160   tikv          10.90.1.30   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.93:20160   tikv          10.90.1.93   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.4.154:6000   tiproxy       10.90.4.154  6000/3080                        linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  Total nodes: 11
  ```

> [!TIP]
> If you find any components in a `Down` state, wait a few seconds and run the display command again.

### Step 3. Initialize TiDB Cluster

+ Connect to TiDB cluster.

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

  ```output
  mysql> 
  ```

+ Because the default value of `tidb_gc_life_time` is 10m, so we set `tidb_gc_life_time` to 2 hours to ensure MVCC snapshots are preserved and not garbage collected within this timeframe.

  ```sql
  SET GLOBAL tidb_gc_life_time='120m';
  SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';
  ```

  ```output
  mysql> SET GLOBAL tidb_gc_life_time='120m';
  Query OK, 0 rows affected (0.03 sec)

  mysql> SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';
  +-------------------+--------+
  | Variable_name     | Value  |
  +-------------------+--------+
  | tidb_gc_life_time | 2h0m0s |
  +-------------------+--------+
  1 row in set (0.01 sec)
  ```

+ Check the default setting about TTL, you will find:
  + `tidb_ttl_job_enable` set to `true` indicates that the Time to Live (TTL) feature is enabled by default.
  + `tidb_ttl_job_schedule_window_start_time` is set to `00:00 +0000`, and `tidb_ttl_job_schedule_window_end_time` is set to `23:59 +0000`, which means that the Time to Live (TTL) feature can run throughout the entire day by default.

  ```sql
  SHOW GLOBAL VARIABLES LIKE 'tidb_ttl%';
  ```

  ```output
  mysql> SHOW GLOBAL VARIABLES LIKE 'tidb_ttl%';
  +-----------------------------------------+-------------+
  | Variable_name                           | Value       |
  +-----------------------------------------+-------------+
  | tidb_ttl_delete_batch_size              | 100         |
  | tidb_ttl_delete_rate_limit              | 0           |
  | tidb_ttl_delete_worker_count            | 4           |
  | tidb_ttl_job_enable                     | ON          |
  | tidb_ttl_job_schedule_window_end_time   | 23:59 +0000 |
  | tidb_ttl_job_schedule_window_start_time | 00:00 +0000 |
  | tidb_ttl_running_tasks                  | -1          |
  | tidb_ttl_scan_batch_size                | 500         |
  | tidb_ttl_scan_worker_count              | 4           |
  +-----------------------------------------+-------------+
  9 rows in set (0.00 sec)
  ```

+ Disconnect the client session.

  ```sql
  EXIT
  ```

  ```output
  mysql> EXIT
  Bye
  ```
