## 模块 3: 修改会话级系统变量

[`tidb_allow_remove_auto_inc`](https://docs.pingcap.com/zh/tidb/stable/system-variables#tidb_allow_remove_auto_inc-%E4%BB%8E-v2118-%E5%92%8C-v304-%E7%89%88%E6%9C%AC%E5%BC%80%E5%A7%8B%E5%BC%95%E5%85%A5) 是会话级系统变量之一，修改它并观察结果。

### 步骤 1. 建立连接

+ 点击以下链接，在实验环境中打开三个 SSH 连接：
  + [终端 1](ssh://monitor1-ssh#1)
  + [终端 2](ssh://monitor1-ssh#2)
  + [终端 3](ssh://monitor1-ssh#3)

- 在 [终端 1](ssh://monitor1-ssh#1) 中，连接到 TiDB 服务器 2：

  ```bash
  ./connect-db2.sh
  ```

  ```output
  ...
  tidb:db1>
  ```

- 在 [终端 2](ssh://monitor1-ssh#2) 中，连接到 TiDB 服务器 1：

  ```bash
  ./connect-db1.sh
  ```

  ```output
  ...
  tidb:db1>
  ```

- 在 [终端 3](ssh://monitor1-ssh#3) 中，连接到 TiDB 服务器 2：

  ```bash
  ./connect-db2.sh
  ```

  ```output
  ...
  tidb:db2>
  ```

### 步骤 2. 在终端 1 中，将 `tidb_allow_remove_auto_inc` 更改为 `ON`

- 在 [终端 1](ssh://monitor1-ssh#1) 中，执行以下命令：

  ```sql
  SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  ```

  ```output
  tidb:db2> SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  +----------------------------+-------+
  | Variable_name              | Value |
  +----------------------------+-------+
  | tidb_allow_remove_auto_inc | OFF   |
  +----------------------------+-------+
  1 row in set (0.00 sec)
  ```

  ```sql
  SET SESSION tidb_allow_remove_auto_inc=ON;
  ```

  ```output
  tidb:db2> SET SESSION tidb_allow_remove_auto_inc=ON;
  Query OK, 0 rows affected (0.00 sec)
  ```

  ```sql
  SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  ```

  ```output
  tidb:db2> SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  +----------------------------+-------+
  | Variable_name              | Value |
  +----------------------------+-------+
  | tidb_allow_remove_auto_inc | ON    |
  +----------------------------+-------+
  1 row in set (0.00 sec)
  ```

### 步骤 3. 在终端 2 中，检查 `tidb_allow_remove_auto_inc` 的值

- 切换到 [终端 2](ssh://monitor1-ssh#2)，执行以下命令：

  ```sql
  SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  ```

  ```output
  tidb:db1> SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  +----------------------------+-------+
  | Variable_name              | Value |
  +----------------------------+-------+
  | tidb_allow_remove_auto_inc | OFF   |
  +----------------------------+-------+
  1 row in set (0.01 sec)
  ```

### 步骤 4. 在终端 3 中，检查 `tidb_allow_remove_auto_inc` 的值

- 切换到 [终端 3](ssh://monitor1-ssh#3)，执行以下命令：

  ```sql
  SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  ```

  ```output
  tidb:db2> SHOW SESSION VARIABLES LIKE 'tidb_allow_remove_auto_inc';
  +----------------------------+-------+
  | Variable_name              | Value |
  +----------------------------+-------+
  | tidb_allow_remove_auto_inc | OFF   |
  +----------------------------+-------+
  1 row in set (0.00 sec)
  ```

### 步骤 5. 在任意终端中，更改 `tidb_allow_remove_auto_inc` 的值

+ 尝试在全局范围内更改 `tidb_allow_remove_auto_inc` 的值.

  ```sql
  SET GLOBAL tidb_allow_remove_auto_inc=ON;
  ```

  ```output
  tidb:db2> SET GLOBAL tidb_allow_remove_auto_inc=ON;
  ERROR 1228 (HY000): Variable 'tidb_allow_remove_auto_inc' is a SESSION variable and can't be used with SET GLOBAL
  ```

### 步骤 6. 在终端 1 中，将 `tidb_allow_remove_auto_inc` 改回 `OFF`

  ```sql
  SET SESSION tidb_allow_remove_auto_inc=OFF;
  ```

  ```output
  tidb:db2> SET SESSION tidb_allow_remove_auto_inc=OFF;
  Query OK, 0 rows affected (0.00 sec)
  ```

### 结论

仅会话的 TiDB 系统变量只能在会话级别进行配置，其效果将应用于你使用 `SET SESSION` 语句的当前会话。
