pt-query-digest雖然功能強大, 但畢竟沒有web界面顯示的直觀,我們可以藉助Anemometer作爲pt-query-digest執行結果的顯示端,Anemometer是專門爲mysql的慢查詢開發的一款開源軟件,地址在http://www.oschina.net/p/anemometer
一、安裝LAMP環境
1,安裝
yum install httpd php *bcmath* *mysqli* -y
2,查看
[root@localhost ~]# rpm -qa|grep http
httpd-tools-2.4.6-40.el7.centos.x86_64
httpd-2.4.6-40.el7.centos.x86_64
[root@localhost ~]# rpm -qa|grep php
php-bcmath-5.4.16-36.el7_1.x86_64
php-cli-5.4.16-36.el7_1.x86_64
php-mysql-5.4.16-36.el7_1.x86_64
php-pear-MDB2-Driver-mysqli-1.5.0-0.8.b4.el7.noarch
php-common-5.4.16-36.el7_1.x86_64
php-pdo-5.4.16-36.el7_1.x86_64
php-process-5.4.16-36.el7_1.x86_64
php-pear-MDB2-2.5.0-0.9.b5.el7.noarch
php-5.4.16-36.el7_1.x86_64
php-xml-5.4.16-36.el7_1.x86_64
php-pear-1.9.4-21.el7.noarch
[root@localhost ~]# rpm -qa|grep bcmath
php-bcmath-5.4.16-36.el7_1.x86_64
4,修改httpd中的ServerName
vi /etc/httpd/conf/httpd.conf
二、Anemometer配置
1,執行SQL腳本
mysql -uroot -p123456 < /var/www/html/anemometer/install.sql
2,添加使用anemometer的權限
grant all on slow_query_log.* to 'anemometer'@'%' identified by '123456';
grant select on *.* to 'anemometer'@'%' identified by '123456';
給與'on *.* t'的權限是因爲做執行計劃時,每個表可能在不同的庫,需要use database;3,設置配置文件,修改 ip,host,port,db,user,password
cd /var/www/html/anemometer/conf
cp sample.config.inc.php config.inc.php
vi config.inc.php
4,啓動
servier httpd start
5,訪問
http://192.168.44.130/anemometer
三、遇到的問題
1,如果界面空白,查看日誌
vi /var/log/httpd/error_log
如果顯示如下錯誤,請關閉selinux: setenforce 0(最好在配置文件中設置,以免重啓後問題重現)
2, 界面顯示沒有global_query_review表
因爲我們在配置文件定義的slow_query_log庫中沒有這個表,畫面的任何數據都是從表裏取得的,而格式也是和pt-query-digest執行的結果相同,所以想要看到數據,需要先執行pt-query-digest並將結果導入到表的命令。
pt-query-digest --user=root --password=123456 --review h=127.0.0.1,D=slow_query_log,t=global_query_review --history h=127.0.0.1,D=slow_query_log,t=global_query_review_history --create-review-table /usr/local/mysql/log/slow.log
或者
pt-query-digest --user=anemometer --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
導入數據的命令一般配置定時任務,每隔多久將慢日誌導入到數據庫中,這個可以根據自己的需求去定製了
這樣就會存在global_query_review和global_query_review_history兩個表了。
執行後,刷新頁面
3,查看SQL具體信息
跳轉畫面後,點擊Explain Plan,可能出現 “Error in Query Explain Plugin: Missing field host” 的錯誤,需要修改配置文件
這個應該是動態的,但是目前是隻是一個測試庫,所以直接寫定db名
修改完成,重啓httpd
部署參考:http://www.tuicool.com/articles/Vny2Y3Y
-------------------------------------
順便推薦QQ羣,僅供學習和交流,歡迎大家的加入
Java : 139978466
MySQL: 167209377
大數據 : 181560406
前端 : 139978026
測試 : 299057857
運維 : 139978281