时间:2021-07-01 10:21:17 帮助过:7人阅读
1)、查看默认慢查询的时间:
mysql> show variables like "%long%";+--------------------+-----------+| Variable_name | Value |+--------------------+-----------+| long_query_time | 10.000000 || max_long_data_size | 1048576 |+--------------------+-----------+2 rows in set (0.24 sec)2)、修改慢查询时间,建议设置为5秒以内,不要设置过大,不然就失去了慢查询的作用了。
mysql> set global long_query_time=2;Query OK, 0 rows affected (0.25 sec)这里设置慢查询时间为2秒。
3)、查看慢查询是否开启
mysql> show variables like "%slow%";+---------------------+-----------------------------------------+| Variable_name | Value |+---------------------+-----------------------------------------+| log_slow_queries | OFF || slow_launch_time | 2 || slow_query_log | OFF || slow_query_log_file | /usr/local/mysql/var/li411-195-slow.log |+---------------------+-----------------------------------------+4 rows in set (0.23 sec)我们可以看到慢查询未开启。
4)、开启慢查询
mysql> set slow_query_log='ON';ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL报错了,这里需要注意一下的是设置时需要加上 global 关键字。
mysql> set global slow_query_log='ON';Query OK, 0 rows affected (1.11 sec)再次查看是否开启:
mysql> show variables like "%slow%";+---------------------+-----------------------------------------+| Variable_name | Value |+---------------------+-----------------------------------------+| log_slow_queries | ON || slow_launch_time | 2 || slow_query_log | ON || slow_query_log_file | /usr/local/mysql/var/li411-195-slow.log |+---------------------+-----------------------------------------+4 rows in set (4.04 sec)我们可以看到mysql 慢查询已经生效了。
long_query_time = 2log-slow-queries = /usr/local/mysql/var/localhost-slow.log重启mysql服务即可。