MySQL資料庫慢日誌分析工具mysqlsla使用教程

來源:互聯網
上載者:User


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
# 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語句
除了以上的輸出, 官方還提供了很多定製化參數, 是一款不可多得的好工具.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.