---
title: "使用Anemometer基于pt-query-digest将MySQL慢查询可视化 "
author: "Horsemen"
date: "2016-03-18T15:45:14Z"
tags: []
categories: ["数据库"]
slug: "anemometer-pt-query-digest-mysql"
draft: false
lastmod: "2016-03-18T15:46:21Z"
id: 2284595
url: "https://horsemen.iteye.com/blog/2284595"
---

本文主要介绍使用Anemometer基于pt-query-digest将MySQL慢查询可视化，因为网上资料相对较少，且都是英文的，遇到报错也没有相关的参考资料，因此写此文。
准备条件:
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 脚本，创建用户：
mysql -uroot -pxxx < install.sql
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
 


查询后可获得慢查询日志的结果：
 








参考文章：
https://github.com/box/Anemometer


https://rtcamp.com/tutorials/mysql/slow-query-log-anemometer/