设为首页 收藏本站
查看: 286|回复: 0

[经验分享] 使用Anemometer基于pt-query-digest将MySQL慢查询可视化

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-2 09:36:05 | 显示全部楼层 |阅读模式
If you're just completely itching to start using this tool, here's what you need:
1、a MySQL database to store query analysis data in.
2、pt-query-digest.You may as well just get the whole Percona Toolkit while you're at it :)
3、a slow query log from a MySQL server (see The Slow Query Log for info on getting one)
4、a webserver with PHP

The first first
1
2
[iyunv@xiaoya anemometer]# wget percona.com/get/percona-toolkit.rpm
[iyunv@xiaoya ~]# yum install -y percona-toolkit.rpm



Then,Pt-query-digest reference configuration is given
1
2
3
[code collapse="false"]
pt-query-digest --user=数据库用户名 --password=数据库密码 --review h=数据库IP和域名,D=slow_query_log,t=global_query_review --history h=数据库IP和域名,D=slow_query_log,t=global_query_review_history --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" 数据库日志文件地址
[/code]



next,Grammar and important options
pt-query-digest [OPTIONS] [FILES] [DSN]
--create-review-table 当使用--review参数把分析结果输出到表中时,如果没有表就自动创建。
--create-history-table 当使用--history参数把分析结果输出到表中时,如果没有表就自动创建。
--filter 对输入的慢查询按指定的字符串进行匹配过滤后再进行分析
--limit限制输出结果百分比或数量,默认值是20,即将最慢的20条语句输出,如果是50%则按总响应时间占比从大到小排序,输出到总和达到50%位置截止。
--host MySQL服务器地址
--user mysql用户名
--password mysql用户密码
--history 将分析结果保存到表中,分析结果比较详细,下次再使用--history时,如果存在相同的语句,且查询所在的时间区间和历史表中的不同,则会记录到数据表中,可以通过查询同一CHECKSUM来比较某类型查询的历史变化。
--review 将分析结果保存到表中,这个分析只是对查询条件进行参数化,一个类型的查询一条记录,比较简单。当下次使用--review时,如果存在相同的语句分析,就不会记录到数据表中。
--output 分析结果输出类型,值可以是report(标准分析报告)、slowlog(Mysql slow log)、json、json-anon,一般使用report,以便于阅读。
--since 从什么时间开始分析,值为字符串,可以是指定的某个”yyyy-mm-dd [hh:mm:ss]”格式的时间点,也可以是简单的一个时间值:s(秒)、h(小时)、m(分钟)、d(天),如12h就表示从12小时前开始统计。
--until 截止时间,配合—since可以分析一段时间内的慢查询。

Install  anemometer
First up, grab the anemometer code from github. Navigate to the document root of your web server and snag a copy of the Box Anemometer code.
1
[iyunv@xiaoya ~]# git clone git://github.com/box/Anemometer.git anemometer



or ,if you have 9418 port closed:
1
[iyunv@xiaoya ~]# git clone



then change your current working directory to the anemometer directory:
1
[iyunv@xiaoya ~]# cd anemometer



Next, you should connect to the MySQL database you're looking to store the analysis data in and issue the following command:
1
2
3
[iyunv@xiaoya anemometer]# mysql -uroot -p123456 < install.sql
[iyunv@xiaoya anemometer]# mysql -uroot -p123456 -e "grant ALL ON slow_query_log.* to
BY '123456';"



Next, grab that slow query log file you have (mine's called "slow.log"!), and run pt-query-digest on it: NOTE: I'm using a BASH 3.0 shell here on my MySQL database server! This is so the "$HOSTNAME" variable properly replaces with "db.example.com")
1
2
3
[iyunv@xiaoya ~]# rpm -qa|grep percona-toolkit

percona-toolkit-2.2.16-1.noarch



For pt-query-digest version < 2.2
1
2
3
4
5
6
7
8
9
10
11
pt-query-digest --user=anemometer --password=superSecurePass \

                  --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\"" \

                  /var/lib/mysql/db.example.com-slow.log



For pt-query-digest version >= 2.2
1
[iyunv@xiaoya ~]# pt-query-digest --user=anemometer --password=123456 --review D=slow_query_log,t=global_query_review --history D=slow_query_log,t=global_query_review_history --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /usr/local/mysql/var/slow.log



You may see an error like above, that's okay! TODO: explain what the options above are doing.
1
2
3
4
5
Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57A" isn't numeric in numeric gt (>) at (eval 40) line 6, <> line 27.

Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57B" isn't numeric in numeric gt (>) at (eval 40) line 6, <> line 28.

Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57C" isn't numeric in numeric gt (>) at (eval 40) line 6, <> line 29.




View the data!Now, navigate to the document root of your web server and copy the sample config so you can edit it:
1
2
[iyunv@xiaoya conf]# cd /home/wwwroot/anemometer/conf
[iyunv@xiaoya conf]# cp sample.config.inc.php config.inc.php



The sample config explains every setting you may want to change in it. At the very least, make sure you set the Datasource to the MySQL database you're storing the analyzed digest information in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$conf['datasources']['localhost'] = array(

    'host'  => 'db.example.com',

    'port'  => 3306,

    'db'    => 'slow_query_log',

    'user'  => 'anemometer',

    'password' => 'superSecurePass',

    'tables' => array(

        'global_query_review' => 'fact',

        'global_query_review_history' => 'dimension'

    )

);



In addition, the "explain" plugin is enabled by default in the current release and you'll need to setup the username and password it uses to an account that has privileges to explain queries on a given schema on a host. For example, if you're digesting slow logs that primarily contain queries from the "world" database on db.example.com, you'll need to ensure that the user account you put into the following section of the config has the necessary privileges on the "world" database on db.example.com. To do this, scroll down in the sample config to the section containing the plugins configuration and change the 'user' and 'password' parameters to an appropriate account:
1
2
3
4
5
6
7
8
$conf['plugins'] = array(
        ...
    'explain'       =>      function ($sample) {
        $conn['user'] = 'anemometer';
        $conn['password'] = 'superSecurePass';
        return $conn;
    },
);



Finally,you can look through
http://10.0.0.5/anemometer/


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-251995-1-1.html 上篇帖子: mysql数据库清理系统日志(针对主从复制主服务器的清理) 下篇帖子: RedHat6.6上安装MySQL5.7.13
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表