一、查看慢查询日志是否开启:
show VARIABLES LIKE '%slow_query%';
+---------------------+-------------------------------------------------------------------+
| Variable_name | Value |
+---------------------+-------------------------------------------------------------------+
| slow_query_log | ON |
| slow_query_log_file | D:/javasoft/mysql-8.0.21-winx64/mysql-8.0.21-winx64/data/slow.log |
+---------------------+-------------------------------------------------------------------+
我的mysql已开启慢查询日志。
二、开启方式:
在mysql配置文件中添加如下配置(配置修改后需重启mysql服务):
#开启慢查询
slow_query_log = ON
#慢查询日志文件
slow_query_log_file = D:/javasoft/mysql-8.0.21-winx64/mysql-8.0.21-winx64/data/slow.log
#慢查询时间定义
long_query_time = 5
三、测试慢查询日志生效:
执行如下耗时操作:
SELECT SLEEP(5);
查看慢查询日志文件,即D:/javasoft/mysql-8.0.21-winx64/mysql-8.0.21-winx64/data/slow.log内容:
# Time: 2021-08-10T14:54:34.804101Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 9
# Query_time: 5.009142 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 1
SET timestamp=1628607269;
SELECT SLEEP(5);