最近玩MySQL,發現了一個很不錯的工具,能夠把MySQL慢查詢可視化,方便咱們去找出和分析慢詢語句,搭建的步驟很少,但網上詳細教程比較少,說得也不夠詳細,一不當心,估計得蛋痛一會,哈哈php
Percona Toolkit 是一組高級的命令行工具,用來管理 MySQL 和系統任務,主要包括:
一、驗證主節點和複製數據的一致性
二、有效的對記錄行進行歸檔
三、找出重複的索引
四、總結 MySQL 服務器
五、從日誌和 tcpdump 中分析查詢
六、問題發生時收集重要的系統信息html
1、PT安裝:mysql
方法一:rpm包安裝git
[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: NOKEY Preparing... ########################################### [100%] 1:percona-toolkit ########################################### [100%]
若是yum 安裝不上依賴包,則檢查下epel源,也能夠查看http://www.cnblogs.com/xuanzhi201111/p/4040761.html去解決!github
方法二:源碼包安裝web
[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/binsql
2、下載Anemometer
官網:https://github.com/box/Anemometer數據庫
3、安裝httpd php,php版本要大於5.3,不然就報錯,除此以外還須要:bcmath,php必須支持pdo_mysql、php_mysqli模塊,下面咱們來安裝一下:apache
[root ~]$ yum install httpd php *bcmath* *mysqli* -y
若是epel源像如下的,說明是舊的,該源沒有php_mysqli相關模塊的vim
[root yum.repos.d]$ rpm -q epel-release epel-release-5-4.noarch
如下版本纔有:
[root yum.repos.d]$ rpm -q epel-release epel-release-6-8.noarch
個人mysql早已經安裝好了的,這裏就很少說了
4、將Anemometer文件包解壓,重命名爲anemometer,並移動到/var/www/html 下(apache默認路徑)
[root ~]$ unzip Anemometer-master.zip [root ~]$ mv Anemometer-master /var/www/html/anemometer
5、導入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)
6、修改能夠視化界面的配置信息
修改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 d ate_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表,哈哈,彆着急哈^.^
7、將慢查詢日誌經過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/
做者:陸炫志 出處:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111 您的支持是對博主最大的鼓勵,感謝您的認真閱讀。本文版權歸做者全部,歡迎轉載,但請保留該聲明。 |