配置慢查詢
默認的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