索引
变量分类
变量设置
变量分类
变量主要分为全局变量和会话变量,全局变量是当客户端程序与服务器端建立会话后自动集成部分全局变量的值,而会话变量是只对当前变量有效,另外全局变量有些支持在线修改,有些则是只读变量,只读变量只允许在配置文件中加载值。
变量设置
1.全局变量设置
全局变量保存在数据库information_schema的GLOBAL_VARIABLES表中,因此修改时可以通过配置文件加载时修改,部分变量也可通过在线修改,在线修改时通常有两种命令方式,分别如下:
mysql>SET GLOBAL system_var_name=value;
mysql>SET @@global.system_var_name=value;
举例说明如下:
MariaDB [(none)]> show global variables like 'transaction_prealloc_size';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| transaction_prealloc_size | 4096 |
+---------------------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set global transaction_prealloc_size=8192;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show global variables like 'transaction_prealloc_size';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| transaction_prealloc_size | 8192 |
+---------------------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set @@global.transaction_prealloc_size=4096;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show global variables like 'transaction_prealloc_size';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| transaction_prealloc_size | 4096 |
+---------------------------+-------+
1 row in set (0.00 sec)
2.会话变量设置
会话变量保存在数据库information_schema的SESSION_VARIABLES表中,因此修改时可以通过配置文件加载时修改,部分变量也可通过在线修改,在线修改时通常有两种命令方式,分别如下:
mysql>SET SESSION system_var_name=value;
mysql>SET @@session.system_var_name=value
MariaDB [(none)]> show session variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set session wait_timeout=30000;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show session variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 30000 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set @@session.wait_timeout=28800;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show session variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)
3.会话变量转全局变量
会话变量调整为全局变量时,通常采用如下方法:
mysql> set @@session.wait_timeout=@@global.wait_timeout;