問題描述:
mysql開啟慢查詢功能,再正常不過,那么存在這樣一種情況:慢查詢寫入的文件位置和文件名是指定好的,如果慢查詢時間設定嚴苛,不出意外,記錄慢查詢的單個文件大小會日益增大,幾十兆或者上百兆,帶來的問題也就是慢查詢日志分析處理不夠方便。
解決思路:
切割日志,再通過定時任務(crontab)按規定時間切分,一般按“天”切分。
測試環境:
mysql5.6和mysql5.7
實現方法:
方法一:通過mysqladmin flush-logs [log_type] (注:mysql5.6無法指定日志類型,見測試過程)
方法二:通定時任務每次去設定慢查詢路徑實現
說明:以下測試過程是方法一的測試,最終實現腳本見頁底。
測試過程:
1. 截取mysql5.7官方文檔中的mysqladm部分說明
flush-logs [log_type ...] Flush all logs. The mysqladmin flush-logs command permits optional log types to be given, to specify which logs to flush. Following the flush-logs command, you can provide a space-separated list of one or more of the following log types: binary, engine, error, general, relay, slow. These correspond to the log types that can be specified for the FLUSH LOGS SQL statement.
注意:“[log_type ...]” 這個附加參數僅適用於mysql5.7.x或以上版本,也就是說,mysql5.6可以使用mysqladm flush-logs命令,但不能指定刷新某種指定日志類型,這很重要,因為這樣意味着mysql5.6執行該命令,就刷新了所有類型的日志,所以mysql5.6根據具體情況選擇使用。
2. 確定一下mysql版本和對應的數據存放目錄(開起了binLog和慢查詢)
說明:以下只顯示二進制日志和慢查詢日志文件詳情,留意文件時間變化
[root@server-10 ~]# mysql -V mysql Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using EditLine wrapper [root@server-10 ~]# cd /var/lib/mysql [root@server-10 mysql]# touch * [root@server-10 mysql]# ls -l |grep -E "master-bin|server-10-slow.log" -rw-r----- 1 mysql mysql 177 Sep 15 10:53 master-bin.000001 -rw-r----- 1 mysql mysql 421 Sep 15 10:53 master-bin.000002 -rw-r----- 1 mysql mysql 885 Sep 15 10:53 master-bin.000003 -rw-r----- 1 mysql mysql 634 Sep 15 10:53 master-bin.000004 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000005 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000006 -rw-r----- 1 mysql mysql 154 Sep 15 10:53 master-bin.000007 -rw-r----- 1 mysql mysql 140 Sep 15 10:53 master-bin.index -rw-r----- 1 mysql mysql 1473 Sep 15 10:53 server-10-slow.log
3. 【測試】刷新二進制文件
[root@server-10 mysql]# mysqladmin -uroot -p flush-logs binary Enter password: [root@server-10 mysql]# ls -l |grep -E "master-bin|server-10-slow.log" -rw-r----- 1 mysql mysql 177 Sep 15 10:53 master-bin.000001 -rw-r----- 1 mysql mysql 421 Sep 15 10:53 master-bin.000002 -rw-r----- 1 mysql mysql 885 Sep 15 10:53 master-bin.000003 -rw-r----- 1 mysql mysql 634 Sep 15 10:53 master-bin.000004 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000005 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000006 -rw-r----- 1 mysql mysql 202 Sep 15 10:57 master-bin.000007 -rw-r----- 1 mysql mysql 154 Sep 15 10:57 master-bin.000008 //二進制日志已經更新為000008 -rw-r----- 1 mysql mysql 160 Sep 15 10:57 master-bin.index -rw-r----- 1 mysql mysql 1473 Sep 15 10:53 server-10-slow.log //留意時間未變化,只指定了binary,故慢查詢日志沒有更新
4. 刷新慢查詢日志文件
[root@server-10 mysql]# mysqladmin -uroot -p flush-logs slow Enter password: [root@server-10 mysql]# ls -l |grep -E "master-bin|server-10-slow.log" -rw-r----- 1 mysql mysql 177 Sep 15 10:53 master-bin.000001 -rw-r----- 1 mysql mysql 421 Sep 15 10:53 master-bin.000002 -rw-r----- 1 mysql mysql 885 Sep 15 10:53 master-bin.000003 -rw-r----- 1 mysql mysql 634 Sep 15 10:53 master-bin.000004 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000005 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000006 -rw-r----- 1 mysql mysql 202 Sep 15 10:57 master-bin.000007 -rw-r----- 1 mysql mysql 298 Sep 15 11:04 master-bin.000008 -rw-r----- 1 mysql mysql 160 Sep 15 10:57 master-bin.index -rw-r----- 1 mysql mysql 1656 Sep 15 11:04 server-10-slow.log //慢查詢文件刷新了(文件描述符已經改變),但這里不會生成多個文件
5. 歸納舊慢查詢日志,生成新慢查詢日志文件
[root@server-10 mysql]# mv server-10-slow.log server-10-slow.log.old [root@server-10 mysql]# ls -l |grep -E "master-bin|server-10-slow.log" -rw-r----- 1 mysql mysql 177 Sep 15 10:53 master-bin.000001 -rw-r----- 1 mysql mysql 421 Sep 15 10:53 master-bin.000002 -rw-r----- 1 mysql mysql 885 Sep 15 10:53 master-bin.000003 -rw-r----- 1 mysql mysql 634 Sep 15 10:53 master-bin.000004 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000005 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000006 -rw-r----- 1 mysql mysql 202 Sep 15 10:57 master-bin.000007 -rw-r----- 1 mysql mysql 298 Sep 15 11:04 master-bin.000008 -rw-r----- 1 mysql mysql 160 Sep 15 10:57 master-bin.index -rw-r----- 1 mysql mysql 1656 Sep 15 11:04 server-10-slow.log.old [root@server-10 mysql]# mysqladmin -uroot -p flush-logs slow Enter password: [root@server-10 mysql]# ls -l |grep -E "master-bin|server-10-slow.log" -rw-r----- 1 mysql mysql 177 Sep 15 10:53 master-bin.000001 -rw-r----- 1 mysql mysql 421 Sep 15 10:53 master-bin.000002 -rw-r----- 1 mysql mysql 885 Sep 15 10:53 master-bin.000003 -rw-r----- 1 mysql mysql 634 Sep 15 10:53 master-bin.000004 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000005 -rw-r----- 1 mysql mysql 202 Sep 15 10:53 master-bin.000006 -rw-r----- 1 mysql mysql 202 Sep 15 10:57 master-bin.000007 -rw-r----- 1 mysql mysql 442 Sep 15 11:18 master-bin.000008 -rw-r----- 1 mysql mysql 160 Sep 15 10:57 master-bin.index -rw-r----- 1 mysql mysql 183 Sep 15 11:18 server-10-slow.log //新生成的慢查詢日志,新的慢查詢會寫入該文件中 -rw-r----- 1 mysql mysql 1656 Sep 15 11:04 server-10-slow.log.old [root@server-10 mysql]# date Sat Sep 15 11:18:20 CST 2018
注意:不能單純地僅重命名,上面重命名成了server-10-slow.log.old,如果不刷新,那么新的慢查詢語句還會寫入server-10-slow.log.old中,linux下存在“文件描述符”這么一個概念,改名不能修改文件描述符。
解決方法:
回到主題:按天切分歸納慢查詢日志
方法一:通過mysqladmin flush-logs [log_type] (注:mysql5.6無法指定日志類型,見上文)
新建腳本:cut_slow_log.sh
#!/bin/bash
time=`date -d yesterday +"%Y-%m-%d"` user="root" passwd="xxxxxx" #提前創建好一個存放目錄:/var/lib/mysql/slow_log/ mv /var/lib/mysql/server-10-slow.log /var/lib/mysql/slow_log/server-10-slow-$time.log mysqladmin -u$user -p$passwd --socket=/var/lib/mysql/mysql.sock flush-logs slow
每天0點執行該腳本
0 0 * * * sh /root/cut_slow_log.sh > /dev/null 2>&1
方法二:通定時任務每次去設定慢查詢路徑實現
新建腳本:cut_slow_log.sh
#!/bin/bash time=`date -d yesterday +"%Y-%m-%d"` host="127.0.0.1" user="root" passwd="xxxxxx" #提前創建好一個存放目錄:/var/lib/mysql/slow_log/ mv /var/lib/mysql/server-10-slow.log /var/lib/mysql/slow_log/server-10-slow-$time.log mysql -u$user -p$passwd -e "set global slow_query_log_file='/var/lib/mysql/server-10-slow.log';"
每天0點執行該腳本
0 0 * * * sh /root/cut_slow_log.sh > /dev/null 2>&1
結束.
