## 模块 2：TiDB Server 缩容过程中的 TiProxy 连接迁移

TiProxy 可以在不断开客户端连接的情况下，将连接从一台 TiDB Server 迁移到另一台。

当某台 TiDB Server 将要下线，或某台 TiDB Server 的连接数比例超过阈值时，会触发连接迁移。在使用 TiProxy 的情况下，客户端对该迁移无感知。

下图中，红色虚线标注的 TiDB Server 为将要缩容的节点。

![demo018_splash_image_aa_003_20250926.png 85%](https://lab-static.pingcap.com/images/2025/9/26/359c3af54e50e857a606f12425b9849e2fcff4ba.png)

### 步骤 1. 校验 TiProxy 相关设置

+ [graceful-wait-before-shutdown](https://docs.pingcap.com/tidb/stable/tidb-configuration-file/#graceful-wait-before-shutdown-new-in-v50) 使得 TiDB 关闭服务（关闭 TCP 端口）时的等待秒数，用于让客户端有序断连。
+ 在 [Terminal 2](ssh://monitor1-ssh#2) 中执行以下命令：

  ```bash
  ./connect-tiproxy.sh
  ```

  ```output
  tidb:tiproxy>
  ```

  ```sql
  show config where name='graceful-wait-before-shutdown';
  exit;
  ```

  ```output
  tidb:tiproxy> show config where name='graceful-wait-before-shutdown';
  +------+------------------+-------------------------------+-------+
  | Type | Instance         | Name                          | Value |
  +------+------------------+-------------------------------+-------+
  | tidb | ${LAB:HOST_DB1_PRIVATE_IP}:4000 | graceful-wait-before-shutdown | 15    |
  | tidb | ${LAB:HOST_DB2_PRIVATE_IP}:4000 | graceful-wait-before-shutdown | 15    |
  +------+------------------+-------------------------------+-------+
  2 rows in set (0.01 sec)
  ```

> [!TIP]
> [graceful-wait-before-shutdown](https://docs.pingcap.com/zh/tidb/stable/tidb-configuration-file/#graceful-wait-before-shutdown-new-in-v50) 的值应至少比应用中最长事务的持续时间多 10 秒，以避免 TiDB Server 下线时客户端连接被中断。
> 此外，建议在缩容 TiDB Server 时避免运行长事务（如批量处理等）。

### 步骤 2. 在终端 1 中检查负载

+ 在 [Terminal 1](ssh://monitor1-ssh#1) 中确认 Python 负载仍在运行，且线程连接到了两台 TiDB Server。
+ 注意，该 Python 代码没有针对 TiDB 缩容的额外处理逻辑；若不使用 TiProxy，通常会出现连接丢失等报错。

  ```output
  ...
  Thread 4: Connected to TiDB server ${LAB:HOST_DB2_PRIVATE_IP}:4000. User 1 balance updated to: 6
  Thread 3: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 9 balance updated to: 8
  Thread 1: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 10 balance updated to: 5
  Thread 2: Connected to TiDB server ${LAB:HOST_DB2_PRIVATE_IP}:4000. User 1 balance updated to: 3
  ...
  ```

### 步骤 3. 缩容一台 TiDB Server

+ 打开 [Terminal 2](ssh://monitor1-ssh#2) 并执行以下命令：

  ```bash
  ~/.tiup/bin/tiup cluster scale-in tidb-demo --node ${HOST_DB2_PRIVATE_IP}:4000 -y
  ```

  ```output
  ...
  Scaled cluster `tidb-demo` in successfully
  ```

### 步骤 4. 观察 [Terminal 1](ssh://monitor1-ssh#1)

+ 在 [Terminal 1](ssh://monitor1-ssh#1) 中，此时应可看到所有线程已连接到同一台 TiDB Server。
+ 屏幕上不会打印错误信息。连接迁移对应用完全透明。

  ```output
  ...
  Thread 2: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 9 balance updated to: 2
  Thread 1: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 3 balance updated to: 9
  Thread 4: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 1 balance updated to: 6
  Thread 2: Connected to TiDB server ${LAB:HOST_DB1_PRIVATE_IP}:4000. User 6 balance updated to: 6
  ...
  ```

+ 使用 `ctrl` + `c` 停止负载。

  ```output
  ^C
  Received interrupt signal. Stopping all threads...
  ```

### 小结

+ 本实验展示了在 TiDB Server 缩容时，TiProxy 能将连接迁移至其他 TiDB Server，使缩容过程对应用透明。
