mysql percona-toolkit_Mysql-慢查询工具percona-toolkit的安装和使用

题记:先简单说明下percona-toolkit(简称pt)是个什么玩意儿?简单而言,PT工具是针对mysql慢查询的分析工具,他可以自定义将出现后的数据存放到制定的库表内(方便别的平台或者工具进一步使用)

1、安装percona-toolkit:

# yum -y install perl-IO-Socket-SSL

# yum -y install perl-DBI

# yum -y install perl-DBD-MySQL

# yum install perl-Digest-MD5 -y (解决Can't locate Digest/MD5.pm i的报错问题)

[root@mysql-cen74 tools]# pt-query-digest --help

Can't locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/pt-query-digest line 2434.

BEGIN failed--compilation aborted at /usr/bin/pt-query-digest line 2434.

# rpm -ivh percona-toolkit-2.2.6-1.noarch.rpm

[root@mysql-cen74 tools]# pt-query-digest --help (验证是否正常)

pt-query-digest analyzes MySQL queries from slow, general, and binary log files.

It can also analyze queries from C and MySQL protocol data

from tcpdump. By default, queries are grouped by fingerprint and reported in

descending order of query time (i.e. the slowest queries first). If no C

are given, the tool reads C. The optional C is used for certain

options like L and L. For more details, please use the

--help option, or try 'perldoc /usr/bin/pt-query-digest' for complete

documentation.

Usage: pt-query-digest [OPTIONS] [FILES] [DSN]

Options:

--ask-pass Prompt for a password when connecting to MySQL

--attribute-aliases=a List of attribute|alias,etc (default db|Schema)

--attribute-value-limit=i A sanity limit for attribute values (default

4294967296)

--charset=s -A Default character set

--config=A Read this comma-separated list of config files;

--help展示正常,就意味着安装成功了。

2、percona-toolkit使用:pt-query-digest命令

(前提:mysql本身的慢查询配置都要打开。)

命令1:分析本地的慢查询文件

pt-query-digest --user=root --password=root /var/lib/mysql/mysql-cen74-slow.log

命令2:将结果保存到相关的库表内

pt-query-digest --user=root --password=root --review h=localhost,D=test,t=query_review /var/lib/mysql/mysql-cen74-slow.log

重要参数说明:

pt-query-digest [OPTIONS] [FILES] [DSN]

--create-review-table 当使用--review参数把分析结果输出到表中时,如果没有表就自动创建。

--create-history-table 当使用--history参数把分析结果输出到表中时,如果没有表就自动创建。

--filter 对输入的慢查询按指定的字符串进行匹配过滤后再进行分析

--limit限制输出结果百分比或数量,默认值是20,即将最慢的20条语句输出,如果是50%则按总响应时间占比从大到小排序,输出到总和达到50%位置截止。

--host mysql服务器地址

--user mysql用户名

--password mysql用户密码

--history 将分析结果保存到表中,分析结果比较详细,下次再使用--history时,如果存在相同的语句,且查询所在的时间区间和历史表中的不同,则会记录到数据表中,可以通过查询同一CHECKSUM来比较某类型查询的历史变化。

--review 将分析结果保存到表中,这个分析只是对查询条件进行参数化,一个类型的查询一条记录,比较简单。当下次使用--review时,如果存在相同的语句分析,就不会记录到数据表中。

--output 分析结果输出类型,值可以是report(标准分析报告)、slowlog(Mysql slow log)、json、json-anon,一般使用report,以便于阅读。

--since 从什么时间开始分析,值为字符串,可以是指定的某个”yyyy-mm-dd [hh:mm:ss]”格式的时间点,也可以是简单的一个时间值:s(秒)、h(小时)、m(分钟)、d(天),如12h就表示从12小时前开始统计。

--until 截止时间,配合—since可以分析一段时间内的慢查询。

示例讲解:

[root@mysql-cen74 mysql]# pt-query-digest --user=root --password=root /var/lib/mysql/mysql-cen74-slow.log

# 260ms user time, 30ms system time, 26.20M rss, 239.13M vsz

# Current date: Thu Nov 22 13:34:07 2018

# Hostname: mysql-cen74.novalocal

# Files: /var/lib/mysql/mysql-cen74-slow.log

# Overall: 54 total, 21 unique, 0 QPS, 0x concurrency ____________________

# Attribute total min max avg 95% stddev median

# ============ ======= ======= ======= ======= ======= ======= =======

# Exec time 34ms 99us 16ms 633us 690us 2ms 236us

# Lock time 25ms 41us 15ms 468us 348us 2ms 98us

# Rows sent 12 0 2 0.22 0.99 0.49 0

# Rows examine 168 0 21 3.11 16.81 5.91 0.99

# Query size 8.38k 24 546 159 346.17 117.81 97.36

第一部分:总体统计结果

Overall: 总共有多少条查询,

Time range: 查询执行的时间范围。

unique: 唯一查询数量,即对查询条件进行参数化以后,总共有多少个不同的查询.

total: 总计 min:最小 max: 最大 avg:平均

95%: 把所有值从小到大排列,位置位于95%的那个数,这个数一般最具有参考价值。

median: 中位数,把所有值从小到大排列,位置位于中间那个数。

****************************************************************************

# Profile

# Rank Query ID Response time Calls R/Call V/M Item

# ==== ================== ============= ===== ====== ===== ===============

# 1 0x75D23FF1A4009CB8 0.0195 56.9% 2 0.0097 0.01 SELECT information_schema.processlist

# 2 0xE709DC2D53FA2AAB 0.0015 4.5% 8 0.0002 0.00 SELECT alarm_temp

