在此和各位介绍mysqlsla,它是hackmysql.com推出的一款MySQL的日志分析工具,功能非常强大. 数据报表,非常有利于分析慢查询的原因, 包括执行频率, 数据量, 查询消耗等。
一、使用
mysqlsla -lt slow mysql-slow.log
或者
mysqlsla -lt slow mysql-slow.log -sf “+SELECT” -db dbName -top 10 -sort t_sum
参数意义
lt:表示日志类型,有slow, general, binary, msl, udl。
sf:[+-][TYPE],[TYPE]有SELECT, CREATE, DROP, UPDATE, INSERT,例如”+SELECT,INSERT”,不出现的默认是-,即不包括。
db:要处理哪个库的日志。
top:表示取按规则排序的前多少条。
sort:按某种规则排序,t_sum按总时间排序, c_sum按总次数排序。
二、分析结果
总查询次数 (queries total), 去重后的sql数量 (unique)
输出报表的内容排序(sorted by)
最重大的慢sql统计信息, 包括 平均执行时间, 等待锁时间, 结果行的总数, 扫描的行总数.
Count, sql的执行次数及占总的slow log数量的百分比.
Time, 执行时间, 包括总时间, 平均时间, 最小, 最大时间, 时间占到总慢sql时间的百分比.
95% of Time, 去除最快和最慢的sql, 覆盖率占95%的sql的执行时间.
Lock Time, 等待锁的时间.
95% of Lock , 95%的慢sql等待锁时间.
Rows sent, 结果行统计数量, 包括平均, 最小, 最大数量.
Rows examined, 扫描的行数量.
Database, 属于哪个数据库
Users, 哪个用户,IP, 占到所有用户执行的sql百分比
Query abstract, 抽象后的sql语句
Query sample, sql语句
三、安装
方法一
从http://hackmysql.com/mysqlsla下载mysqlsla,当前最新版为:2.03
tar zxvf mysqlsla-2.03.tar.gz
cp mysqlsla-2.03/bin/mysqlsla /usr/local/bin
如果提示“Can’t locate DBI.pm”,执行以下命令
yum install perl-DBI perl-DBD-MySQL -y
这时就可以使用mysqlsla了,用法如下
mysqlsla -lt slow slow.log
方法二#
wgethttp://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
-18:44:18- http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
正在解析主机 hackmysql.com… 64.13.232.157
Connecting to hackmysql.com|64.13.232.157|:80… 已连接。
已发出 HTTP 请求,正在等待回应… 200 OK
长度:33674 (33K) [application/x-tar]
Saving to: `mysqlsla-2.03.tar.gz’
100%[=====================================================================================>] 33,674 45.9K/s in 0.7s
18:44:20 (45.9 KB/s) – `mysqlsla-2.03.tar.gz’ saved [33674/33674]
# tar xzvf mysqlsla-2.03.tar.gz
mysqlsla-2.03/
mysqlsla-2.03/Changes
mysqlsla-2.03/INSTALL
mysqlsla-2.03/README
mysqlsla-2.03/Makefile.PL
mysqlsla-2.03/bin/
mysqlsla-2.03/bin/mysqlsla
mysqlsla-2.03/META.yml
mysqlsla-2.03/lib/
mysqlsla-2.03/lib/mysqlsla.pm
mysqlsla-2.03/MANIFEST
# cd mysqlsla-2.03
# ll
总计 68
drwxr-xr-x 2 1000 1000 4096 2008-11-11 bin
-rw-r-r- 1 1000 1000 5630 2008-11-11 Changes
-rw-r-r- 1 1000 1000 394 2008-07-13 INSTALL
drwxr-xr-x 2 1000 1000 4096 2008-11-11 lib
-rw-r-r- 1 1000 1000 389 2008-07-13 Makefile.PL
-rw-r-r- 1 1000 1000 152 2008-07-13 MANIFEST
-rw-r-r- 1 1000 1000 303 2008-11-11 META.yml
-rw-r-r- 1 1000 1000 1969 2008-11-11 README
# less INSTALL
Installing mysqlsla
===================
perl Makefile.PL
make
make install
Depending on your system, the mysqlsla script will be copied to some common bin
directory (/usr/local/bin/ for example). Then you should be able to run it from
the command line like: mysqlsla -lt slow LOG
For quick help, man mysqlsla
For all documentation and guides, visit http://hackmysql.com/mysqlsla
# less Makefile.PL
use 5.008004;
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => ‘mysqlsla’,
VERSION_FROM => ‘lib/mysqlsla.pm’,
PREREQ_PM => {},
($] >= 5.005 ?
(ABSTRACT_FROM => ‘lib/mysqlsla.pm’, # retrieve abstract from module
AUTHOR => ‘Daniel Nichter (http://hackmysql.com/contact)’) : ()),
EXE_FILES => [ 'bin/mysqlsla' ],
);
# perl Makefile.PL
Checking if your kit is complete…
Looks good
Writing Makefile for mysqlsla
# ll
总计 96
drwxr-xr-x 2 1000 1000 4096 2008-11-11 bin
-rw-r-r- 1 1000 1000 5630 2008-11-11 Changes
-rw-r-r- 1 1000 1000 394 2008-07-13 INSTALL
drwxr-xr-x 2 1000 1000 4096 2008-11-11 lib
-rw-r-r- 1 root root 22722 09-08 18:52 Makefile
-rw-r-r- 1 1000 1000 389 2008-07-13 Makefile.PL
-rw-r-r- 1 1000 1000 152 2008-07-13 MANIFEST
-rw-r-r- 1 1000 1000 303 2008-11-11 META.yml
-rw-r-r- 1 1000 1000 1969 2008-11-11 README
# make
cp lib/mysqlsla.pm blib/lib/mysqlsla.pm
cp bin/mysqlsla blib/script/mysqlsla
/usr/bin/perl “-MExtUtils::MY” -e “MY->fixin(shift)” blib/script/mysqlsla
Manifying blib/man3/mysqlsla.3pm
# make install
Installing /usr/lib/perl5/site_perl/5.8.8/mysqlsla.pm
Installing /usr/share/man/man3/mysqlsla.3pm
Installing /usr/bin/mysqlsla
Writing /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/auto/mysqlsla/.packlist
Appending installation info to /usr/lib/perl5/5.8.8/i386-linux-thread-multi/perllocal.pod
默认将命令mysqlsla安装到/usr/bin目录
执行mysqlsla -help时提示没有安装DBI模块,mysqlsla是用perl写的
第一次通过CPAN安装perl模块时,需要进行相关的配置,大部分配置采用默认值,一路回车即可
#perl -MCPAN -e shell
然后在下面的各个询问中可以改已经配置好的选项
然后执行下面的命令保存
CPAN>reload index
CPAN>reload cpan
安装DBI模块
CPAN>install DBI
如果是第一次以后进入CPAN系统,想修改以前的配置,可以在CPAN命令行(即CPAN>)键入以下指令:
CPAN>o conf init
同时要执行下面的命令保存
CPAN>reload index
CPAN>reload cpan
man mysqlsla可以看到mysqlsla的相关选项
# man mysqlsla
XXX
XXX WARNING: old character encoding and/or character set
XXX
mysqlsla(3) User Contributed Perl Documentation mysqlsla(3)
NAME
mysqlsla – Parse, filter, analyze and sort MySQL slow, general and binary logs
SYNOPSIS
# Basic operation: parse a MySQL slow or general log
mysqlsla -log-type slow LOG
mysqlsla -log-type general LOG
# Parse output from mysqlbinlog
# mysqlsla cannot directly parse binary logs
mysqlbinlog LOG │ mysqlsla -log-type binary -
# Parse a microslow patched slow log
mysqlsla -log-type msl LOG
# Replay a replay file
mysqlsla -replay FILE
# Parse a user-defined log specify its format
mysqlsla -log-type udl -udl-format FILE
# Let mysqlsla automatically determine the log type
mysqlsla LOG
DESCRIPTION
mysqlsla parses, filters, analyzes and sorts MySQL slow, general, binary and microslow patched slow logs.
It also supports user-defined logs.
This POD/man page is only a very brief outline of usage and command line options. For the full library of
mysqlsla documentation visithttp://hackmysql.com/mysqlsla.
# mysqlsla /tmp/mysqlslow.log | less
Auto-detected logs as slow logs
Report for slow logs: /tmp/mysqlslow.log
6 queries total, 6 unique
Sorted by ‘t_sum’
Grand Totals: Time 0 s, Lock 0 s, Rows sent 13, Rows Examined 13
______________________________________________________________________ 001 ___
Count : 1 (16.67%)
Time : 2.321 ms total, 2.321 ms avg, 2.321 ms to 2.321 ms max (47.20%)
Lock Time (s) : 629 ?s total, 629 ?s avg, 629 ?s to 629 ?s max (77.75%)
Rows sent : 8 avg, 8 to 8 max (61.54%)
Rows examined : 8 avg, 8 to 8 max (61.54%)
Database :
Users :
root@ 192.168.1.100 : 100.00% (1) of query, 100.00% (6) of all users
Query abstract:
SET timestamp=N; SHOW variables LIKE ‘S’;
Query sample:
SET timestamp=1252395365;
show variables like ‘%character%’;
……
格式说明如下:
总查询次数 (queries total), 去重后的sql数量 (unique)
输出报表的内容排序(sorted by)
最重大的慢sql统计信息, 包括平均执行时间, 等待锁时间, 结果行的总数, 扫描的行总数.
Count, sql的执行次数及占总的slow log数量的百分比.
Time, 执行时间, 包括总时间, 平均时间, 最小, 最大时间, 时间占到总慢sql时间的百分比.
95% of Time, 去除最快和最慢的sql, 覆盖率占95%的sql的执行时间.
Lock Time, 等待锁的时间.
95% of Lock , 95%的慢sql等待锁时间.
Rows sent, 结果行统计数量, 包括平均, 最小, 最大数量.
Rows examined, 扫描的行数量.
Database, 属于哪个数据库
Users, 哪个用户,IP, 占到所有用户执行的sql百分比
Query abstract, 抽象后的sql语句
Query sample, sql语句
除了以上的输出, 官方还提供了很多定制化参数, 是一款不可多得的好工具.