percona toolkit 简介

凤扬
2023-12-01

os: centos 7.4
db: mysql 5.7
software: toolkit 3.0.8

percona toolkit 是一款percona公司推出的优秀的开源的mysql分析工具。

##下载
https://www.percona.com/software/database-tools/percona-toolkit
https://www.percona.com/downloads/percona-toolkit/LATEST/

提供git支持
https://github.com/percona/percona-toolkit.git

文档
https://www.percona.com/doc/percona-toolkit/LATEST/index.html
https://www.percona.com/doc/percona-toolkit/3.0/index.html

##安装
主要有几种方式
1)yum Repositories方式
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum list|grep -i percona
yum install percona-toolkit.x86_64

2)手动rpm包安装
percona-toolkit-3.0.8-1.el7.x86_64.rpm
percona-toolkit-3.0.8-r283ac9a-el7-x86_64-bundle.tar

3)二进制的方式
percona-toolkit-3.0.8_x86_64.tar.gz

4)源码编译
percona-toolkit-3.0.8.tar.gz

5)git方式,越来越多的项目提供git支持了
https://github.com/percona/percona-toolkit.git

这里使用手动rpm方式

# cd /tmp/toolkit
# tar -xvf ./percona-toolkit-3.0.8-r283ac9a-el7-x86_64-bundle.tar

toolkit工具是使用perl语言编写和执行的,所以需要系统中有perl环境。

依赖包检查命令为:

# rpm -qa perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL

# yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL;

# rpm -ivh ./percona-toolkit-3.0.8-1.el7.x86_64.rpm
warning: ./percona-toolkit-3.0.8-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
error: Failed dependencies:
	perl(Term::ReadKey) is needed by percona-toolkit-3.0.8-1.el7.x86_64

# yum list |grep -i readkey
perl-TermReadKey.x86_64                     2.30-20.el7                base 
	
# yum install perl-TermReadKey.x86_64


# rpm -ivh ./percona-toolkit-3.0.8-1.el7.x86_64.rpm
warning: ./percona-toolkit-3.0.8-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:percona-toolkit-3.0.8-1.el7      ################################# [100%]

检查下

