数据库管理员一般是用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. PHP ECSHOP中 诡异的问题:expects parameter 1 to be double

    使用Ecshop给客户做了一个商城系统,在测试时发现后台在更改订单的配送方式时出现了以下问题 "PHP Warning: number_format() expects parameter ...

  2. [Spring Boot] Introduce to Mockito

    We have the implemetion: @SpringBootApplication public class MockitoDemoApplication { public static ...

  3. ASP入门(十八)-访问Access中的数据库

    Northwind 数据库 这是一个微软为软件的使用者提供的一个练习用的数据库,它模拟了一个商贸公司日常业务流程所需要的一些数据. 并且网络上大部分的 SQL 语句的讲解都是以这个数据库为例子讲解的. ...

  4. Android输出日志Log类

    android.util.Log常用的方法有以下5个: Log.v() Log.d() Log.i() Log.w() 以及 Log.e().根据首字母分别对应VERBOSE,DEBUG,INFO,W ...

  5. javascript简写精练

    一.算术运算符 var n = 5,v; 1.n = n*5; 2.n*=5; 同 n = n*5 二.条件判断 var b = true; 1.if (!false) { alert('true') ...

  6. Android 之 SharedPreferences应用

    Android 平台给我们提供了一个 SharedPreferences 类,它是一个轻量级的存储类,特别适合用于保存共享数据.使用SharedPreferences保存数据,其背后是用xml文件存放 ...

  7. 微信小程序 - 动态背景图片实现

    很简单-就两步 wxml(遍历style的background-image路径即可) wxss(.ab)

  8. Performance Tuning Guidelines for Windows Server 2012

    http://msdn.microsoft.com/en-us/library/windows/hardware/jj248719.aspx This guide describes importan ...

  9. unity3d与web网页通信

    总结一下: Unity3D 中的 C# 和 JavaScript 脚本之间是可以互相访问并交互的,但是要求这些被访问和操作的 C# 和 JavaScript 组件必须放在名为 Standard Ass ...

  10. Warning: Divide by zero.

    问题:如标题 解决方案:分母加上+eps   参考自:http://www.ilovematlab.cn/thread-43128-1-1.html