MySQLSlowlog慢查詢日誌的配置以及清空日誌內容

許願流星1號發表於2015-12-02

#配置slow log

#long_query_time 是指執行超過多久的sql會被log下來,這裡是1秒。

#log-slow-queries和slow_query_log_file 設定把日誌寫在哪裡

long_query_time = 1

slow_query_log

slow_query_log_file = D:\Program Files\MySQL\mysql5.6.27\data\mysql-slow.log

 

# Log queries that is not using indexes 未使用索引查詢配置和使用

#也會記錄到slow log裡

log-queries-not-using-indexes

 

 

 

 

 

[MySQLSlowlog]正確安全清空線上慢查詢日誌slowlog的流程

 

1, see the slow log status;

> 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)

 

2, stop the slow log server.

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)

 

mysql>

mysql> show variables like '%slow%'; -- check slow log status

+---------------------+------------------------------------------+

| 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)

 

3, reset the new path of slow log

mysql> set global slow_query_log_file='/mysqllog/slow_log/slow_queries_3306_new.log';

Query OK, 0 rows affected (0.03 sec)

 

4, start the slow log server

mysql>

mysql>

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)

 

5, check the slow sql in the new slow log file.

mysql> select sleep(10) as a, 1 as b;

+---+---+

| a | b |

+---+---+

| 0 | 1 |

+---+---+

1 row in set (10.00 sec)

 

mysql>

[mysql@xxx-xxx ~]$ more /mysqllog/slow_log/slow_queries_3306_new.log

......

Time Id Command Argument

# Time: 140213 6:44:24

# User@Host: root[root] @ localhost []

# Query_time: 10.000365 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0

SET timestamp=1392273864;

select sleep(10) as a, 1 as b;

 

6, backup the old big slow log file to other directory.

mv /mysqllog/slow_log/slow_queries_3306.log /mysqlbackup/slow_log/slow_queries_3306.log.bak.20140213

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29893219/viewspace-1851581/,如需轉載,請註明出處,否則將追究法律責任。

相關文章