数据库管理员一般是用percona的toolkit工具来分析MySQL慢查询记录,但是不够直观。

下面介绍一款比较直观的工具来统计分析MySQL慢查询记录anemometer。

在使用之前需要安装percona的toolkit工具,anemometer提供web界面。

anemometer介绍参见:https://github.com/box/Anemometer/wiki

1. 安装

# cd /data/www/my.ttlsa.com
# git clone https://github.com/box/Anemometer.git anemometer
# cd anemometer
1
2
3
# cd /data/www/my.ttlsa.com
# git clone https://github.com/box/Anemometer.git anemometer
# cd anemometer

2. 创建表和用户名

mysql < install.sql
mysql -e "grant ALL ON slow_query_log.* to 'anemometer'@'localhost' IDENTIFIED BY 'my.ttlsa.com';"
mysql -e "grant SELECT ON *.* to 'anemometer'@'localhost' IDENTIFIED BY 'my.ttlsa.com';"
1
2
3
mysql<install.sql
mysql-e"grant ALL ON slow_query_log.* to 'anemometer'@'localhost' IDENTIFIED BY 'my.ttlsa.com';"
mysql-e"grant SELECT ON *.* to 'anemometer'@'localhost' IDENTIFIED BY 'my.ttlsa.com';"

3. 分析MySQL慢日志

# pt-query-digest --user=anemometer --password=superSecurePass \
--review D=slow_query_log,t=global_query_review \
--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\"" /data/log/mysql/slow.log
1
2
3
4
# pt-query-digest --user=anemometer --password=superSecurePass \
                  --reviewD=slow_query_log,t=global_query_review\
                  --review-historyD=slow_query_log,t=global_query_review_history\
                  --no-report--limit=0%--filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\""  /data/log/mysql/slow.log

4. 配置anemometer

# cp conf/sample.config.inc.php conf/config.inc.php
# vi conf/config.inc.php
$conf['datasources']['localhost'] = array(
'host' => 'localhost',
'port' => 3306,
'db' => 'slow_query_log',
'user' => 'anemometer',
'password' => 'my.ttlsa.com',
'tables' => array(
'global_query_review' => 'fact',
'global_query_review_history' => 'dimension'
),
'source_type' => 'slow_query_log'
);

$conf['plugins'] = array(

'visual_explain' => '/usr/bin/pt-visual-explain',
'query_advisor' => '/usr/bin/pt-query-advisor',

#... other lines

$conn['user'] = 'anemometer';
$conn['password'] = 'my.ttlsa.com';

return $conn;
},

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# cp conf/sample.config.inc.php conf/config.inc.php
# vi conf/config.inc.php
$conf['datasources']['localhost']=array(
        'host'  =>'localhost',
        'port'  =>3306,
        'db'    =>'slow_query_log',
        'user'  =>'anemometer',
        'password'=>'my.ttlsa.com',
        'tables'=>array(
                'global_query_review'=>'fact',
                'global_query_review_history'=>'dimension'
        ),
        'source_type'=>'slow_query_log'
);
 
$conf['plugins']=array(
 
'visual_explain'=>'/usr/bin/pt-visual-explain',
'query_advisor'=>'/usr/bin/pt-query-advisor',
 
#... other lines
 
$conn['user']='anemometer';
$conn['password']='my.ttlsa.com';
 
return$conn;
},

5. 自动化处理

# vi /etc/logrotate.d/mysql
postrotate
pt-query-digest --user=anemometer --password=superSecurePass \
--review D=slow_query_log,t=global_query_review \
--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\"" /data/log/mysql/slow.log.1
endscript
1
2
3
4
5
6
7
# vi /etc/logrotate.d/mysql
postrotate
pt-query-digest--user=anemometer--password=superSecurePass\
                  --reviewD=slow_query_log,t=global_query_review\
                  --review-historyD=slow_query_log,t=global_query_review_history\
                  --no-report--limit=0%--filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\""/data/log/mysql/slow.log.1
endscript

这样就可以通过my.ttlsa.com/anemometer来访问查看慢查询了。

参考文档:

https://www.box.com/blog/optimizing-mysql-performance-at-scale-with-anemometer-2/

http://isadba.com/?p=655

