问题背景: mysql server是5.7.20版本,在mysql客户端修改long_query_time全局变量由0.01修改为1但slow log日志仍显示小于1的记录,难道set global long_query_time不起作用吗?
测试一下
root@localhost:mysql3306.sock [(none)]> show global variables like 'long_query_time'; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 1.000000 | +-----------------+----------+ 1 row in set (0.00 sec) root@localhost:mysql3306.sock [(none)]> show variables like 'long_query_time'; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 1.000000 | +-----------------+----------+ 1 row in set (0.00 sec) root@localhost:mysql3306.sock [(none)]> set global long_query_time=2; Query OK, 0 rows affected (0.00 sec) root@localhost:mysql3306.sock [(none)]> show global variables like 'long_query_time'; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 2.000000 | +-----------------+----------+ 1 row in set (0.00 sec) root@localhost:mysql3306.sock [(none)]> show variables like 'long_query_time'; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 1.000000 | +-----------------+----------+ 1 row in set (0.01 sec)
结果显示修改long_query_time全局变量后仅全局变量变更了,但当前session的long_query_time没有变更,那原因又是什么呢?
经查官方文档https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html显示,long_query_time属于全局和会话范围的系统变量
https://dev.mysql.com/doc/refman/8.0/en/using-system-variables.html显示修改全局变量仅对新会话生效而对当前会话不起作用
再查看slow log发现修改全局变量后所谓的不生效日志记录都是同一个Id: 1041729,也就是说这个会话在修改全局变量前就已创建了,这并非是MySQL的Bug~~
如果想立即生效,那该怎么办呢? 一种办法是重启mysqld服务,另一种是kill掉Sleep和Query的连接(mysql -S /tmp/mysql3306.sock -uroot -p -e 'select concat("kill ",thd_id,";") from sys.session where command="Sleep" or command="Query";'|grep kill);值得注意的是要评估此类操作对业务影响再执行!