mysql慢查詢日誌開啟和儲存格式,mysql日誌開啟儲存

來源:互聯網
上載者:User

mysql慢查詢日誌開啟和儲存格式,mysql日誌開啟儲存

mysql版本是mysql5.6.22,安裝環境windows7。


1.使用該查詢日誌可以找到有效率問題的sql語句,並記錄下來,進行監控。


可以使用如下語句查詢和設定慢查詢日誌


(1) 查看慢查詢日誌是否開啟
SHOW VARIABLES LIKE '%show_query_log%'; 或是 SHOW VARIABLES LIKE '%show_query_log%'\G (mysql命令列中使用)
查詢結果如下
Variable_name: slow_query_log
        Value: OFF
上邊Value表示未開啟
Variable_name: slow_query_log_file
        Value: D:\ProgramFiles\MySQL5.6.22\mysql_master\data\LHY-slow.log

上邊Value表示日誌存放的路徑

設定開啟狀態:

set global slow_query_log = on;


(2) 查看未使用索引的查詢日誌是否開啟
SHOW VARIABLES LIKE '%log_queries_not_using_indexes%'; 或是 SHOW VARIABLES LIKE '%log_queries_not_using_indexes%'\G (mysql命令列中使用)
查詢結果如下
Variable_name: log_queries_not_using_indexes
        Value: OFF

上邊Value表示未開啟

設定開啟狀態:

set global log_queries_not_using_indexes = on;


(3) 查看超過設定時間的查詢日誌
SHOW VARIABLES LIKE '%long_query_time%'; 或是 SHOW VARIABLES LIKE '%long_query_time%'\G (mysql命令列中使用)
查詢結果如下:
Variable_name: long_query_time
        Value: 10.000000

上邊value:10s表示記錄執行時間超過10秒的sql

設定執行時間為1s
set global long_query_time = 1;

exit;

注意1:修改執行之間之後要退出重新登入才會起作用.
注意2:設定時間過短導致日誌記錄過多會很快將磁碟空間沾滿,所以應該定期執行磁碟清理,這裡設定為1是為了查看執行效果,生產環境中需要自己設定.


以上三個步驟執行完成之後,從資料庫中執行任何sql語句就都會被記錄到日誌中,可以到第一步中那個日誌查看日誌資訊.


以上設定是在控制台處理的,當資料庫重啟之後,該設定即失效;
長期有效方式是在mysql安裝目錄下找到my.ini檔案,若是沒有該檔案,只有mysql-default.ini檔案
那麼就將該檔案備份,然後改名為mysql.ini,然後在該檔案中[mysqld]下邊添加如下的配置資訊即可。


slow_query_log=on
slow_query_log_file=D:/ProgramFiles/MySQL5.6.22/mysql_master/data/LHY-slow.log
log_queries_not_using_indexes=on
long_query_time=1


2.儲存格式


# Time: 150401 11:24:27
# User@Host: root[root] @ localhost [127.0.0.1]  Id:     7
# Query_time: 0.034002  Lock_time: 0.000000 Rows_sent: 3  Rows_examined: 3
use libu;
SET timestamp=1427858667;
select * from aaa;


分析如下:
(1) Time: 執行時間
(2) User@Host: 執行sql的主機資訊
(3) Query_time: sql的執行資訊,Lock_time: 鎖定時間, Rows_sent: 發送(結果)行數, Rows_examined:掃描的行數
(4) timestamp: 執行時間

(5) select * from aaa; : 查詢語句內容


3.慢查詢日誌分析工具

    5種工具: mysqldumpslow,mysqlsla,myprofi,mysql-explain-slow-log,mysqllogfilter

  mysqldumpslow mysql內建的分析工具

  致歉:本人目前還未找到這些工具在window上使用的方式,若是哪位大神有相關的教程,請@我,謝謝!!!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.