数据库管理员一般是用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. Jsonp 关键字详解及json和jsonp的区别,ajax和jsonp的区别

    为什么要用jsonp? 相信大家对跨域一定不陌生,对同源策略也同样熟悉.什么,你没听过?没关系,既然是深入浅出,那就从头说起. 假如我写了个index页面,页面里有个请求,请求的是一个json数据(不 ...

  2. 【Kafka】Kafka-配置参数详解-参数调优

    Kafka-配置参数详解-参数调优 kafka 目录_百度搜索 为什么kafka使用磁盘而不是内存 - CSDN博客 Kafka 配置说明 - 風吹云动 - 博客园 kafka生产服务器配置 - Or ...

  3. Fixing the JavaScript typeof operator

    https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/javascript 类 ...

  4. [Jest] Set up Testing Globals in an Application with Jest

    For some React component testing, we have common setup in each test file: import { render } from 're ...

  5. Dijkstra和Floyd_warshall

    import java.util.Arrays; import java.util.Scanner; /*题目描写叙述: 有n个城市.城市间有m条道路.每条道路都有长度d.给你起点城市s终点终点t.要 ...

  6. Android改动包名称规范方法

    第一步.在项目上右键,选择android tools->rename application package,输入须要改为的名称,然后选择须要替换的文件里的包名.这里仅改动了project中包括 ...

  7. c++ 11 游记 之 decltype constexpr

    title: c++ 11 游记 1 keyword :c++ 11 decltype constexpr 作者:titer1 zhangyu 出处:www.drysaltery.com 联系:130 ...

  8. Creating objects on stack or heap

    class Player {  private: int health;  int strength;  int agility; public: void move(); void attackEn ...

  9. IOCCC(The International Obfuscated C Code Contest)

    国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...

  10. Linux中找出占用内存最多的前N个进程

    一.使用ps命令 ps -aux | sort -k4nr | head -N *命令详解: 1. head:-N可以指定显示的行数,默认显示10行. 2. ps:参数a指代all——所有的进程,u指 ...