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

[`tidb_gc_enable`](https://docs.pingcap.com/zh/tidb/stable/system-variables#tidb_gc_enable-%E4%BB%8E-v50-%E7%89%88%E6%9C%AC%E5%BC%80%E5%A7%8B%E5%BC%95%E5%85%A5) 是全局持久化的 TiDB 系统变量之一，修改它并观察结果。

### 步骤 1：登录远程 Linux

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

+ 现在你将拥有三个活动的终端会话。

### 步骤 2：连接到 TiDB 服务器 1

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

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

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

### 步骤 3：再创建一个到 TiDB 服务器 1 的连接

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

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

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

### 步骤 4：连接到 TiDB 服务器 2

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

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

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

### 步骤 5. 在终端 1 中，将 `tidb_gc_enable` 更改为 `OFF`

+ 在[终端 1](ssh://monitor1-ssh#1)中，查看 `tidb_gc_enable` 的默认值.

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

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

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

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

+ 在全局范围将 `tidb_gc_enable` 的值从 `ON` 更改为 `OFF`.

  ```sql
  SET GLOBAL tidb_gc_enable=OFF;
  ```

  ```output
  tidb:db1> SET GLOBAL tidb_gc_enable=OFF;
  Query OK, 0 rows affected (0.06 sec)
  ```

### 步骤 6. 在当前会话中验证更改结果

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

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

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

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

### 步骤 7. 在另一个会话中验证更改结果

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

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

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

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

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

### 步骤 8. 在另一台 TiDB 服务器上验证更改结果

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

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

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

  ```

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

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

### 步骤 9. 尝试在会话范围内更改 `tidb_gc_enable` 的值

+ 在任意终端中，在会话范围内更改 `tidb_gc_enable` 的值.

  ```sql
  SET SESSION tidb_gc_enable=ON;
  ```

  ```output
  tidb:db2> SET SESSION tidb_gc_enable=ON;
  ERROR 1229 (HY000): Variable 'tidb_gc_enable' is a GLOBAL variable and should be set with SET GLOBAL
  ```

### 步骤 10. 在所有终端中，退出 MySQL 客户端

  ```bash
  EXIT
  ```

### 步骤 11. 重启 TiDB 集群

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

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

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

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

+ 验证在[终端 1](ssh://monitor1-ssh#1)、[终端 2](ssh://monitor1-ssh#2) 和[终端 3](ssh://monitor1-ssh#3) 中，`tidb_gc_enable` 的值为 `OFF`。我们以[终端 1](ssh://monitor1-ssh#1)为例，你需要重新连接到 TiDB servers ，因为之前的重启断开了你的会话。

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

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

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

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

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

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

### 步骤 13. 在任意终端中，将 `tidb_gc_enable` 恢复为 `ON`

  ```sql
  SET GLOBAL tidb_gc_enable=ON;
  ```

  ```output
  tidb:db1> SET GLOBAL tidb_gc_enable=ON;
  Query OK, 0 rows affected (0.07 sec)
  ```

### 步骤 14. 关闭所有终端

### 结论

全局持久化的 TiDB 系统变量只能在全局级别进行配置，其效果将立即应用于集群中的所有 TiDB server 和会话。
