使用Anemometer基於pt-query-digest將MySQL慢查詢可視化

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

最近玩MySQL,發現了一個很不錯的工具,可以把MySQL慢查詢可以視化,方便我們去找出和分析慢詢語句,搭建的步驟不多,但網上詳細教程比較少,說得也不夠詳細,一不小心,估計得蛋痛一會,哈哈

 

Percona Toolkit 是一組進階的命令列工具,用來管理 MySQL 和系統任務,主要包括:
1、驗證主節點和複製資料的一致性
2、有效對記錄行進行歸檔
3、找出重複的索引
4、總結 MySQL 伺服器
5、從日誌和 tcpdump 中分析查詢
6、問題發生時收集重要的系統資訊

一、PT安裝:

方法一:rpm包安裝

[root ~]$ wget http://www.percona.com/downloads/percona-toolkit/LATEST/RPM/percona-toolkit-2.2.12-1.noarch.rpm[root ~]$ yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes -y[root ~]$ rpm -ivh percona-toolkit-2.2.12-1.noarch.rpm                    warning: percona-toolkit-2.2.12-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEYPreparing...                ########################################### [100%]   1:percona-toolkit        ########################################### [100%]

如果yum 安裝不上依賴包,則檢查下epel源,也可以查看http://www.cnblogs.com/xuanzhi201111/p/4040761.html去解決!

方法二:源碼包安裝

[root ~]$ wget http://www.percona.com/downloads/percona-toolkit/2.2.12/deb/percona-toolkit_2.2.12.tar.gz[root ~]$tar zxf  percona-toolkit_2.2.12.tar.gz[root ~]$cd percona-toolkit_2.2.12[root percona-toolkit-2.2.12]$perl Makefile.PL[root percona-toolkit-2.2.12]$ make && make install

工具安裝目錄在:/usr/local/bin

二、下載Anemometer
官網:https://github.com/box/Anemometer

 

三、安裝httpd php,php版本要大於5.3,否則就報錯,除此之外還需要:bcmath,php必須支援pdo_mysql、php_mysqli模組,下面我們來安裝一下:

[root ~]$ yum install  httpd php *bcmath* *mysqli*  -y

如果epel源像以下的,說明是舊的,該源沒有php_mysqli相關模組的

[root yum.repos.d]$ rpm -q epel-releaseepel-release-5-4.noarch

以下版本才有:

[root yum.repos.d]$ rpm -q epel-releaseepel-release-6-8.noarch

我的mysql早已經安裝好了的,這裡就不多說了

四、將Anemometer檔案包解壓,重新命名為anemometer,並移動到/var/www/html 下(apache預設路徑)

[root ~]$ unzip Anemometer-master.zip[root ~]$ mv Anemometer-master /var/www/html/anemometer

 

五、匯入anemometer目錄下的install.sql,並給該庫對應的許可權:

[root anemometer]$ pwd/var/www/html/anemometer[root anemometer]$  mysql -uroot -p123456 -S /data/mysql-5.5.40/mysql.sock <./mysql56-install.sql
mysql> grant all on slow_query_log.* to ‘anemometer‘@‘%‘ identified by ‘123456‘;Query OK, 0 rows affected (0.03 sec)mysql> grant all on slow_query_log.* to ‘anemometer‘@‘localhost‘ identified by ‘123456‘;  Query OK, 0 rows affected (0.00 sec)mysql>  grant select on *.* to  ‘anemometer‘@‘%‘;Query OK, 0 rows affected (0.00 sec)mysql> grant all on slow_query_log.* to ‘anemometer‘@‘localhost‘;Query OK, 0 rows affected (0.00 sec)mysql> grant select on *.* to  ‘anemometer‘@‘localhost‘;Query OK, 0 rows affected (0.00 sec)


六、修改可以視化介面的配置資訊

修改php配置,vim /etc/php.ini添加以下內容,(如果本來就有;extension = mysqli.so,只需要把分號去掉即可,我的是yum安裝的,本來沒有,所以自己加)

修改apache的設定檔,vim /etc/httpd/conf/httpd.conf

[root conf]$ cat /etc/httpd/conf/httpd.conf  |grep "ServerName"# ServerName gives the name and port that the server uses to identify itself.ServerName 192.168.1.128:80

重啟httpd,訪問不了,看httpd的日誌報以下錯:

date_default_timezone_set(): Timezone ID ‘CST‘ is invalid in /var/www/html/anemometer/lib/Anemometer.php on line 47[Fri Nov 28 15:47:57 2014] [error] [client 192.168.1.1] PHP Warning:  date_default_timezone_get(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Chongqing‘ for ‘CST/8.0/no DST‘instead in /var/www/html/anemometer/lib/Anemometer.php on line 48

修改下/etc/php.ini

如果重啟httpd,還是訪問不了,日誌還是報同時區的錯誤,則再修改以下的:

vim /var/www/html/anemometer/lib/Anemometer.php +47  添加下內容:

 

訪問:http://192.168.1.128/anemometer,會提示沒有global_query_review表,哈哈,別著急哈^.^

 

 

七、將慢查詢日誌通過pt-query-digest分析後存入資料庫中:

[root ~]$ pt-query-digest --user=anemometer --password=123456 --socket=/data/mysql-5.5.40/mysql.sock > --review h=localhost,D=slow_query_log,t=global_query_review > --history h=localhost,D=slow_query_log,t=global_query_review_history \ > --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /data/mysql-5.5.40/localhost-slow.log

如果你的mysql是通過sock方式串連的話,必須加上--socket指定sock檔案,執行完以上的命令,資料庫裡就會多出global_query_review 表和global_query_review_history表了

詳細匯入可以查看官方協助文檔和表屬性說明:

http://www.mysqlperformanceblog.com/2012/08/28/hidden-columns-of-query_review_history/

http://code.google.com/p/maatkit/wiki/EventAttributes


回到web端查看:

 

想瞭解更多功能,或者怎麼使用,這要看大家了^.^

可以參考文章:

https://github.com/box/Anemometer

http://blog.itpub.net/26355921/viewspace-1162415/

 

使用Anemometer基於pt-query-digest將MySQL慢查詢可視化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.