# rpm -ql percona-toolkit-3.0.8
/usr/bin/pt-align
/usr/bin/pt-archiver
/usr/bin/pt-config-diff
/usr/bin/pt-deadlock-logger
/usr/bin/pt-diskstats
/usr/bin/pt-duplicate-key-checker
/usr/bin/pt-fifo-split
/usr/bin/pt-find
/usr/bin/pt-fingerprint
/usr/bin/pt-fk-error-logger
/usr/bin/pt-heartbeat
/usr/bin/pt-index-usage
/usr/bin/pt-ioprofile
/usr/bin/pt-kill
/usr/bin/pt-mext
/usr/bin/pt-mongodb-query-digest
/usr/bin/pt-mongodb-summary
/usr/bin/pt-mysql-summary
/usr/bin/pt-online-schema-change
/usr/bin/pt-pmp
/usr/bin/pt-query-digest
/usr/bin/pt-show-grants
/usr/bin/pt-sift
/usr/bin/pt-slave-delay
/usr/bin/pt-slave-find
/usr/bin/pt-slave-restart
/usr/bin/pt-stalk
/usr/bin/pt-summary
/usr/bin/pt-table-checksum
/usr/bin/pt-table-sync
/usr/bin/pt-table-usage
/usr/bin/pt-upgrade
/usr/bin/pt-variable-advisor
/usr/bin/pt-visual-explain
/usr/share/doc/percona-toolkit-3.0.8
/usr/share/doc/percona-toolkit-3.0.8/COPYING
/usr/share/doc/percona-toolkit-3.0.8/Changelog
/usr/share/doc/percona-toolkit-3.0.8/INSTALL
/usr/share/doc/percona-toolkit-3.0.8/README.md
/usr/share/man/man1/percona-toolkit.1p.gz
/usr/share/man/man1/pt-align.1p.gz
/usr/share/man/man1/pt-archiver.1p.gz
/usr/share/man/man1/pt-config-diff.1p.gz
/usr/share/man/man1/pt-deadlock-logger.1p.gz
/usr/share/man/man1/pt-diskstats.1p.gz
/usr/share/man/man1/pt-duplicate-key-checker.1p.gz
/usr/share/man/man1/pt-fifo-split.1p.gz
/usr/share/man/man1/pt-find.1p.gz
/usr/share/man/man1/pt-fingerprint.1p.gz
/usr/share/man/man1/pt-fk-error-logger.1p.gz
/usr/share/man/man1/pt-heartbeat.1p.gz
/usr/share/man/man1/pt-index-usage.1p.gz
/usr/share/man/man1/pt-ioprofile.1p.gz
/usr/share/man/man1/pt-kill.1p.gz
/usr/share/man/man1/pt-mext.1p.gz
/usr/share/man/man1/pt-mysql-summary.1p.gz
/usr/share/man/man1/pt-online-schema-change.1p.gz
/usr/share/man/man1/pt-pmp.1p.gz
/usr/share/man/man1/pt-query-digest.1p.gz
/usr/share/man/man1/pt-show-grants.1p.gz
/usr/share/man/man1/pt-sift.1p.gz
/usr/share/man/man1/pt-slave-delay.1p.gz
/usr/share/man/man1/pt-slave-find.1p.gz
/usr/share/man/man1/pt-slave-restart.1p.gz
/usr/share/man/man1/pt-stalk.1p.gz
/usr/share/man/man1/pt-summary.1p.gz
/usr/share/man/man1/pt-table-checksum.1p.gz
/usr/share/man/man1/pt-table-sync.1p.gz
/usr/share/man/man1/pt-table-usage.1p.gz
/usr/share/man/man1/pt-upgrade.1p.gz
/usr/share/man/man1/pt-variable-advisor.1p.gz
/usr/share/man/man1/pt-visual-explain.1p.gz

可以看到工具位于/usr/bin目录下,以pt-开头
下面逐个介绍下,其实最重要的还是看官方文档
https://www.percona.com/doc/percona-toolkit/3.0/index.html

所有的命令都可以通过 pt-xxx --help 来查看具体的使用

/usr/bin/pt-align

格式化输出

/usr/bin/pt-archiver

将mysql数据库中表的记录归档到另外一个表或者文件
该工具具只是归档旧的数据,对线上数据的OLTP查询影响比较小。

/usr/bin/pt-deadlock-logger

检测mysql的死锁
# pt-deadlock-logger h=localhost

/usr/bin/pt-diskstats

GNU/linux 的磁盘监控,比iostat更详细
# pt-diskstats 
  #ts device    rd_s rd_avkb rd_mb_s rd_mrg rd_cnc   rd_rt    wr_s wr_avkb wr_mb_s wr_mrg wr_cnc   wr_rt busy in_prg    io_s  qtime stime
  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0
  1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0
  1.0 dm-0       0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

/usr/bin/pt-duplicate-key-checker

在表中查找重复的索引和外键
# pt-duplicate-key-checker -hlocalhost
# ########################################################################
# Summary of indexes                                                      
# ########################################################################

# Total Indexes  55

/usr/bin/pt-fifo-split

模拟切割文件并通过管道传递给先入先出队列而不用真正的切割文件

/usr/bin/pt-find

类似 GNU/linux 的find 命令,查找mysql的表并执行命令
# pt-find --engine InnoDB
`mysql`.`engine_cost`
`mysql`.`gtid_executed`
`mysql`.`help_category`
`mysql`.`help_keyword`
`mysql`.`help_relation`
`mysql`.`help_topic`
`mysql`.`innodb_index_stats`
`mysql`.`innodb_table_stats`
`mysql`.`plugin`
`mysql`.`server_cost`
`mysql`.`servers`
`mysql`.`slave_master_info`
`mysql`.`slave_relay_log_info`
`mysql`.`slave_worker_info`
`mysql`.`time_zone`
`mysql`.`time_zone_leap_second`
`mysql`.`time_zone_name`
`mysql`.`time_zone_transition`
`mysql`.`time_zone_transition_type`
`sys`.`sys_config`

