## 模块 2: 修改非持久化全局系统变量

[`max_connections`](https://docs.pingcap.com/zh/tidb/stable/system-variables#max_connections) 是一个非持久化的全局系统变量，修改它并观察结果。

### 步骤 1. 连接准备

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

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

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

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

- 在 [终端 2](ssh://monitor1-ssh#2) 中，执行：

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

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

- 在 [终端 3](ssh://monitor1-ssh#3) 中，执行：

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

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

### 步骤 2. 将 `max_connections` 从 `0` 更改为 `1`

- 切换到 [终端 1](ssh://monitor1-ssh#1).

+ 检查默认值.

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

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

  ```sql
  SHOW GLOBAL VARIABLES LIKE 'max_connections';
  ```

  ```output
  tidb:db1> SHOW GLOBAL VARIABLES LIKE 'max_connections';
  +-----------------+-------+
  | Variable_name   | Value |
  +-----------------+-------+
  | max_connections | 0     |
  +-----------------+-------+
  1 row in set (0.00 sec)
  ```

+ 将 `max_connections` 的值从 `0` 更改为 `1`.

  ```sql
  SET GLOBAL max_connections=1;
  ```

  ```output
  tidb:db1> SET GLOBAL max_connections=1;
  Query OK, 0 rows affected (0.00 sec)
  ```

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

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

  ```sql
  SHOW GLOBAL VARIABLES LIKE 'max_connections';
  ```

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

### 步骤 3. 验证更改结果

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

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

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

  ```sql
  SHOW GLOBAL VARIABLES LIKE 'max_connections';
  ```

  ```output
  tidb:db1> SHOW GLOBAL VARIABLES LIKE 'max_connections';
  +-----------------+-------+
  | Variable_name   | Value |
  +-----------------+-------+
  | max_connections | 1     |
  +-----------------+-------+
  1 row in set (0.00 sec)
  ```

### 步骤 4. 在另一台 TiDB 服务器上检查 `max_connections` 的值

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

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

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

  ```sql
  SHOW GLOBAL VARIABLES LIKE 'max_connections';
  ```

  ```output
  tidb:db2> SHOW GLOBAL VARIABLES LIKE 'max_connections';
  +-----------------+-------+
  | Variable_name   | Value |
  +-----------------+-------+
  | max_connections | 0     |
  +-----------------+-------+
  1 row in set (0.01 sec)
  ```

### 步骤 5. 在终端 1 中退出数据库会话，然后尝试重新登录

+ 在 [终端 1](ssh://monitor1-ssh#1) 退出数据库会话。

  ```sql
  EXIT
  ```

+ 再次运行 `connect-db1.sh` 尝试登录。但这一次，由于 TiDB 服务器实例 1 上的 `max_connections` 设置，登录会失败。

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

  ```output
  ERROR 1040 (HY000): Too many connections
  ```

### 步骤 6. 在终端 1 中尝试登录 TiDB 服务器 2

- 执行以下命令，应该可以成功连接。

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

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

### 步骤 7. 退出到操作系统提示符

- 在所有终端（[终端 1](ssh://monitor1-ssh#1)、[终端 2](ssh://monitor1-ssh#2)、[终端 3](ssh://monitor1-ssh#3)）中，退出数据库会话，返回操作系统提示符。

  ```sql
  EXIT
  ```

### 步骤 8. 在任意终端中，重新启动 TiDB 集群

  ```bash
  tiup cluster restart tidb-test --yes
  ```

  ```output
  ...
  Restarted cluster `tidb-test` successfully
  ```

### 步骤 9. 在所有终端中，检查 `max_connections` 的值

+ 我们以[终端 1](ssh://monitor1-ssh#1)为例.

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

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

  ```bash
  SHOW SESSION VARIABLES LIKE 'max_connections';
  ```

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

  ```bash
  SHOW GLOBAL VARIABLES LIKE 'max_connections';
  ```

  ```output
  tidb:db2> SHOW GLOBAL VARIABLES LIKE 'max_connections';
  +-----------------+-------+
  | Variable_name   | Value |
  +-----------------+-------+
  | max_connections | 0     |
  +-----------------+-------+
  1 row in set (0.01 sec)
  ```

### 步骤 10. 退出并关闭所有终端

### 结论

非持久化的全局 TiDB 系统变量只能在全局级别进行配置，其效果将应用于你使用的 `SET GLOBAL` 语句的 TiDB 实例。
