在MySQL中,my.cnf是参数文件(Option Files),雷同于ORACLE数据库中的spfile、pfile参数文件,照理说,参数文件my.cnf中的都是系统参数(这种称号较量切合思维习惯),可是官方又称号其为系统变量(system variables),那么到底这个叫系统参数或系统变量(system variables)呢? 这个曾经是一个让我很纠结的问题,因为MySQL中有各类范例的变量,有时候语言就是这么博大博识;相信许多人也对这个问题或多或少有点狐疑。其实抛开这些名词,它们就是同一个工作(对象),不管你叫它系统变量(system variables)或系统参数都可,无需那么纠结。 就比如王三,有人叫他王三;也有人也叫他王麻子外号一样。
别的,MySQL中有许多变量范例,确实有时候让人有点夹杂不清,本文规划总结一下MySQL数据库的各类变量范例,理清各类变量范例观念。可以或许从全局有个清晰思路。MySQL变量范例详细参考下图:
Server System Variables(系统变量)
MySQL系统变量(system variables)是指MySQL实例的各类系统变量,实际上是一些系统参数,用于初始化或设定命据库对系统资源的占用,文件存放位置等等,这些变量包括MySQL编译时的参数默认值,可能my.cnf设置文件里设置的参数值。默认环境下系统变量都是小写字母。官方文档先容如下:
The MySQL server maintains many system variables that indicate how it is configured. Each system variable has a default value. System variables can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically at runtime using the SET statement, which enables you to modify operation of the server without having to stop and restart it. You can also use system variable values in expressions.
系统变量(system variables)按浸染域范畴可以分为会话级别系统变量和全局级别系统变量。假如要确认系统变量是全局级别照旧会话级别,可以参考官方文档,假如Scope其值为GLOBAL或SESSION,暗示变量既是全局级别系统变量,又是会话级别系统变量。假如其Scope其值为GLOBAL,暗示系统变量为全局级别系统变量。
select * from information_schema.global_variables; select * from information_schema.global_variables where variable_name='xxxx'; select * from performance_schema.global_variables;
–查察系统变量的当前会话值
select * from information_schema.session_variables; select * from information_schema.session_variables where variable_name='xxxx'; select * from performance_schema.session_variables;
SELECT @@global.sql_mode, @@session.sql_mode, @@sql_mode; mysql> show variables like '%connect_timeout%'; mysql> show local variables like '%connect_timeout%'; mysql> show session variables like '%connect_timeout%'; mysql> show global variables like '%connect_timeout%';
留意:对付SHOW VARIABLES,假如不指定GLOBAL、SESSION可能LOCAL,MySQL返回SESSION值,假如要区分系统变量是全局照旧会话级别。不能利用下面方法,假如某一个系统变量是全局级此外,那么在当前会话的值也是全局级此外值。譬喻系统变量AUTOMATIC_SP_PRIVILEGES,它是一个全局级别系统变量,可是 show session variables like ‘%automatic_sp_privileges%’一样能查到其值。所以这种方法无法区别系统变量是会话级别照旧全局级别。
mysql> show session variables like '%automatic_sp_privileges%'; +-------------------------+-------+ | Variable_name | Value | +-------------------------+-------+ | automatic_sp_privileges | ON | +-------------------------+-------+ 1 row in set (0.00 sec) mysql> select * from information_schema.global_variables -> where variable_name='automatic_sp_privileges'; +-------------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------------+----------------+ | AUTOMATIC_SP_PRIVILEGES | ON | +-------------------------+----------------+ 1 row in set, 1 warning (0.00 sec) mysql>
假如要区分系统变量是全局照旧会话级别,可以用下面方法:
mysql> SET AUTOMATIC_SP_PRIVILEGES=OFF; ERROR 1229 (HY000): Variable 'automatic_sp_privileges' is a GLOBAL variable and should be set with SET GLOBAL
可以利用SET呼吁修改系统变量的值,如下所示:
修改全局级别系统变量:
SET GLOBAL max_connections=300; SET @@global.max_connections=300;
留意:变动全局变量的值,需要拥有SUPER权限
修改会话级别系统变量:
SET @@session.max_join_size=DEFAULT; SET max_join_size=DEFAULT; --默认为会话变量。假如在变量名前没有级别限定符,暗示修改会话级变量。 SET SESSION max_join_size=DEFAULT;
假如修改系统全局变量没有指定GLOBAL或@@global的话,就会报“Variable ‘xxx’ is a GLOBAL variable and should be set with SET GLOBAL”这类错误。
mysql> set max_connections=300; ERROR 1229 (HY000): Variable 'max_connections' is a GLOBAL variable and should be set with SET GLOBAL mysql> set global max_connections=300; Query OK, 0 rows affected (0.00 sec) mysql>
系统变量(system variables)按是否可以动态修改,可以分为系统动态变量(Dynamic System Variables)和系统静态变量。怎么区分系统变量是动态和静态的呢? 这个只能查察官方文档,系统变量的”Dynamic”属性为Yes,则暗示可以动态修改。Dynamic Variable详细可以参考https://dev.mysql.com/doc/refman/5.7/en/dynamic-system-variables.html
别的,有些系统变量是只读的,不能修改的。如下所示:
mysql> mysql> set global innodb_version='5.6.21'; ERROR 1238 (HY000): Variable 'innodb_version' is a read only variable mysql>
别的,尚有一个Structured System Variables观念,其实就是系统变量是一个布局体(Strut),官方先容如下所示:
Structured System Variables
A structured variable differs from a regular system variable in two respects:
Its value is a structure with components that specify server parameters considered to be closely related.
There might be several instances of a given type of structured variable. Each one has a different name and refers to a different resource maintained by the server.
Server Status Variables(处事器状态变量)
MySQL状态变量(Server Status Variables)是当前处事器从启动后累计的一些系统状态信息,譬喻最大毗连数,累计的间断毗连等等,主要用于评估当前系统资源的利用环境以进一步阐明系统机能而做出相应的调办理策。这个预计有人会跟系统变量夹杂,其实状态变量是动态变革的,别的,状态变量是只读的:只能由MySQL处事器自己配置和修改,对付用户来说是只读的,不行以通过SET语句配置和修改它们,而系统变量则可以随时修改。状态变量也分为会话级与全局级别状态信息。有些状态变量可以用FLUSH STATUS语句重置为零值。
关于查察状态变量,show status也支持like匹配查询。如下所示:
show status like '%variable_name%' show global status like '%variable_name%'
#当前测试情况 ysql> select version() from dual; -----------+ version() | -----------+ 5.7.21 | -----------+ row in set (0.00 sec) mysql> show status; --查察所有的状态变量 ysql> show global status like 'Aborted_connects%'; ------------------+-------+ Variable_name | Value | ------------------+-------+ Aborted_connects | 2 | ------------------+-------+ row in set (0.01 sec) ysql> show session status like 'Aborted_connects%'; ------------------+-------+ Variable_name | Value | ------------------+-------+ Aborted_connects | 2 | ------------------+-------+ row in set (0.00 sec) ysql> select * from information_schema.global_status; RROR 3167 (HY000): The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56' ysql> # ysql> show variables like '%show_compatibility_56%'; -----------------------+-------+ Variable_name | Value | -----------------------+-------+ show_compatibility_56 | OFF | -----------------------+-------+ row in set (0.00 sec) ysql> set global show_compatibility_56=on; uery OK, 0 rows affected (0.00 sec) ysql> select * from information_schema.global_status; -----------------------------------------------+---------------------------------------+ VARIABLE_NAME VARIABLE_VALUE | -----------------------------------------------+---------------------------------------+ ABORTED_CLIENTS | 138097 | ABORTED_CONNECTS | 5 | BINLOG_CACHE_DISK_USE | 0 | BINLOG_CACHE_USE | 0 | .................................................................................... select * from performance_schema.global_status; select * from performance_schema.session_status;
留意:MySQL 5.7今后系统变量和状态变量需要从performance_schema中举办获取,information_schema仍然保存了GLOBAL_STATUS,GLOBAL_VARIABLES两个表做兼容,假如但愿沿用information_schema中举办查询的习惯,5.7提供了show_compatibility_56参数,配置为ON可以兼容5.7之前的用法,不然就会报错(ERROR 3167 (HY000)).
User-Defined Variables(用户自界说变量)