## 模块 1: TiDB 集群初始化

在本模块中，参与者将部署一个 TiDB 集群。

### 步骤 1. 连接到 TiUP 控制机

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

### 步骤 2. 创建一个新 TiDB 集群

+ 以下代码块中的命令将按顺序执行一系列操作：1. 更改 [TiUP mirror](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) 设置，2. 检查系统设置，3. 修复失败的检查，4. 部署 TiDB 集群，最后 5. 启动 TiDB 集群。大约需要 3 分钟完成。

  ```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-${LAB:TIDB_VERSION}-linux-amd64
  /home/ec2-user/tidb-community-server-${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
  ```

### 步骤 3. 初始化数据库

+ 连接到 TiDB。

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

  ```output
  mysql> 
  ```

+ 设置 `tidb_gc_life_time` 为 2 小时(默认为 10 分钟)，保证 MVCC 快照在 2 小时内不会被删除。

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

+ 查看 TiDB 集群与 Time to Live (TTL) 相关的默认配置，会发现:
  + `tidb_ttl_job_enable` 为 `true`，表示 Time to Live (TTL) 功能默认开启。
  + `tidb_ttl_job_schedule_window_start_time` 为 `00:00 +0000`，`tidb_ttl_job_schedule_window_end_time` 为 `23:59 +0000` 表示 Time to Live (TTL) 功能默认全天可以运行。

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

+ 断开客户端连接。

  ```sql
  EXIT
  ```

  ```output
  mysql> EXIT
  Bye
  ```

+ 到此为止，TiDB 集群已经准备完毕，可以开始进行 TTL 功能的学习了。
