## モジュール 2: 非永続的なグローバルシステム変数の変更

[`max_connections`](https://docs.pingcap.com/tidb/stable/system-variables#max_connections) は非永続的なグローバルシステム変数の一つです。これを変更し、その結果を観察します。

### 手順 1. 接続の準備

+ 以下のリンクをクリックして、実験用仮想マシンに3つの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. すべてのターミナルで OS プロンプトに戻る

- [ターミナル 1](ssh://monitor1-ssh#1)、[ターミナル 2](ssh://monitor1-ssh#2)、[ターミナル 3](ssh://monitor1-ssh#3) のすべてで、データベースセッションを終了して OS プロンプトに戻ります。

  ```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インスタンスに適用されます。
