English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to enable MySQL slow query log without restarting MySQL?

We can enable the MySQL slow query log by using the SET statement.

The following is the syntax.

SET GLOBAL slow_query_log = 'Value';

In the above syntax, the value can be ON / OFF fill. To enable the slow query log, let us view the query.

mysql> SET GLOBAL slow_query_log = 'ON';

To check if the slow query is enabled, please implement the following query-

mysql> SHOW GLOBAL VARIABLES LIKE 'slow\_%';

This is the output.

+---------------------+--------------------------+
| Variable_name | Value |
+---------------------+--------------------------+
| slow_launch_time | 2                        |
| slow_query_log | ON |                     
| slow_query_log_file | DESKTOP-QN2RB3H-slow.log |
+---------------------+--------------------------+
3 rows in set (0.00 sec)

We set the slow query time to seconds because if any query exceeds the given number of seconds, it will enter the slow query log file.

We can also set the number of seconds. This is the query to set the number of seconds.

mysql> SET GLOBAL long_query_time = 20;

Check if the time has been inserted.

mysql> SHOW GLOBAL VARIABLES LIKE 'long_query_time';

This is the output of the above query.

+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 20.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

After completing the above tasks, we need to refresh the logs.

mysql> FLUSH LOGS;

Note-We can permanently disable it with the my.cnf file. Set slow_query_log = 0; Disable.