本文主要描述基于pt-query-digest工具對慢查詢日志進行監控的工具Query-digest-UI。(安裝、使用、介紹以及benren提供的改進版。)
本文中描述的內容與其他網站上對Query-digest-UI的安裝和使用稍有不同,因為本人對此工具稍做了調整。
歡迎轉載,請注明作者、出處。
作者:張正
blog:http://space.itpub.net/26355921?
QQ:176036317
如有疑問,歡迎聯系。
1.MySQL開啟慢查詢
2.安裝Percona Toolkit
rpm -qa|grep percona
percona-toolkit-2.2.7-1
下載(百度云盤):http://pan.baidu.com/s/1eQgQAhc
3.下載Query-Digest-UI
官網:https://github.com/kormoc/Query-Digest-UI
下載(百度云盤):http://pan.baidu.com/s/1gd1Nn1L
4.準備好php相關模塊(或者yum)
Apache, mod_php, php_pdo
5.啟動apache服務
rpm -qa|grep http
httpd-2.2.3-45.el5
安裝:
1.安裝Percona Toolkit:
rpm -ivh percona-toolkit-2.2.7-1.noarch.rpm
NOTES:
需要先安裝:perl-DBD-MySQL-3.0007-2.el5
但是安裝此模塊時,可能會報錯:
libmysqlclient.so.15()(64bit) is needed by perl-DBD-MySQL-3.0007-2.el5.x86_64
libmysqlclient.so.15(libmysqlclient_15)(64bit) is needed by perl-DBD-MySQL-3.0007-2.el5.x86_64
需要安裝 或?提供libmysqlclient.so.15
可在/usr/lib或者/usr/lib64下,對libmysqlclient.so.16或libmysqlclient.so.18 建立軟連接:libmysqlclient.so.15
ln -s /usr/lib64/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.15
2.將Query-Digest-UI-master.zip文件包解壓,重命名為slow,并移動到/var/www/html 下(apache默認路徑)
pwd
/var/www/html/slow
3.安裝php相關模塊:

4.執行setup 腳本,并創建用戶:
此處的install.sql腳本為Anemometer工具的腳本,該腳本創建的global_query_review和global_query_review_history表
比pt-query-digest工具創建的表多一些字段,能提供更詳細的信息。
[root@/var/www/html/slow]mysql -uroot -pxxx < install.sql
[root@/var/www/html/slow]mysql -uroot -pxxx
mysql > grant all on slow_query_log.* to 'slow'@'%' identified by 'test';
mysql > grant select on *.* to ?'slow'@'%';
mysql > grant all on slow_query_log.* to 'slow'@'localhost';
mysql > grant select on *.* to ?'slow'@'localhost';

5.將慢查詢日志通過pt-query-digest分析后存入db.example.com主機上的數據庫中:
pt-query-digest --user=slow --password=test \
? ? ? ? ? ? ? ? ? --review h=db.example.com,D=slow_query_log,t=global_query_review \
? ? ? ? ? ? ? ? ? --review-history h=db.example.com,D=slow_query_log,t=global_query_review_history \
? ? ? ? ? ? ? ? ? --no-report --limit=0% \?
? ? ? ? ? ? ? ? ? --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" \?
? ? ? ? ? ? ? ? ? /root/test/localhost-slow.log
pt 版本大于2.2版本的語句:
pt-query-digest --user=slow --password=test --review h=db.example.com,D=slow_query_log,t=global_query_review --history h=hd-119-186,D=slow_query_log,t=global_query_review_history --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /root/test/localhost-slow.log
NOTES:
1.此處的$HOSTNAME 的值為執行該命令主機的主機名。
2.如果是用一臺服務器監控多臺mysql數據庫的慢查詢,修改 --filter 中 ?$event->{hostname}=\"host2\""
3.或者將host2改為IP,即主機名顯示為IP,取本機局域網IP:
將host2改為:
`ifconfig eth0|grep "inet addr"|awk '{print $2}'|awk -F':' '{print $2}'`
執行這個操作后,將會把慢日志分析后存入mysql的slow_query_log數據庫。記得保證相應 '用戶'@'主機' 的訪問權限。
6.修改數據庫連接信息:
pwd/var/www/html/slow
cp config.php.example config.php
vi config.php
. . . . . .?
$reviewhost = array(
// Replace hostname and database in this setting
// use host=hostname;port=portnum if not the default port
? ? 'dsn' ? ? ? ? ? => 'mysql:host=hostname;port=3306;dbname=slow_query_log',
? ? 'user' ? ? ? ? ?=> 'slow',
? ? 'password' ? ? ?=> 'test',
// See http://www.percona.com/doc/percona-toolkit/2.1/pt-query-digest.html#cmdoption-pt-query-digest--review
? ? 'review_table' ?=> 'global_query_review',
// This table is optional. You don't need it, but you lose detailed stats
// Set to a blank string to disable
// See http://www.percona.com/doc/percona-toolkit/2.1/pt-query-digest.html#cmdoption-pt-query-digest--review-history
? ? 'history_table' => 'global_query_review_history',
);
注意正確填寫紅色字體部分變量的值。
7.通過網頁訪問:(注:目前發現該工具不支持360瀏覽器)
http://192.168.44.130/slow
效果圖:
?

8.發現如果該工具監控多臺MySQL服務器,慢查詢日志又非常多的情況下,會發現非常不方便。
我對這個工具進行了修改,增加了hosts列,可通過選擇框選擇相應的值進行過濾:

下載(百度云盤):http://pan.baidu.com/s/1hqoUcpm
9.這個監控界面已經有了對主機名的過濾篩選,但是如果用于實際的生產環境中來監控,還是顯得不夠直觀。
我對這個工具又進行了進一步的修改,界面如下:

下載(百度云盤):http://pan.baidu.com/share/link?shareid=3651139099&uk=2550236109
2.第二列為總執行次數
3.第三列為總的執行時間
4.第四列為平均執行時間
5.第五列為平均返回行數
6.第六列為平均掃描行數
7.增加了第七列,使用時間來進行過濾。
進入本監控頁面,默認是顯示所有慢查詢SQL的前10條。然后可通過該選項列表中選擇 最近3天、最近7天、10天以前、以及可以單獨選擇從從10天以前到現在的每一天中的SQL情況。
實際生產的監控環境,可通過Last executed和Monitored Hosts來過濾我們需要查看的慢查詢SQL。
最后附上該工具的完整文檔:

本文轉自ITPUB博客84223932的博客,原文鏈接:Query-digest-UI監控慢查詢,以及此工具的改進版,如需轉載請自行聯系原博主。