mysql> show variables like '%slow_query_log%';
+---------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /application/mysql/data/localhost-slow.log |
+---------------------+--------------------------------------------+
2 rows in set (0.01 sec)
输入 语句修改(重启后失效,建议在/etc/my.cnf中修改永久生效)
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.11 sec)
再次查看
mysql> show variables like '%slow_query_log%';
+---------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /application/mysql/data/localhost-slow.log |
+---------------------+--------------------------------------------+
2 rows in set (0.00 sec)
mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
查看慢查询日志
[root@localhost data]# cat /application/mysql/data/localhost-slow.log
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
/application/mysql/bin/mysqld, Version: 5.5.51-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
# Time: 170605 6:37:00
# User@Host: root[root] @ localhost []
# Query_time: 2.000835 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1496615820;
select sleep(2);
通过MySQL命令查看有多少慢查询
mysql> show global status like '%Slow_queries%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 1 |
+---------------+-------+
1 row in set (0.00 sec)