使用Anemometer分析MySQL慢查询记录的更多相关文章

  1. 十分钟部署Anemometer作为Mysql慢查询可视化系统

    前言 采用Anemometer将Mysql慢查询日志可视化,可以更便捷的查询慢查询日志,并根据时间戳进行历史查询.如下是单机版Anemometer部署的演示,实际应用中,为安全起见,建议把anemom ...

  2. 企业级中带你ELK如何实时收集分析Mysql慢查询日志

    什么是Mysql慢查询日志? 当SQL语句执行时间超过设定的阈值时,便于记录到指定的日志文件中或者表中,所有记录称之为慢查询日志 为什么要收集Mysql慢查询日志? 数据库在运行期间,可能会存在这很多 ...

  3. 利用pt-query-digest分析MySQL慢查询

    1.用法与参数说明 pt-query-digest [OPTIONS] [FILES] [DSN] --create-review-table ##当使用--review参数把分析结果输出到表中时,如 ...

  4. linux上开启和分析mysql慢查询日志

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  5. mysql随机查询记录的高效率方法

    mysql使用rand随机查询记录的高效率方法 一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真 ...

  6. MYSQL获得查询记录的行号

    对于获得查询记录的行号这一功能,Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的ROWNUM,MS SQL Server 则在 2005 版本中提供了ROW_NUMBER()函数. ...

  7. mysqlsla 分析mysql慢查询日志

    发现有一个工具mysqlsla,分析查询日志比 mysqldumpslow分析的会更清晰明了! 安装mysqlsla: 下载mysqlsla-2.03.tar.gz [root@yoon export ...

  8. 基于binlog来分析mysql的行记录修改情况(python脚本分析)

          最近写完mysql flashback,突然发现还有有这种使用场景:有些情况下,可能会统计在某个时间段内,MySQL修改了多少数据量?发生了多少事务?主要是哪些表格发生变动?变动的数量是怎 ...

  9. 基于binlog来分析mysql的行记录修改情况

    https://www.cnblogs.com/xinysu/archive/2017/05/26/6908722.html import pymysqlfrom pymysql.cursors im ...

随机推荐

  1. Spearman(斯皮尔曼) 等级相关

    Spearman相关系数又称秩相关系数,是利用两变量的秩次大小作线性相关分析,对原始变量的分布不作要求,属于非参数统计方法,适用范围要广些.对于服从Pearson相关系数的数据亦可计算Spearman ...

  2. Java-Shiro(四):Shiro

    https://blog.csdn.net/visket2008/article/details/78539334 不错的视屏教程,很实用:https://www.bilibili.com/video ...

  3. GPUImage API文档之GPUImageFramebuffer类

    GPUImageFramebuffer类用于管理帧缓冲对象,负责帧缓冲对象的创建和销毁,读取帧缓冲内容 属性 @property(readonly) CGSize size 说明:只读属性,在实现中, ...

  4. AngularJs - Javascript MVC 框架

    在2012年6月google发布了AngularJs 1.0稳定版, 并宣称:AngularJS可以让你扩展HTML的语法,以便清晰.简洁地表示应用程序中的组件,并允许将标准的HTML作为你的模板语言 ...

  5. 使用windbg抓取崩溃文件和分析的过程

    在软件编程中,崩溃的场景比较常见的.且说微软技术再牛X,也是会出现崩溃的场景.网上有一段Win98当着比尔盖茨蓝屏的视频非常有意思. (转载请指明出于breaksoftware的csdn博客)     ...

  6. 微信小程序 - 上拉加载更多组件

    详情用例看demo,点击下载示例:loadmore

  7. 微软BI 之SSRS 系列 - 使用 LookupSet 和 Adjacent Group 等高级技巧在报表中跨 Dataset 分组查询

    SSRS 报表中有一些高级的技巧,平常很少用到,下面我通过这个案例来展现一下如何在实际开发中使用它们,并且如何解决一些实际的需求. 这张报表分别统计了不同的 Product 产品在不同的月份的 Ord ...

  8. 高级NUMA参数

    Advanced NUMA Attributes You can use the advanced NUMA attributes to customize NUMA usage. Attribute ...

  9. javascript将算法复杂度从O(n^2)做到O(n)

    compare the difference of two giving array, return results: 1. elements in both array, 2. elements o ...

  10. Live555实战之交叉编译live555共享库

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 能够通过这个链接获得最新的live555源代码:Live555源代码下载 Live555 是一个为 ...