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

在本模块中，参与者将部署一个 TiDB 集群，并创建三个 PLACEMENT POLICY 供后面的模块使用。本模块中会用到 Placement Rules in SQL 特性，关于 Placement Rules in SQL 特性请参考：<https://docs.pingcap.com/zh/tidb/stable/placement-rules-in-sql/#placement-rules-in-sql>

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

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

### 步骤 2. 检查 TiDB 集群拓扑文件

+ 检查提供的拓扑文件 `nine-nodes.yaml`，该文件用于部署 TiDB 集群，找到 `server_configs` 下的 `pd`，发现 `replication.location-labels` 设置为 `["region","host"]` 表示该 TiDB 集群定义了 2 层拓扑结构，依次是 `region` 和 `host`。

  ```bash
  cat nine-nodes.yaml
  ```

  ```output
  ...
    arch: "amd64"
  server_configs:
    pd:
      replication.location-labels: ["region","host"]
      replication.max-replicas: 3
  pd_servers:
  ...
  ```

+ 检查提供的拓扑文件 `nine-nodes.yaml`，找到 `tikv_servers` 下 3 个 TiKV 节点的 `config` 下的 `server.labels`，发现 3 个 TiKV 节点被设置了 3 个不同的标签，分别为 `region1`, `region2` 和 `region3`。

  ```output
  ...
  tikv_servers:
    - host: 10.90.1.136
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region1", host: "tikv1" }
        ...
    - host: 10.90.1.54
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region2", host: "tikv2" }
        ...
    - host: 10.90.1.84
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region3", host: "tikv3" }
  ...
  ```

+ 检查提供的拓扑文件 `nine-nodes.yaml`，找到 `tikv_servers` 下 3 个 TiKV 节点的 `config` 下的 `in-memory-engine.` 为前缀的项目，发现：
  + `region1` 的 TiKV 节点的 `in-memory-engine.enable` 为 `false`, `region2` 和 `region3` 的 TiKV 节点的 `in-memory-engine.enable` 为 `true`， 说明 `region1` 的 TiKV 节点没有开启 MVCC In-Memory Engine 功能， 而 `region2` 和 `region3` 的 TiKV 节点开启了 In-Memory Engine 功能。
  + 在开启了 In-Memory Engine 功能的情况下，`region1`，`region2` 和 `region3` 的 TiKV 的 `in-memory-engine.capacity` 均为 `1GiB`， 表示 In-Memory Engine 可使用的内存大小均为 `1GiB`。
  + 在开启了 In-Memory Engine 功能的情况下，`in-memory-engine.mvcc-amplification-threshold` 表示选取加载 Region 时 MVCC 读放大的阈值，`region1` 和 `region2` 的 TiKV 均设置为 `5`， `region3` 的 TiKV 设置为 `100`。
  + 在开启了 In-Memory Engine 功能的情况下，`region1`，`region2` 和 `region3` 的 TiKV 的 `in-memory-engine.load-evict-interval` 为 `2m`， 表示 In-Memory Engine 根据请求统计信息自动选取加载数据的时间间隔为 2 分钟。

  ```output
  ...
  tikv_servers:
    - host: 10.90.1.136
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region1", host: "tikv1" }
        in-memory-engine.capacity: 1GiB
        in-memory-engine.enable: false
        in-memory-engine.mvcc-amplification-threshold: 5
        in-memory-engine.load-evict-interval: 2m
    - host: 10.90.1.54
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region2", host: "tikv2" }
        in-memory-engine.capacity: 1GiB
        in-memory-engine.enable: true
        in-memory-engine.mvcc-amplification-threshold: 5
        in-memory-engine.load-evict-interval: 2m
    - host: 10.90.1.84
      port: 20160
      status_port: 20180
      config:
        server.labels: { region: "region3", host: "tikv3" }
        in-memory-engine.capacity: 1GiB
        in-memory-engine.enable: true
        in-memory-engine.mvcc-amplification-threshold: 100
        in-memory-engine.load-evict-interval: 2m
  ...
  ```

