配置慢查詢
預設的my.cnf檔案在/etc/目錄下
vim /etc/my.cnf
#slow_query
slow_query_log=1#開啟慢查詢
slow_query_log_file=/var/lib/mysql/mysql-slow.log #設定慢查詢路徑
long_query_time=0.01#設定慢查詢時間
配置完要重啟mysql服務才生效
查詢慢查詢配置是否成功
測試慢查詢
use information_schema;
select * from tables;
語句執行時間花了:1.1秒,返回了314行記錄;
查看慢查詢檔案:cat mysql-slow.log
慢查詢記錄檔中記錄了每一天慢查詢記錄,每一條記錄都是以Time:開頭;記錄了包含登入資訊,查詢所花的時間,鎖花的時間,返回的行數,掃描的記錄數,執行的語句。
線上產生慢查詢記錄檔
1.關閉全域慢查詢
SET global slow_query_log=0
查詢是否關閉全域慢查詢
SHOW VARIABLES LIKE '%query_log%';
設定一個新的慢查詢檔案
SET global slow_query_log_file='/var/lib/mysql/mysql-slow_new.log'
開啟慢查詢
SET global slow_query_log=1;
SHOW VARIABLES LIKE '%query_log%';
在慢查詢路徑中可以看到產生了一個新的慢查詢檔案。
後面的慢查詢資訊會記錄到新的記錄檔當中,
這時我們可以mv以前的慢查詢檔案進行歸檔了。
重啟mysql服務
service mysql restart
SHOW VARIABLES LIKE '%query_log%';
當重啟mysql服務之後,慢記錄檔會重新變成之前的在my.cnf裡面設定的檔案,所以如果你想要重啟服務之後慢記錄檔還是剛才設定的 mysql-slow_new.log的話你就需要在修改全域設定的同時再修改my.cnf檔案,這樣就能保證重啟之後檔案還是之前修改的檔案。
總結
配置慢查詢對平時收集效能差的語句很有協助,也有很多工具專門來分析慢查詢日誌的,其中percona-toolkit就是一個很不錯的分析慢查詢的工具,如果沒有使用可以去瞭解一下。
mysql正確安全清空線上慢查詢日誌slow log
1, see the slow log status;
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)
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