# 3 0xEB5E2E5393F700F3 0.0014 4.1% 2 0.0007 0.00 SELECT information_schema.processlist

# 4 0x8F71C596EAA84C2D 0.0013 3.9% 8 0.0002 0.00 DELETE alarm_temp

# 5 0xF89E0231A9324457 0.0013 3.9% 2 0.0007 0.00 SELECT processlist

# 6 0x0C51E2B936B1C3A1 0.0012 3.4% 2 0.0006 0.00 SELECT information_schema.processlist

# 7 0x9AEDE4CC0E6828E1 0.0011 3.3% 2 0.0006 0.00 INSERT SELECT REPLACE mysql_status_history mysql_status

# 8 0x814B4FC4AE8D822D 0.0007 2.1% 2 0.0004 0.00 SELECT mysql_status db_servers_mysql

# 9 0xE0291E348A8A091E 0.0006 1.7% 2 0.0003 0.00 SELECT mysql_replication db_servers_mysql

# 10 0x1FE59AC243646472 0.0006 1.7% 2 0.0003 0.00 UPDATE db_status

# 11 0x7B61A38A6C3427FA 0.0005 1.6% 2 0.0003 0.00 UPDATE db_status

# 12 0x76280286C84D0186 0.0005 1.6% 2 0.0003 0.00 UPDATE db_status

# 13 0x9B2068E6EBD9D112 0.0005 1.5% 2 0.0003 0.00 UPDATE db_status

# 14 0xBCC5FD596E704DC3 0.0005 1.5% 2 0.0003 0.00 UPDATE db_status

# 15 0xD92E364C25910F3A 0.0005 1.5% 2 0.0003 0.00 SELECT db_servers_mysql

# 16 0xE982609CBA37D8B3 0.0005 1.4% 2 0.0002 0.00 DELETE mysql_status

# 17 0x6A7FB141929CA200 0.0005 1.4% 2 0.0002 0.00 SELECT db_status

# MISC 0xMISC 0.0014 4.0% 8 0.0002 0.0 <4 ITEMS>

第二部分:查询分组统计结果

Response: 总的响应时间。

time: 该查询在本次分析中总的时间占比。

calls: 执行次数,即本次分析总共有多少条这种类型的查询语句。

R/Call: 平均每次执行的响应时间。

Item : 查询对象

****************************************************************************

# Query 1: 0 QPS, 0x concurrency, ID 0x75D23FF1A4009CB8 at byte 11760 ____

# This item is included in the report because it matches --limit.

# Scores: V/M = 0.01

# Attribute pct total min max avg 95% stddev median

# ============ === ======= ======= ======= ======= ======= ======= =======

# Count 3 2

# Exec time 56 19ms 4ms 16ms 10ms 16ms 8ms 10ms

# Lock time 74 19ms 4ms 15ms 9ms 15ms 8ms 9ms

# Rows sent 0 0 0 0 0 0 0 0

# Rows examine 19 33 16 17 16.50 17 0.71 16.50

# Query size 2 196 98 98 98 98 0 98

# String:

# Databases information_schema

# Hosts

# Time 2018-11-22... (1/50%), 2018-11-22... (1/50%)

# Users root

# Query_time distribution

# 1us

# 10us

# 100us

# 1ms ################################################################

# 10ms ################################################################

# 100ms

# 1s

# 10s+

# Tables

# SHOW TABLE STATUS FROM `information_schema` LIKE 'processlist'\G

# SHOW CREATE TABLE `information_schema`.`processlist`\G

# EXPLAIN /*!50100 PARTITIONS*/

select * from information_schema.processlist where DB !='information_schema' and command !='Sleep'\G

第三部分:每一种查询的详细统计结果,

由下面查询的详细统计结果,最上面的表格列出了执行次数、最大、最小、平均、95%等各项目的统计。

Databases: 库名

Users: 各个用户执行的次数(占比)

Query_time distribution : 查询时间分布, 长短体现区间占比,本例中1s-10s之间查询数量是10s以上的两倍。

Tables: 查询中涉及到的表

Explain: 示例

****************************************************************************

3、其他用法:pt-query-digest命令用法示例

(1)直接分析慢查询文件:

pt-query-digest slow.log > slow_report.log

(2)分析最近12小时内的查询:

pt-query-digest --since=12h slow.log > slow_report2.log

(3)分析指定时间范围内的查询:

pt-query-digest slow.log --since '2014-04-17 09:30:00' --until '2014-04-17 10:00:00'> > slow_report3.log

(4)分析指含有select语句的慢查询

pt-query-digest--filter '$event->{fingerprint} =~ m/^select/i' slow.log> slow_report4.log

(5) 针对某个用户的慢查询

pt-query-digest--filter '($event->{user} || "") =~ m/^root/i' slow.log> slow_report5.log

(6) 查询所有所有的全表扫描或full join的慢查询

pt-query-digest--filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' slow.log> slow_report6.log

(7)把查询保存到query_review表

pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_review--create-review-table slow.log

(8)把查询保存到query_history表

pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_ history--create-review-table slow.log_20140401

pt-query-digest --user=root –password=abc123--review h=localhost,D=test,t=query_history--create-review-table slow.log_20140402

(9)通过tcpdump抓取mysql的tcp协议数据,然后再分析

tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txt

pt-query-digest --type tcpdump mysql.tcp.txt> slow_report9.log

(10)分析binlog

mysqlbinlog mysql-bin.000093 > mysql-bin000093.sql

pt-query-digest --type=binlog mysql-bin000093.sql > slow_report10.log

(11)分析general log

pt-query-digest --type=genlog localhost.log > slow_report11.log

你可能感兴趣的:(mysql,percona-toolkit)