+ 根据提供的拓扑文件 `nine-nodes.yaml`，可以得到关于 TiKV 集群的拓扑图，如下：

  ![IME_Topology.png 70%](https://lab-static.pingcap.com/images/2025/8/18/ab10f088aef5e642b32818f086bb4650822e3622.png)

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

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

  ```bash
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check nine-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --apply
  tiup cluster deploy tidb-demo ${LAB:TIDB_VERSION} ./nine-nodes.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
  A new version of cluster is available:  -> v1.16.1

      To update this component:   tiup update cluster
      To update all components:   tiup update --all

  The component `cluster` version  is not installed; downloading from repository.
  ...
  [ec2-user@ip-10-90-4-47 ~]$ tiup cluster display tidb-demo
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v8.5.1
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://54.250.180.235:2379/dashboard
  Grafana URL:        http://10.90.4.47:3000
  ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----        -------       ------  --------                      ----------
  10.90.4.47:9093    alertmanager  10.90.4.47   9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.47:3000    grafana       10.90.4.47   3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.129:2379   pd            10.90.2.129  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.203:2379   pd            10.90.2.203  2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.254:2379   pd            10.90.2.254  2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.47:9090    prometheus    10.90.4.47   9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.1.194:4000   tidb          10.90.1.194  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.40:4000    tidb          10.90.1.40   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.3.147:20160  tikv          10.90.3.147  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.3.49:20160   tikv          10.90.3.49   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.3.51:20160   tikv          10.90.3.51   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  Total nodes: 11
  ```

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

+ 连接到 TiDB。

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

  ```output
  mysql> 
  ```

+ 设置 `tidb_gc_life_time` 为 24 小时，保证 MVCC 快照在 24 小时内不会被删除。

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

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

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

+ 查看各个 TiKV 节点的 In-Memory Engine 功能是否开启。

  ```sql
  SHOW CONFIG WHERE type='tikv' AND name='in-memory-engine.enable';
  ```

  ```output
  mysql> SHOW CONFIG WHERE type='tikv' AND name='in-memory-engine.enable';
  +------+-------------------+-------------------------+-------+
  | Type | Instance          | Name                    | Value |
  +------+-------------------+-------------------------+-------+
  | tikv | 10.90.3.147:20160 | in-memory-engine.enable | true  |
  | tikv | 10.90.3.51:20160  | in-memory-engine.enable | true  |
  | tikv | 10.90.3.49:20160  | in-memory-engine.enable | false |
  +------+-------------------+-------------------------+-------+
  3 rows in set (0.04 sec)
  ```

+ 创建 3 个 PLACEMENT POLICY 为后面的步骤使用。关于 Placement Rules in SQL 特性请参考：<https://docs.pingcap.com/zh/tidb/stable/placement-rules-in-sql/#placement-rules-in-sql>
  + PLACEMENT POLICY `IME_R1` 指定表的 LEADER 角色的 Region 都放在 `region1` 的 TiKV 节点上，`region2` 和 `region3` 的 TiKV 节点上放置 FOLLOWER 角色的 Region。
  + PLACEMENT POLICY `IME_R2` 指定表的 LEADER 角色的 Region 都放在 `region2` 的 TiKV 节点上，`region1` 和 `region3` 的 TiKV 节点上放置 FOLLOWER 角色的 Region。
  + PLACEMENT POLICY `IME_R3` 指定表的 LEADER 角色的 Region 都放在 `region3` 的 TiKV 节点上，`region1` 和 `region2` 的 TiKV 节点上放置 FOLLOWER 角色的 Region。

  ```sql
  DROP PLACEMENT POLICY IF EXISTS IME_R1;
  DROP PLACEMENT POLICY IF EXISTS IME_R2;
  DROP PLACEMENT POLICY IF EXISTS IME_R3;
  CREATE PLACEMENT POLICY IME_R1 PRIMARY_REGION="region1" REGIONS="region1,region2,region3";
  CREATE PLACEMENT POLICY IME_R2 PRIMARY_REGION="region2" REGIONS="region1,region2,region3";
  CREATE PLACEMENT POLICY IME_R3 PRIMARY_REGION="region3" REGIONS="region1,region2,region3";
  SHOW PLACEMENT;
  ```

  ```output
  mysql>  DROP PLACEMENT POLICY IF EXISTS IME_R1;
  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql>   DROP PLACEMENT POLICY IF EXISTS IME_R2;
  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql>   DROP PLACEMENT POLICY IF EXISTS IME_R3;
  Query OK, 0 rows affected, 1 warning (0.01 sec)

  mysql> CREATE PLACEMENT POLICY IME_R1 PRIMARY_REGION="region1" REGIONS="region1,region2,region3";
  Query OK, 0 rows affected (0.52 sec)

  mysql> CREATE PLACEMENT POLICY IME_R2 PRIMARY_REGION="region2" REGIONS="region1,region2,region3";
  Query OK, 0 rows affected (0.51 sec)

  mysql> CREATE PLACEMENT POLICY IME_R3 PRIMARY_REGION="region3" REGIONS="region1,region2,region3";
  Query OK, 0 rows affected (0.52 sec)

  mysql> SHOW PLACEMENT;
  +---------------+------------------------------------------------------------+------------------+
  | Target        | Placement                                                  | Scheduling_State |
  +---------------+------------------------------------------------------------+------------------+
  | POLICY IME_R1 | PRIMARY_REGION="region1" REGIONS="region1,region2,region3" | NULL             |
  | POLICY IME_R2 | PRIMARY_REGION="region2" REGIONS="region1,region2,region3" | NULL             |
  | POLICY IME_R3 | PRIMARY_REGION="region3" REGIONS="region1,region2,region3" | NULL             |
  +---------------+------------------------------------------------------------+------------------+
  3 rows in set (0.01 sec)
  ```

+ 断开客户端连接。

  ```sql
  EXIT
  ```

  ```output
  mysql> EXIT
  Bye
  ```

### 步骤 5. 准备监控

+ 使用用户名 `admin` 和密码 `admin` 登录到 [Grafana 仪表盘](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000)。建议从此步骤开始保持监控页面打开，并将监控时间范围设置为最近 5 分钟或 15 分钟，同时将刷新频率改为 5 秒。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/9c787410ab425dd7eb7e7297cc46711ec63ccc22.png)

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