mysql开启关闭慢查询日志

时间:2022-06-01 19:12:54

在日常开发中,为了监控数据库的执行情况,往往我们需要开启慢查询日志。通过检查慢查询日志来确定sql的方向。但是长时间的运行势必导致慢查询日志过大,这种情况就需要将旧的慢查询日志保存起来,然后重新生成一份慢查询日志。
————参考博客:正确安全清空在线慢查询日志slow log的流程

查看当前慢查询的信息

mysql> show variables like '%slow%';
 --------------------- ------------------------------------------ 
| Variable_name       | Value                                    |
 --------------------- ------------------------------------------ 
| log_slow_queries    | ON                                       |
| slow_launch_time    | 2                                        |
| slow_query_log      | ON                                       |
| slow_query_log_file | /mysqllog/slow_log/slow_queries_3306.log |
 --------------------- ------------------------------------------ 
4 rows in set (0.00 sec)

停止当前慢查询日志

mysql> set global slow_query_log=0;
Query OK, 0 rows affected (0.27 sec)

mysql> show variables like '%slow%';
 --------------------- ------------------------------------------ 
| Variable_name       | Value                                    |
 --------------------- ------------------------------------------ 
| log_slow_queries    | OFF                                      |
| slow_launch_time    | 2                                        |
| slow_query_log      | OFF                                      |
| slow_query_log_file | /mysqllog/slow_log/slow_queries_3306.log |
 --------------------- ------------------------------------------ 
4 rows in set (0.00 sec)

重命名旧的慢查询日志

mv slow.log slow.log.01

或者重新设置一个慢查询日志路径

mysql> set global slow_query_log_file='/mysqllog/slow_log/slow_queries_3306_new.log';
Query OK, 0 rows affected (0.03 sec)

开启慢查询日志功能

mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like '%slow%';
 --------------------- ---------------------------------------------- 
| Variable_name       | Value                                        |
 --------------------- ---------------------------------------------- 
| log_slow_queries    | ON                                           |
| slow_launch_time    | 2                                            |
| slow_query_log      | ON                                           |
| slow_query_log_file | /mysqllog/slow_log/slow_queries_3306_new.log |
 --------------------- ---------------------------------------------- 
4 rows in set (0.00 sec)