MySQL slow query log is troubleshooting a SQL statement, as well as check the current MySQL performance is an important feature.
- Check whether to open slow query functions:
mysql> show variables like 'slow_query%'; +---------------------+------------------------------------+ | Variable_name | Value | +---------------------+------------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /var/lib/mysql/instance-1-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.00 sec)
Description:
slow_query_log slow queries on state
slow_query_log_file slow query log storage location (the directory needs to run the MySQL account write permission, usually set storage directory for MySQL data)
long_query_time query exceeds the number of seconds before the record
Configuration
Temporary configuration
Not enabled by default slow query log, temporarily turned on by the command:
mysql> set global slow_query_log='ON'; Query OK, 0 rows affected (0.00 sec) mysql> set global slow_query_log_file='/var/lib/mysql/instance-1-slow.log'; Query OK, 0 rows affected (0.00 sec) mysql> set global long_query_time=2; Query OK, 0 rows affected (0.00 sec)
Permanent configuration
Modify the configuration file reaches a permanent configuration status:
/etc/mysql/conf.d/mysql.cnf [mysqld] slow_query_log = ON slow_query_log_file = /var/lib/mysql/instance-1-slow.log long_query_time = 2
Once configured, you can restart the MySQL.
test
By running the following command, to issue SQL statements executed:
mysql> select sleep(2); +----------+ | sleep(2) | +----------+ | 0 | +----------+ 1 row in set (2.00 sec)
Slow query log and then view the contents:
$ cat /var/lib/mysql/instance-1-slow.log /usr/sbin/mysqld, Version: 8.0.13 (MySQL Community Server - GPL). started with: Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock Time Id Command Argument /usr/sbin/mysqld, Version: 8.0.13 (MySQL Community Server - GPL). started with: Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock Time Id Command Argument # Time: 2018-12-18T05:55:15.941477Z # User@Host: root[root] @ localhost [] Id: 53 # Query_time: 2.000479 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 SET timestamp=1545112515; select sleep(2);