## Module 2: Modifying Non-Persistent Global System Variables

[`max_connections`](https://docs.pingcap.com/tidb/stable/system-variables#max_connections) is one of the non-persistent global system variables, modify it and observe the result.

### Step 1. Setup the connections

+ Open three SSH connections to your lab virtual machine, by clicking:
  + [Terminal 1](ssh://monitor1-ssh#1)
  + [Terminal 2](ssh://monitor1-ssh#2)
  + [Terminal 3](ssh://monitor1-ssh#3)

- In [Terminal 1](ssh://monitor1-ssh#1), execute:

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

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

- In [Terminal 2](ssh://monitor1-ssh#2), execute:

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

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

- In [Terminal 3](ssh://monitor1-ssh#3), execute:

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

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

### Step 2. Change the `max_connections` from `0` to `1`

- On [Terminal 1](ssh://monitor1-ssh#1), execute the following commands.

+ Check the default value.

  ```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)
  ```

+ Change the `max_connections` from `0` to `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)
  ```

### Step 3. Verify the change result

- Switch to [Terminal 2](ssh://monitor1-ssh#2), execute the following commands.

  ```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)
  ```

### Step 4. Check the value of `max_connections` from another TiDB server

- Switch to [Terminal 3](ssh://monitor1-ssh#3), execute the following commands.

  ```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)
  ```

### Step 5. In terminal 1, exit the database session, and then try to log in again

+ Exit the database session on [Terminal 1](ssh://monitor1-ssh#1).

  ```sql
  EXIT
  ```

+ Attempt to log back in by running `connect-db1.sh` again. However, this time the login attempt will fail due to the `max_connections` setting on the TiDB server instance 1.

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

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

### Step 6. In terminal 1, try to log in to TiDB Server 2

- Execute the following command, a successful connection is expected.

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

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

### Step 7. Exit to OS prompt

- In all terminals, [Terminal 1](ssh://monitor1-ssh#1), [Terminal 2](ssh://monitor1-ssh#2), and [Terminal 3](ssh://monitor1-ssh#3), exit to OS prompt.

  ```sql
  EXIT
  ```

### Step 8. In any terminal, restart the TiDB cluster

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

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

### Step 9. In all terminals, check the value of `max_connections`

+ We use [Terminal 1](ssh://monitor1-ssh#1) as an example.

  ```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)
  ```

### Step 10. Exit and close all terminals

### Summary

The non-persistent global TiDB system variable can only be configured on a global level, and its effects will be applied to the TiDB instance that you issued the `SET GLOBAL` statement.
