这篇文章主要讲解了“mysql关于variable知识点有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql关于variable知识点有哪些”吧!
1、MySQL的my.cnf文件类似Oracle的参数文件,可以把它理解成oracle的pfile静态参数文件
2、MySQL可以使用set命令动态的修改变量,修改session变量只对当前会话产生影响;修改global变量,对所有新会话的global变量、session变量(特殊情况除外,比如wait_timeout变量)产生影响
3、set命令修改的变量不会写入my.cnf文件,DB重启的话之前set修改的变量就失效了,想要永久生效,就把变量写入到my.cnf文件
4、只读变量,不能使用set修改,只能通过修改my.cnf文件再重启生效
5、全局(GLOBAL)级变量对整个MySQL服务器有效;会话(SESSION)级变量只影响当前会话。有些变量同时拥有这两个级别,MySQL将在建立连接时用全局级变量初始化会话级变量,但一旦连接建立之后,全局级变量的改变不会影响到会话级变量
6、可以把全局(GLOBAL)级变量理解成oracle的system参数,会话(SESSION)级变量理解成oracle的session参数
7、mysql没有类似oracle的create pfile from memory保存所有内存的中参数值的功能,mysql如果要保存所有修改的参数但是没有写入my.cnf的方法,就是select * from INFORMATION_SCHEMA.GLOBAL_VARIABLES查询出来并保存
查看系统变量的值
方法1
mysql> show variables like '%wait_timeout%';
mysql> show global variables like '%wait_timeout%';
mysql> show session variables like '%wait_timeout%';
注意:show variables优先显示会话级变量的值,如果这个值不存在,则显示全局级变量的值,当然你也可以加上SESSION关键字区别
方法2
mysql> set global show_compatibility_56=on;
mysql> select * from INFORMATION_SCHEMA.GLOBAL_VARIABLES where variable_name like '%wait_timeout%'
mysql> select * from INFORMATION_SCHEMA.SESSION_VARIABLES where variable_name like '%wait_timeout%'
修改系统变量的值
mysql> set wait_timeout = xxx;
mysql> set global wait_timeout = yyy;
只读系统标量,不能使用set修改
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set log_bin=off;
ERROR 1238 (HY000): Variable 'log_bin' is a read only variable
只有全局变量,没有会话变量
全局变量修改的时候,其他新开会话的会话变量和全局变量都是修改后的值
会话1
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
mysql> set server_id=2;
ERROR 1229 (HY000): Variable 'server_id' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set GLOBAL server_id=2;
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
mysql> show global variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
打开另一个会话
全局变量修改的时候,其他新开会话的会话变量和全局变量都是修改后的值
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
mysql> show global variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
有全局变量,也有会话变量
会话变量修改的时候,不影响本身的全局变量,也不影响其他新开会话的会话变量和全局变量
全局变量修改的时候,其他新开会话的会话变量和全局变量都是修改后的值
会话1
mysql> show variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
mysql> show global variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
mysql> set read_buffer_size=212992;
mysql> show variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 212992 |
+------------------+--------+
mysql> show global variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
打开另一个会话2
会话变量修改的时候,不影响本身的全局变量,也不影响其他新开会话的会话变量和全局变量
mysql> show variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
mysql> show global variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
会话1继续如下
mysql> set global read_buffer_size=16384;
mysql> show variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 212992 |
+------------------+--------+
mysql> show global variables like 'read_buffer_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| read_buffer_size | 16384 |
+------------------+-------+
打开另一个会话3
全局变量修改的时候,其他新开会话的会话变量和全局变量都是修改后的值
mysql> show variables like 'read_buffer_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| read_buffer_size | 16384 |
+------------------+-------+
mysql> show global variables like 'read_buffer_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| read_buffer_size | 16384 |
+------------------+-------+
重启之后
重启之后,所有的set修改的变量值都不在了,回到了初始值
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
mysql> show global variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
mysql> show variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
mysql> show global variables like 'read_buffer_size';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
感谢各位的阅读,以上就是“mysql关于variable知识点有哪些”的内容了,经过本文的学习后,相信大家对mysql关于variable知识点有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。