First we need to understand the four parameters:
slow_query_log # 是否开启慢查询日志,默认OFF,开启则设置为 ON。 slow_query_log_file # 慢查询日志文件存储位置。 log_queries_not_using_indexes # 是否把没有使用到索引的SQL记录到日志中,默认OFF,开启则设置为 ON。 long_query_time # 超过多少秒的查询才会记录到日志中,注意单位是秒。
Then execute the statement settings (this method will become invalid after restarting MySQL)
(Related video tutorial Recommended: mysql video tutorial)
After knowing the meaning of the parameters, we can directly set the parameters we need. I am using global settings here.
SET GLOBAL slow_query_log = 'ON'; SET GLOBAL slow_query_log_file = '文件路径(绝对路径)'; SET GLOBAL log_queries_not_using_indexes = 'ON'; SET GLOBAL long_query_time = 1; # 这里需要注意下,long_query_time参数设置后需要下次会话后才生效,当前会话查询还是原来的数值
Last modification of the configuration file (permanent settings)
If you want it to not be invalid after restarting MySQL, you can modify the MySQL configuration file. Add the following statement in the configuration file:
slow_query_log="ON" slow_query_log_file="文件路径(绝对路径)" log_queries_not_using_indexes="ON" long_query_time=1
It will take effect after restarting.
Recommended related articles and tutorials: mysql tutorial
The above is the detailed content of How to enable mysql slow query log. For more information, please follow other related articles on the PHP Chinese website!