参数的分类
- 全局参数:GLOBAL
- 可修改参数
- 不可修改参数
- 会话参数:SESSION
- 可修改参数
- 不可修改参数
1: 用户可在线修改
非只读参数
,只读参数
只能预先在配置文件中进行设置,通过重启数据库实例,方可生效。
2: 所有的在线修改过的参数(GLOBAL/SESSION),在重启后,都会丢失,不会写如
my.cnf
,无法将修改进行持久化
3: 有些参数,即存在于
GLOBAL
又存在于SESSION
, 比如autocommit
(PS:MySQL默认是提交的)
1. 查看参数
mysql> show variables; # 显示当前mysql的所有参数,且无隐藏参数
mysql> show variables like "max_%"; #查以max_开头的变量
2. 设置参数
-
设置全局(GLOBAL)参数
mysql> set global slow_query_log = off; #不加global,会提示错误 #slow_query_log是全局参数 mysql> set slow_query_log = off; # 下面就报错了,默认是会话参数 ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
-
设置会话(SESSION)参数
mysql> set autocommit = 0; # 当前会话生效 # 或者 mysql> set session autocommit = 0; # 当前会话生效
autocommit
同样在GLOBAL
中, 也有同样的参数mysql> set global autocommit = 1; #当前实例,全局生效
注意:如果这个时候/etc/init.d/mysqld restart, 则全局的autocommit的值会变成默认值,或者依赖于my.cnf的设置值。
执行的效果如下:
mysql> show variables like "slow%"; # 原值为ON +---------------------+----------+ | Variable_name | Value | +---------------------+----------+ | slow_launch_time | 2 | | slow_query_log | OFF | | slow_query_log_file | slow.log | +---------------------+----------+ 3 rows in set (0.00 sec) mysql> select @@session.autocommit; # 等价于 slect @@autocomit; +----------------------+ | @@session.autocommit | +----------------------+ | 0 | +----------------------+ 1 row in set (0.00 sec) mysql> select @@global.autocommit; +---------------------+ | @@global.autocommit | +---------------------+ | 1 | +---------------------+ 1 row in set (0.00 sec)