php、mysql、redis 慢日志 slowlog

MySQl 慢日志

查看是否开启慢查询日志功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> show variables like 'slow_query%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/homestead-slow.log |
+---------------------+-----------------------------------+
2 rows in set (0.01 sec)

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.03 sec)
  • slow_query_log:开启状态(on/off)
  • slow_query_log_file:慢查询日志存放文件位置
  • long_query_time:超过多长时间会记录

临时配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 开启慢日志
mysql> set global slow_query_log='ON';
Query OK, 0 rows affected (0.01 sec)

# 指定存放路径
mysql> set global slow_query_log_file='/var/lib/mysql/slow.log'
-> ;
Query OK, 0 rows affected (0.02 sec)

# 配置超出时间
mysql> set global long_query_time=2;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'slow_query%';
+---------------------+-------------------------+
| Variable_name | Value |
+---------------------+-------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/slow.log |
+---------------------+-------------------------+
2 rows in set (0.00 sec)

永久配置

修改 mysql 配置文件

1
2
3
4
# Here you can see queries with especially long duration
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2

重启 mysql 完成。

Redis 慢日志

临时配置(当前会话)

1
2
3
4
5
127.0.0.1:6379> config set slowlog-log-slower-than 10000
OK
127.0.0.1:6379> config get slowlog-log-slower-than
1) "slowlog-log-slower-than"
2) "10000"

永久配置

修改配置文件

1
2
3
4
5
6
7
8
9
################################## SLOW LOG ###################################
# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 128

查看慢查询日志

1
2
3
4
5
6
7
8
127.0.0.1:6379> slowlog get
1) 1) (integer) 4 # 唯一标识符
2) (integer) 1638858424 # 执行时系统时间戳
3) (integer) 5 # 执行时长(微妙)
4) 1) "get" # 命令
2) "name"
5) "127.0.0.1:46102"
6) ""

PHP 慢日志

在配置文件 php-fpm.conf 或者 www.conf, 查找 slowlog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = log/$pool.log.slow

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 1

; Depth of slow log stack trace.
; Default Value: 20
request_slowlog_trace_depth = 20

设置的路径需要存在的路径,或提前手动创建

查看

1
2
3
4
5
6
[07-Dec-2021 07:16:41]  [pool www] pid 6249
script_filename = /home/vagrant/code/MeEdu/public/index.php
[0x00007f0c4c61c420] Composer\Autoload\includeFile() /home/vagrant/code/MeEdu/vendor/composer/ClassLoader.php:322
[0x00007f0c4c61c380] loadClass() unknown:0
[0x00007f0c4c61c320] spl_autoload_call() unknown:0
[0x00007ffe3c6eeb40] ???() /home/vagrant/code/MeEdu/public/index.php:55