mysql的慢查询配置和查看(mariadb)


首先需要开启mysql的慢查询,直接console查询是否:

show variables like "%slow%";

如果slow_query_log不为ON,则需要开启:

打开配置文件vim /etc/my.cnf.d/server.cnf,配置如下三项:

[mysqld]
slow_query_log = ON
slow_query_log_file = /datas/logs/mysql/slow.log
long_query_time = 1

记得创建对应目录并且授权:

#mkdir /datas/logs/mysql -p
#chown -R mysql.mysql /datas/logs/mysql

如果不知道是啥用户,可以查看数据库所在的文件的权限:ll /var/lib/mysql/

重启mariadb数据库:systemctl restart mysqld

找个慢查询超过一秒的执行一下, 即可查看慢查询日志:tail -f /datas/logs/mysql/slow.log

日志如下:

# Time: 240110 11:49:38
# User@Host: root[root] @  [000.000.000.000]
# Thread_id: 3  Schema: dbname  QC_hit: No
# Query_time: 1.635935  Lock_time: 0.000109  Rows_sent: 18308  Rows_examined: 1327054
# Rows_affected: 0  Bytes_sent: 175120
SET timestamp=1704858578;
SELECT tb1.id FROM tb1,tb2 WHERE ... ;

原文链接:https://blog.yongit.com/note/1573031.html