数据库管理员一般是用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. MySQL数据库select语句的使用方法

    select语句可 以用回车分隔 $sql="select * from article where id=1"和 $sql="select * from article ...

  2. [Functional Programming] Running though a serial number prediction functions for tagging, pairing the result into object

    Let's we have some prediction functions, for each prediction function has a corresponding tag: const ...

  3. Python标准库:内置函数type(object)

    type(object) type(name, bases, dict) 本函数是返回对象的类型对象.仅仅有一个參数object时,直接返回对象的类型对象.假设仅仅是想推断一个对象是否属于某一个类的对 ...

  4. spring /spring boot中mock

    1 Mockito简介 1.1 Mockito是什么   Mockito是一个简单的流行的Mock框架.它允许你创建和配置mock对象.使用Mockito可以明显的简化对外部依赖的测试类的开发.一般使 ...

  5. 自然语言处理哪家强?【36kr】

    语音交互事关未来,国内外已经不少公司在抢蛋糕了,大公司收购.投资.合作不断,就可见一斑.目前,基本上所有的巨头都有涉足. 苹果收购 Siri.Novauris,组建基于神经网络算法的语音识别团队 20 ...

  6. Navicat标识为灰

    Navicat Premium作为一种通用数据管理工具,非常受迎接.最近,又遇到一个小问题.即为了实现自增,需要设置主键标识,而在Navicat中发现其为灰色,不可用. 原因:Navicat不允许设置 ...

  7. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...

  8. 两个自定义对象List列表取交集(intersection)

    public static void main(String[] args) { List<Fpxx> list = ListUtils.intersection(getFpList1() ...

  9. [转]Spring MVC之@RequestMapping 详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, ...

  10. top高级技能

    默认输入top命令后进入的是交互模式,默认显示的TOP程序界面如下图所示: 我们注意到进程的内存区域数据很长一串,主要是因为这里用的单位是k表示的,我们可以修改显示的单位,在界面上直接输入E可以改变显 ...