# 慢查询日志的状态和位置
mysql> SHOW VARIABLES LIKE 'slow_query%';
+---------------------+---------------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/VM-4-4-centos-slow.log |
+---------------------+---------------------------------------+
# 慢查询日志的记录阈值
mysql> SHOW VARIABLES LIKE 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
# 开启慢查询日志
mysql> set global slow_query_log = on;
Query OK, 0 rows affected (0.01 sec)
# 查看开启后的慢查询日志状态
mysql> SHOW VARIABLES LIKE 'slow_query%';
+---------------------+---------------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/VM-4-4-centos-slow.log |
+---------------------+---------------------------------------+
# 设置慢查询日志的阈值,单位为秒
mysql> set global long_query_time = 1;
Query OK, 0 rows affected (0.00 sec)
# 重新连接会话才能看到修改后的值
mysql> SHOW VARIABLES LIKE 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
# 执行查询
mysql> select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set (0.02 sec)
mysql> select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set (0.02 sec)
mysql> select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set (0.01 sec)
# 慢查询日志
# Time: 2022-07-08T02:07:39.972786Z
# User@Host: root[root] @ localhost [] Id: 40
# Query_time: 0.014260 Lock_time: 0.000085 Rows_sent: 1 Rows_examined: 100000
use helloworld;
SET timestamp=1657246059;
select count(*) from sbtest1;
# Time: 2022-07-08T02:07:40.903657Z
# User@Host: root[root] @ localhost [] Id: 40
# Query_time: 0.015279 Lock_time: 0.000094 Rows_sent: 1 Rows_examined: 100000
SET timestamp=1657246060;
select count(*) from sbtest1;
# Time: 2022-07-08T02:07:41.757439Z
# User@Host: root[root] @ localhost [] Id: 40
# Query_time: 0.013896 Lock_time: 0.000085 Rows_sent: 1 Rows_examined: 100000
SET timestamp=1657246061;
select count(*) from sbtest1;
# 修改my.conf,在mysqld组种加入以下配置
[mysqld]
# dir是指具体的目录,filename是指具体的文件名称,如:/var/lib/mysql/VM-4-4-centos-slow.log
log-slow-queries=dir/filename
# t指的是具体阈值的时间,单位是秒
long_query_time=t
5. 慢查询日志清理
# 清理命令
[root@VM-4-4-centos mysql]# mysqladmin -uroot -p flush-logs
# 查看清理结果
[root@VM-4-4-centos mysql]# tail -f VM-4-4-centos-slow.log
SET timestamp=1657246060;
select count(*) from sbtest1;
# Time: 2022-07-08T02:07:41.757439Z
# User@Host: root[root] @ localhost [] Id: 40
# Query_time: 0.013896 Lock_time: 0.000085 Rows_sent: 1 Rows_examined: 100000
SET timestamp=1657246061;
select count(*) from sbtest1;
/usr/sbin/mysqld, Version: 5.7.32 (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。