修改mysql配置文件
vim /etc/my.cnf
slow_query_log = 1 #开启慢日志
long-query-time = 1 #慢查询时间 超过1秒则为慢查询
slow_query_log_file = /var/log/mysql_slow.log
测试是否生效
mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
cat mysql_slow.log
/usr/libexec/mysqld, Version: 5.1.73-log (Source distribution). started with:
Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 180319 14:59:34
# User@Host: root[root] @ localhost []
# Query_time: 3.000318 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1521442774;
select sleep(3);
# Time: 180319 15:00:17
# User@Host: root[root] @ localhost []
# Query_time: 2.000225 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1521442817;
select sleep(2);
生效了。