MySQL slow query log
Slow query log concept
The slow query log of MySQL is a log record provided by MySQL. It is used to record the statements whose response time exceeds the threshold in MySQL. Specifically, the SQL whose running time exceeds the long_query_time value will be recorded in the slow query log. The default value of long_query_time is 10, which means that the statement will run for more than 10S. By default, the Mysql database does not start the slow query log, and we need to manually set this parameter. Of course, if it is not required for tuning, it is generally not recommended to start this parameter, because turning on the slow query log will bring more or less certain benefits. performance impact. The slow query log supports writing log records to files, and also supports writing log records to database tables.
Parameters related to slow query logs
mysql> show variables like '%quer%'; |
The options marked in red boxes are:
-slow_query_log Whether to log slow queries. The "slow query" is determined by the value of the long_query_time variable.
-slow_query_log_file slow log file path
-long_query_time slow log execution time (seconds), the log will be recorded after the set time
Slow query log configuration
By default, the value of slow_query_log is OFF, which means that the slow query log is disabled. It can be enabled by setting the value of slow_query_log, as shown below:
mysql> show variables like '%slow_query_log%'; +---------------------+-----------------------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log | +---------------------+-----------------------------------------------+ 2 rows in set (0.00 sec)
mysql> set global slow_query_log=1; Query OK, 0 rows affected (0.09 sec)
mysql> show variables like '%slow_query_log%'; +---------------------+-----------------------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------------------+ | slow_query_log | ON | | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log | +---------------------+-----------------------------------------------+ 2 rows in set (0.00 sec) |
Using set global slow_query_log=1 to enable the slow query log only takes effect on the current database, and will become invalid if MySQL is restarted. If you want to make it permanent, you must modify the configuration file my.cnf (the same is true for other system variables).
Modify the my.cnf file, add or modify the parameters slow_query_log and slow_query_log_file, and then restart the MySQL server, as shown below
Linux: Add under the [mysqld] option of the /etc/my.cnf configuration file: slow_query_log=TRUE slow_query_log_file=/usr/local/mysql/slow_query_log.txt long_query_time=3 |
Windows: Add under the [mysqld] option of the my.ini configuration file: slow_query_log=TRUE slow_query_log_file=c:/slow_query_log.txt long_query_time=3 |
Restart the database to view
After restarting the database MYSQL, you can find that the query log has been enabled
systemctl restart mysqld |