Home > Backend Development > PHP Tutorial > How to enable mysql and php slow logs

How to enable mysql and php slow logs

藏色散人
Release: 2023-04-07 21:46:02
forward
3023 people have browsed it

Open mysql slow log

MySQL's slow query log is a log record provided by MySQL. It is used to record statements whose response time exceeds the threshold in MySQL, specifically referring to SQL that takes longer than the long_query_time value will be recorded in the slow query log. The default value of long_query_time is 10, which means running statements for more than 10 seconds.

By default, the Mysql database does not start the slow query log. We need to manually set this parameter. Of course, if it is not needed for tuning, it is generally not recommended to start this parameter, because turning on the slow query log will cause or More or less will have a certain performance impact. The slow query log supports writing log records to files and also supports writing log records to database tables.

1. Method 1 to enable mysql slow log (temporary, restarting mysql will fail):

Log in to mysql on the server and view the current slow query log status

mysql> show variables like '%slow_query_log%';
+---------------------+-----------------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /usr/local/mysql/var/localtest-slow.log |
+---------------------+-----------------------------------------+
2 rows in set (0.00 sec)
mysql> show variables like '%long_query_time%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
Copy after login

Enable slow log and set slow query time (seconds)

mysql> set global log_slow_queries=ON; mysql5.6以下版本
mysql> set global slow_query_log=ON; mysql5.6以上版本
mysql> set global slow_launch_time=10;
mysql> set long_query_time=3; mysql5.7
Copy after login

2. Method two to enable mysql slow log (effective permanently)

Edit mysql configuration file /etc/ my.cnf

[mysqld]
slow_query_log = 1 #是否开启慢查询日志,1表示开启,0表示关闭,也可以使用off和on
long_query_time = 5 #慢查询时间
log-slow-queries=/var/log/slowquery.log #mysql5.6以下版本
slow-query-log-file=/var/log/slowquery.log #mysql5.6及以上版本
Copy after login

Enable php slow log

Edit php-fpm.conf

request_slowlog_timeout = 5 执行时间大于5秒记录慢日志,0表示关闭慢日志
slowlog = /var/log/php-fpm/www-slow.log 指定慢日志路径
Copy after login

After the modification is completed, you need to restart php

Recommended: "PHP Tutorial"

The above is the detailed content of How to enable mysql and php slow logs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:whsir.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template