/usr/bin/pt-fingerprint

生成查询标识。主要将将sql查询生成queryID,pt-query-digest中的ID即是通过此工具来完成的。
类似于Oracle中的SQL_ID,涉及绑定变量,字面量等
# pt-fingerprint --query "select user, host from users where user = 'root'"
select user, host from users where user = ?

/usr/bin/pt-fk-error-logger

将外键相关的错误信息记录到日志或表
# pt-fk-error-logger h=localhost --iterations 1 --dest h=localhost,D=peiybdb1,t=fke

/usr/bin/pt-heartbeat

监控mysql复制架构的延迟
Monitor replication lag on slave:
# pt-heartbeat -D test --monitor -h slave-server
Check slave lag once and exit (using optional DSN to specify slave host):
# pt-heartbeat -D test --check h=slave-server

/usr/bin/pt-index-usage

# pt-index-usage /var/log/mysql-slow.log --host localhost

/usr/bin/pt-ioprofile

对某个pid附加一个strace进程进行IO分析
# pt-ioprofile
Wed Mar 28 11:44:25 CST 2018
Tracing process ID 26153
     total filename

/usr/bin/pt-kill

Kill掉符合指定条件mysql语句
# pt-kill --busy-time 60 --kill
# pt-kill --busy-time 60 --print

/usr/bin/pt-mext

Get output from mysqladmin:
# pt-mext -r -- mysqladmin ext -i10 -c3
Get output from a file:
# pt-mext -r -- cat mysqladmin-output.txt

/usr/bin/pt-mongodb-query-digest
/usr/bin/pt-mongodb-summary

/usr/bin/pt-online-schema-change
/usr/bin/pt-pmp
/usr/bin/pt-query-digest
/usr/bin/pt-show-grants
/usr/bin/pt-sift

/usr/bin/pt-slave-delay
/usr/bin/pt-slave-find
/usr/bin/pt-slave-restart

/usr/bin/pt-stalk

/usr/bin/pt-table-checksum
/usr/bin/pt-table-sync
/usr/bin/pt-table-usage

/usr/bin/pt-upgrade
/usr/bin/pt-visual-explain

##常用
/usr/bin/pt-summary

# pt-summary

/usr/bin/pt-mysql-summary

在尾部也提供当前实例的的配置文件的信息
# pt-mysql-summary --user=root

/usr/bin/pt-config-diff

比较服务器的参数文件和服务参数
至少要有2个配置源需要指定,可以用于迁移或升级前后配置文件进行对比
# pt-config-diff /etc/my.cnf h=localhost
1 config difference
Variable                  /etc/my.cnf node1
========================= =========== =======
binlog_cache_size         16777216    4194304

/usr/bin/pt-variable-advisor
分析MySQL的变量(my.cnf),并对可能存在的问题提出建议
# pt-variable-advisor --user=root

# WARN delay_key_write: MyISAM index blocks are never flushed until necessary.

# WARN key_buffer_size: The key buffer size is set to its default value, which is not good  for most production systems.

# NOTE max_binlog_size: The max_binlog_size is smaller than the default of 1GB.

# WARN max_connections: If the server ever really has more than a thousand threads running, then the system is likely to spend more time scheduling threads than really doing useful work.

# NOTE sort_buffer_size-1: The sort_buffer_size variable should generally be left at its default unless an expert determines it is necessary to change it.

# NOTE innodb_data_file_path: Auto-extending InnoDB files can consume a lot of disk space that is very difficult to reclaim later.

# NOTE innodb_flush_method: Most production database servers that use InnoDB should set innodb_flush_method to O_DIRECT to avoid double-buffering, unless the I/O system is very low performance.

# WARN myisam_recover_options: myisam_recover_options should be set to some value such as BACKUP,FORCE to ensure that table corruption is noticed.
 类似资料: