查看慢查询日志是否开启
SHOW VARIABLES LIKE '%slow_query_log%';
mysql> SHOW VARIABLES LIKE '%slow_query_log%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
2 rows in set (0.01 sec)
windows下默认开启,linux下默认关闭
查询慢查询日志多少秒才记录
show variables like 'long_query_time';
临时开启
set global slow_query_log=1;
设置位置
set global slow_query_log_file='/var/lib/mysql/localhost-1-slow.log';
设置阀值(重连mysql看到值变化)
set global long_query_time=2;
永久配置
修改配置文件达到永久配置状态:
/etc/mysql/conf.d/mysql.cnf
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/localhost-1-slow.log
long_query_time = 2
测试
通过运行下面的命令,达到问题 SQL 语句的执行:
mysql> select sleep(2);