使用Anemometer基于pt-query-digest将MySQL慢查询可视化

来源:互联网 发布:plc 单片机的区别 编辑:程序博客网 时间:2024/06/05 15:09

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

准备条件:
1.MySQL开启慢查询


2.安装Percona Toolkit
rpm -qa|grep percona
percona-toolkit-2.2.7-1


3.下载Anemometer
官网:https://github.com/box/Anemometer


4.准备好php相关模块(或者yum)


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


2.将Anemometer文件包解压,重命名为anemometer,并移动到/var/www/html 下(apache默认路径)
pwd
/var/www/html/anemometer


3.安装php相关模块:
rpm -qa|grep php
php53-common-5.3.3-1.el5
php53-bcmath-5.3.3-1.el5
php53-mysql-5.3.3-1.el5
php53-dba-5.3.3-1.el5
php53-cli-5.3.3-1.el5
php53-gd-5.3.3-1.el5
php53-5.3.3-1.el5
php53-pdo-5.3.3-1.el5

NOTE:
PHP版本要大于5.3,否则就报错,如:
Anemometer requires PHP 5.3 or newer. You have 5.1.6
除此之外还需要:bcmath
rpm -qa|grep bcmath
php53-bcmath-5.3.3-1.el5


4.执行setup 脚本,创建用户:
[root@/var/www/html/anemometer]mysql -uroot -pxxx < install.sql
[root@/var/www/html/anemometer]mysql -uroot -pxxx
mysql > grant all on slow_query_log.* to 'anemometer'@'%' identified by 'test';
mysql > grant select on *.* to 'anemometer'@'%';
mysql > grant all on slow_query_log.* to 'anemometer'@'localhost';
mysql > grant select on *.* to 'anemometer'@'localhost';




5.将慢查询日志通过pt-query-digest分析后存入数据库中:
pt 版本小于2.2版本的语句:
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


pt 版本大于2.2版本的语句:
pt-query-digest --user=anemometer --password=test --review h=hd-119-186,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


执行这个操作后,将会把慢日志分析后存入mysql的slow_query_log数据库。


NOTE:
记得保证相应 '用户'@'主机' 的访问权限




6.修改可视界面的配置信息:
cd /var/www/html/anemometer/conf
cp sample.config.inc.php config.inc.php
vi config.inc.php

以及:


NOTE:
主机名一定要修改正确,不能保留为localhost,否则会导致最终界面无法访问。






7.最终本地通过IP访问:
http://192.168.44.130/anemometer



查询后可获得慢查询日志的结果:





0 0