太多这样的问题, 所以打算一窥究竟
测试设置参数后,是否会生效
https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
* 重点说明 If you change a session system variable, the value remains in effect within your session until you change the variable to a different value or the session ends. The change has no effect on other sessions. If you change a global system variable, the value is remembered and used for new sessions until you change the variable to a different value or the server exits. The change is visible to any client that accesses the global variable. However, the change affects the corresponding session variable only for clients that connect after the change. The global variable change does not affect the session variable for any current client sessions (not even the session within which the SET GLOBAL statement occurred). 官方重点说明,设置global变量的时候,只对后面连接进来的session生效,对当前session和之前的session不生效 接下来,我们好好测试下5.7 可以看到遗憾的是:只能看到Both和session的变量,scope=global没法看(因为会立即生效)
dba:(none)> select * from performance_schema.variables_by_thread as a,\ -> (select THREAD_ID,PROCESSLIST_ID,PROCESSLIST_USER,PROCESSLIST_HOST,PROCESSLIST_COMMAND,PROCESSLIST_STATE from performance_schema.threads where PROCESSLIST_USER<>'NULL') as b\ -> where a.THREAD_ID = b.THREAD_ID and a.VARIABLE_NAME = 'sql_safe_updates'; +-----------+------------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | THREAD_ID | PROCESSLIST_ID | PROCESSLIST_USER | PROCESSLIST_HOST | PROCESSLIST_COMMAND | PROCESSLIST_STATE | +-----------+------------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | 313 | sql_safe_updates | OFF | 313 | 232 | repl | xx.xxx.xxx.xxx | Binlog Dump GTID | Master has sent all binlog to slave; waiting for more updates | | 381 | sql_safe_updates | ON | 381 | 300 | dba | localhost | Query | Sending data | +-----------+------------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ 2 rows in set (0.00 sec)用我们刚刚学到的知识,来验证更加快速和靠谱
变量代表 1. Both 级别的变量代表:sql_safe_updates , long_query_time 测试 * 第一次查看long_query_time参数,PROCESSLIST_ID=307,308,309 都是一样的,都是300s dba:(none)> select * from performance_schema.variables_by_thread as a, (select THREAD_ID,PROCESSLIST_ID,PROCESSLIST_USER,PROCESSLIST_HOST,PROCESSLIST_COMMAND,PROCESSLIST_STATE from performance_schema.threads where PROCESSLIST_USER<>'NULL') as b where a.THREAD_ID = b.THREAD_ID and a.VARIABLE_NAME = 'long_query_time'; +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | THREAD_ID | PROCESSLIST_ID | PROCESSLIST_USER | PROCESSLIST_HOST | PROCESSLIST_COMMAND | PROCESSLIST_STATE | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | 388 | long_query_time | 300.000000 | 388 | 307 | dba | localhost | Sleep | NULL | | 389 | long_query_time | 300.000000 | 389 | 308 | dba | localhost | Query | Sending data | | 390 | long_query_time | 300.000000 | 390 | 309 | dba | localhost | Sleep | NULL | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ 4 rows in set (0.00 sec) * 我们再PROCESSLIST_ID=308的session上进行设置long_query_time=100,我们能看到这个时候所有的session都还是300,没有生效 dba:(none)> set global long_query_time=100; Query OK, 0 rows affected (0.00 sec) dba:(none)> select * from performance_schema.variables_by_thread as a, (select THREAD_ID,PROCESSLIST_ID,PROCESSLIST_USER,PROCESSLIST_HOST,PROCESSLIST_COMMAND,PROCESSLIST_STATE from performance_schema.threads where PROCESSLIST_USER<>'NULL') as b where a.THREAD_ID = b.THREAD_ID and a.VARIABLE_NAME = 'long_query_time'; +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | THREAD_ID | PROCESSLIST_ID | PROCESSLIST_USER | PROCESSLIST_HOST | PROCESSLIST_COMMAND | PROCESSLIST_STATE | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | 388 | long_query_time | 300.000000 | 388 | 307 | dba | localhost | Sleep | NULL | | 389 | long_query_time | 300.000000 | 389 | 308 | dba | localhost | Query | Sending data | | 390 | long_query_time | 300.000000 | 390 | 309 | dba | localhost | Sleep | NULL | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ 4 rows in set (0.00 sec) * 接下来,我们再断开309,重连时,processlist id 应该是310,这时候的结果就是100s了。这一点说明,在执行set global参数后进来的session才会生效,对当前session和之前的session不生效 dba:(none)> select * from performance_schema.variables_by_thread as a, (select THREAD_ID,PROCESSLIST_ID,PROCESSLIST_USER,PROCESSLIST_HOST,PROCESSLIST_COMMAND,PROCESSLIST_STATE from performance_schema.threads where PROCESSLIST_USER<>'NULL') as b where a.THREAD_ID = b.THREAD_ID and a.VARIABLE_NAME = 'long_query_time'; +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | THREAD_ID | PROCESSLIST_ID | PROCESSLIST_USER | PROCESSLIST_HOST | PROCESSLIST_COMMAND | PROCESSLIST_STATE | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ | 388 | long_query_time | 300.000000 | 388 | 307 | dba | localhost | Sleep | NULL | | 389 | long_query_time | 300.000000 | 389 | 308 | dba | localhost | Query | Sending data | | 391 | long_query_time | 100.000000 | 391 | 310 | dba | localhost | Sleep | NULL | +-----------+-----------------+----------------+-----------+----------------+------------------+------------------+---------------------+---------------------------------------------------------------+ 4 rows in set (0.00 sec)官方文档也不是很靠谱,也有很多差强人意的地方自己动手,测试验证的时候做好测试方案和计划,以免遗漏导致测试失败,得出错误的结论