写作时间:2020-01-30
Docker: 2.1.0.2, MySQL: 8.0.9, MacOS: 10.15.2,
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time
值的SQL,则会被记录到慢查询日志中。long_query_time
的默认值为10
,意思是运行10秒
以上的语句。
默认情况下,Mysql数据库并不启动慢查询日志,需要我们手动来设置这个参数,当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。慢查询日志支持将日志记录写入文件,也支持将日志记录写入数据库表。
'ON'
(1)表示开启,'OFF'
(0)表示关闭。log_output='FILE'
表示将日志存入文件,默认值是'FILE'
。log_output='TABLE'
表示将日志存入数据库,这样日志信息就会被写入到mysql.slow_log
表中。log_output='FILE,TABLE'
。笔者从Docker启动MySQL容器开始,如需了解Docker如何安装MySQL,请参考:Docker安装MySQL 8 for Mac(图文详解)
% docker start docker-mysql
docker-mysql
进入MySQL容器
% docker exec -it docker-mysql bash
登录MySQL,
root@0d4a5de80d4d:/# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.19 MySQL Community Server - GPL
...
查看慢查询相关参数
mysql> show variables like 'slow_query%';
+---------------------------+----------------------------------+
| Variable_name | Value |
+---------------------------+----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/0d4a5de80d4d-slow.log |
+---------------------------+----------------------------------+
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
设置方法
方法一:全局变量设置
将 slow_query_log
全局变量设置为“ON”
状态
mysql> set global slow_query_log='ON';
查询超过1秒就记录. (注意:此方法需要退出数据库,重新登录才会设置成功。并且容器停止后就会失效。)
mysql> set global long_query_time=1;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
...
mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
方法二:配置文件设置
要想在容器中长期有效,需要退出数据库exit
, 在进入MySQL容器里面修改配置文件/etc/mysql/my.cnf
,
在[mysqld]
下的下方加入
[mysqld]
slow_query_log = ON
long_query_time = 1
重启MySQL服务
mysql> exit
Bye
root@0d4a5de80d4d:/# exit
exit
12:09 zgpeace@zgpeaces-MacBook-Pro /Users/zgpeace
% docker restart docker-mysql
docker-mysql
12:09 zgpeace@zgpeaces-MacBook-Pro /Users/zgpeace
% docker exec -it docker-mysql bash
root@0d4a5de80d4d:/# mysql -u root -p123456
mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
1.执行一条慢查询SQL语句
mysql> select sleep(2);
2.查看慢查询日志信息
# 退出sql,进入到容器里面
mysql> exit
Bye
# 慢查询日志记录
root@0d4a5de80d4d:/# cat /var/lib/mysql/0d4a5de80d4d-slow.log
/usr/sbin/mysqld, Version: 8.0.19 (MySQL Community Server - GPL). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
# Time: 2020-01-30T01:23:23.342345Z
# User@Host: root[root] @ localhost [] Id: 9
# Query_time: 2.000844 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 1
SET timestamp=1580347401;
select sleep(2);
log_output
参数是指定日志的存储方式。
日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源,因此对于需要启用慢查询日志,又需要能够获得更高的系统性能,那么建议优先记录到文件。
查询日志存储方式
mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
修改日志存储方式
mysql> set global log_output='FILE,TABLE';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%log_output%';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| log_output | FILE,TABLE |
+---------------+------------+
1 row in set (0.00 sec)
执行慢查询
mysql> select sleep(6);
+----------+
| sleep(6) |
+----------+
| 0 |
+----------+
1 row in set (6.04 sec)
查看慢查询记录
mysql> select * from mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+----------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+----------------------------------+-----------+
| 2020-01-30 04:57:08.674648 | root[root] @ localhost [] | 00:00:06.000849 | 00:00:00.000000 | 1 | 1 | | 0 | 0 | 1 | 0x73656C65637420736C656570283629 | 10 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+----------------------------------+-----------+
1 row in set (0.01 sec)
系统变量log-queries-not-using-indexes
:未使用索引的查询也被记录到慢查询日志中(可选项)。
如果调优的话,建议开启这个选项。另外,开启了这个参数,其实使用full index scan的sql也会被记录到慢查询日志。
mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF |
+-------------------------------+-------+
1 row in set (0.01 sec)
mysql> set global log_queries_not_using_indexes=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON |
+-------------------------------+-------+
1 row in set (0.01 sec)
系统变量log_slow_admin_statements表示是否将慢管理语句例如ANALYZE TABLE和ALTER TABLE等记入慢查询日志。
mysql> show variables like 'log_slow_admin_statements';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| log_slow_admin_statements | OFF |
+---------------------------+-------+
1 row in set (0.00 sec)
查询有多少条慢查询记录,可以使用系统变量Slow_queries
。
mysql> show global status like '%slow_queries%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 2 |
+---------------+-------+
1 row in set (0.01 sec)
在生产环境中,如果要手工分析日志,查找、分析SQL,显然是个体力活,MySQL提供了日志分析工具mysqldumpslow
退出用户登录MySQL到容器里面
mysql> exit
Bye
查看mysqldumpslow的帮助信息:
root@0d4a5de80d4d:/# mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v verbose
-d debug
-s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default
al: average lock time
ar: average rows sent
at: average query time
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is '*', i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don't subtract lock time from total time
参数解析:
-s
, 是表示按照何种方式排序,c
: 访问计数l
: 锁定时间r
: 返回记录t
: 查询时间al
:平均锁定时间ar
:平均返回记录数at
:平均查询时间-t
, 是top n的意思,即为返回前面多少条的数据;-g
, 后边可以写一个正则匹配模式,大小写不敏感的;得到返回记录集最多的10个SQL。
mysqldumpslow -s r -t 10 path/logfile
笔者机器举例
root@0d4a5de80d4d:/# mysqldumpslow -s r -t 10 /var/lib/mysql/0d4a5de80d4d-slow.log
Reading mysql slow query log from /var/lib/mysql/0d4a5de80d4d-slow.log
Count: 4 Time=4.25s (17s) Lock=0.00s (0s) Rows=1.0 (4), root[root]@localhost
select sleep(N)
Died at /usr/bin/mysqldumpslow line 162, <> chunk 3.
得到访问次数最多的10个SQL
mysqldumpslow -s c -t 10 path/logfile
笔者机器举例
root@0d4a5de80d4d:/# mysqldumpslow -s c -t 10 /var/lib/mysql/0d4a5de80d4d-slow.log
Reading mysql slow query log from /var/lib/mysql/0d4a5de80d4d-slow.log
Count: 4 Time=4.25s (17s) Lock=0.00s (0s) Rows=1.0 (4), root[root]@localhost
select sleep(N)
Died at /usr/bin/mysqldumpslow line 162, <> chunk 3.
得到按照时间排序的前10条里面含有左连接的查询语句。
mysqldumpslow -s t -t 10 -g “left join” path/logfile
笔者机器举例
mysqldumpslow -s t -t 10 -g "left join" /var/lib/mysql/0d4a5de80d4d-slow.log
Reading mysql slow query log from /var/lib/mysql/0d4a5de80d4d-slow.log
Died at /usr/bin/mysqldumpslow line 162, <> chunk 3.
另外建议在使用这些命令时结合 | 和more 使用 ,否则有可能出现刷屏的情况。
mysqldumpslow -s r -t 20 path/logfile | more
笔者机器举例
root@0d4a5de80d4d:/# mysqldumpslow -s r -t 20 /var/lib/mysql/0d4a5de80d4d-slow.log | more
Reading mysql slow query log from /var/lib/mysql/0d4a5de80d4d-slow.log
Died at /usr/bin/mysqldumpslow line 162, <> chunk 3.
Count: 4 Time=4.25s (17s) Lock=0.00s (0s) Rows=1.0 (4), root[root]@localhost
select sleep(N)
清除日志的方法,实际上就是删掉老表重新建一个新的.
SET GLOBAL slow_query_log = 'OFF';
ALTER TABLE mysql.slow_log RENAME mysql.slow_log_drop;
CREATE TABLE `mysql`.`slow_log` (
`start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`user_host` mediumtext NOT NULL,
`query_time` time(6) NOT NULL,
`lock_time` time(6) NOT NULL,
`rows_sent` int(11) NOT NULL,
`rows_examined` int(11) NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int(11) NOT NULL,
`insert_id` int(11) NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`sql_text` mediumblob NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';
SET GLOBAL slow_query_log = 'ON';
DROP TABLE mysql.slow_log_drop;
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_long_query_time
https://www.cnblogs.com/luyucheng/p/6265594.html
https://www.cnblogs.com/kerrycode/p/5593204.HTML
https://blog.csdn.net/zgpeace/article/details/104100413
https://dev.mysql.com/doc/refman/8.0/en/slow-query-log.html
https://blog.csdn.net/phker/article/details/83146676