Percona Toolkit 学习(heartbeat, index-usage,ioprofile,killmextmysql-summary)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Percona Toolkit 学习(heartbeat, index-usage,ioprofile,killmextmysql-summary)相关的知识,希望对你有一定的参考价值。

seconds_behind_master含义及不足

seconds_behind_master的值是通过将salve服务器当前的时间戳与二进制日志中的事件的时间戳相比得到的,所以只有执行事件时才会报告延迟。

1.1 如果备库复制线程没有运行,就会报延迟为null。
1.2 一些错误比如网络不稳定可能导致复制中断或停止复制线程,但是seconds_behind_master将显示为0,而不是显示错误
1.3 即使备库线程正在运行,备库有时候可能无法计算延时,如果发生这种情况,备库会报0或者null。
1.4 一个大事务可能会导致延迟波动,例如一个事务更新数据长达1个小时,最后提交。这条更新语句将比它实际发生时间要晚一个小时才记录到二进制日志中,当备库执行这 条语句时,会临时报告备库延迟1小时,然后又很快变为0。


pt-heartbeat原理
改进的做法就是使用percona toolkit工具包的pt-heartbeat,工作原理如下:
2.1 在master上创建一张heartbeat表,按照一定的时间频率更新该表的字段,主要就是向该表写入当前的时间戳
2.2 连接到slave上检查该表的时间记录,和运行pt-heartbeat的当前系统时间进行比较,得出时间的差异,这个时间差异就是复制延迟
注1:这里的2中的系统时间并不一定是slave的系统时间,如果pt-heartbeat的复制监控脚本运行在master上,那么当前系统时间就是master的当前系统时间,这时就不需要严格同步master服务器和slave服务器的时间一致了。
注2:这里的创建的heartbeat表必须在复制同步下的库,有些教材都写了test库,但有时我们在复制配置中过滤了test库的复制,需要注意这一点


 使用方法:

pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop

在主库上开启守护进程来更新game.heartbeat表:

pt-heartbeat -D game --update -h master-server --daemonize

  监控从的延迟情况:

pt-heartbeat -D game --monitor -h slave-server   #一直执行,不退出
pt-heartbeat -D game --check h=slave-server       #执行一次就退出

常用参数:

注意:需要指定的参数至少有 --stop,--update,--monitor,--check。其中--update,--monitor和--check是互斥的,--daemonize和--check也是互斥。
--ask-pass
隐式输入mysql密码
--charset
字符集设置
--check
检查从的延迟,检查一次就退出,除非指定了--recurse会递归的检查所有的从服务器。
--check-read-only
如果从服务器开启了只读模式,该工具会跳过任何插入。
--create-table
在主上创建心跳监控的表,如果该表不存在。可以自己建立,建议存储引擎改成memory。通过更新该表知道主从延迟的差距。
CREATE TABLE heartbeat (
  ts                    varchar(26) NOT NULL,
  server_id             int unsigned NOT NULL PRIMARY KEY,
  file                  varchar(255) DEFAULT NULL,    -- SHOW MASTER STATUS
  position              bigint unsigned DEFAULT NULL, -- SHOW MASTER STATUS
  relay_master_log_file varchar(255) DEFAULT NULL,    -- SHOW SLAVE STATUS
  exec_master_log_pos   bigint unsigned DEFAULT NULL  -- SHOW SLAVE STATUS
);
heratbeat表一直在更改ts和position,而ts是我们检查复制延迟的关键。
--daemonize
执行时,放入到后台执行
--user
-u,连接数据库的帐号
--database
-D,连接数据库的名称
--host
-h,连接的数据库地址
--password
-p,连接数据库的密码
--port
-P,连接数据库的端口
--socket
-S,连接数据库的套接字文件
--file 【--file=output.txt】
打印--monitor最新的记录到指定的文件,很好的防止满屏幕都是数据的烦恼。
--frames 【--frames=1m,2m,3m】
在--monitor里输出的[]里的记录段,默认是1m,5m,15m。可以指定1个,如:--frames=1s,多个用逗号隔开。可用单位有秒(s)、分钟(m)、小时(h)、天(d)。
--interval
检查、更新的间隔时间。默认是见是1s。最小的单位是0.01s,最大精度为小数点后两位,因此0.015将调整至0.02。
--log
开启daemonized模式的所有日志将会被打印到制定的文件中。
--monitor
持续监控从的延迟情况。通过--interval指定的间隔时间,打印出从的延迟信息,通过--file则可以把这些信息打印到指定的文件。
--master-server-id
指定主的server_id,若没有指定则该工具会连到主上查找其server_id。
--print-master-server-id
在--monitor和--check 模式下,指定该参数则打印出主的server_id。
--recurse
多级复制的检查深度。模式M-S-S...不是最后的一个从都需要开启log_slave_updates,这样才能检查到。
--recursion-method
指定复制检查的方式,默认为processlist,hosts。
--update
更新主上的心跳表。
--replace
使用--replace代替--update模式更新心跳表里的时间字段,这样的好处是不用管表里是否有行。
--stop
停止运行该工具(--daemonize),在/tmp/目录下创建一个“pt-heartbeat-sentinel” 文件。后面想重新开启则需要把该临时文件删除,才能开启(--daemonize)。
--table
指定心跳表名,默认heartbeat。

主:192.168.0.11

从:192.168.0.9

主数据库上面运行:

pt-heartbeat --user=root --ask-pss --host=127.0.0.1 --create-table -D game --interval=1 --update --replace --daemonize

主数据库上执行监控:

pt-heartbeat -D game --table=heartbeat --monitor -h192.168.0.9 -uroot --ask-pass
pt-heartbeat -D game --table=heartbeat --check -h192.168.0.9 -uroot --ask-pass 只监控一次

 pt-index-usage

功能介绍:
从log文件中读取插叙语句,并用explain分析他们是如何利用索引。完成分析之后会生成一份关于索引没有被查询使用过的报告。
l 用法介绍:
pt-index-usage [OPTION...] [FILE...]

可以直接从慢查询中获取sql,FILE文件中的sql格式必须和慢查询中个是一致,如果不是一直需要用pt-query-digest转换一下。也可以不生成报告直接保存到数据库中,具体的见后面的示例

l  使用示例:

从满查询中的sql查看索引使用情况范例:

pt-index-usage  /tmp/localhost-slow.log --host=localhost --user=root --password=123456



将分析结果保存到数据库范例:

pt-index-usage /tmp/localhost-slow.log --host=localhost --user=root --password=123456  --no-report --create-save-results-database

使用--create-save-results-database会自动生成数据库和表来保存结果。


pt-ioprofile

是一个percona的性能分析工具,可以查看进程输出、输入,打印一些表文件及活动IO。pt-ioprofile是一个只读工具,所以对数据没风险。

有人在生产环境中使用ioprofile时出现导致mysql挂起的现象,虽然是5年前了,但还是请谨慎使用。

pt-ioprofile -p 8534 -c count
Sun Apr  3 14:32:48 CST 2016
Tracing process ID 21751
     total      pread       read     pwrite      write      fsync       open      close   getdents      lseek      fcntl  ftruncate filename
    124838          0         24     124796          0          0          4          5          2          6          1          0 /tmp/#sql_54f7_0.MYD
     36315          0      16888          0       5877          0        406        695        694         17      11738          0 /usr/local/mysql/data/game/
      9534          0       2959          0       3616          0          0          0          0       2959          0          0 /usr/local/mysql/data/relay-bin.005950
      7232          0          0          0       3616          0          0          0          0       3616          0          0 /usr/local/mysql/data/master.info
      3003          0       1421          4        576          0          4         33          0          4        961          0 /tmp/#sql_54f7_0.MYI
      2191          0       1051          0        365          0          1         34         20          2        718          0 /usr/local/mysql/data/mysql-bin.000232
      1244          0          0          0        622          0          0          0          0        622          0          0 /usr/local/mysql/data/relay-log.info
       858          0          0          0        858          0          0          0          0          0          0          0 /usr/local/mysql/data/slowlog/20160403_slow.log
       685          0          0        651          0         34          0          0          0          0          0          0 /usr/local/mysql/data/ib_logfile2
       152          4         60          2         32          0          3          4          0         12         35          0 /tmp/#sql54f7_142ba6e6_1.frm
       112          0          0         51          0         61          0          0          0          0          0          0 /usr/local/mysql/data/ibdata1
        88         28         30          0          0          0          0          0          0         30          0          0 /usr/local/mysql/data/game/tb_area.MYD
        53         20         12          0         12          0          4          1          0          4          0          0 /tmp/#sql_54f7_1.MYD
        49          4         10          2         15          0          3          3          0         12          0          0 /tmp/#sql54f7_142ba6e6_0.frm
        30          0          0          0          0         30          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_trlog#P#p1604.ibd
        29          0          0          0          0         29          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_stat.ibd
        27          0          0          0          0         27          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player.ibd
        24          0          0          0          0         24          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_log#P#p1604.ibd
        16          0          0          0          8          8          0          0          0          0          0          0 /usr/local/mysql/data/mysql-bin.000234
        16          0          0          0          0         16          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_log#P#p1604.ibd
        13          0          0          0          0         13          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_log#P#p1604.ibd
        11          0          2          0          5          0          0          0          0          3          0          1 /tmp/ibhAJY2f
         9          0          2          2          0          0          3          1          0          1          0          0 /tmp/#sql_54f7_1.MYI
         8          0          0          0          0          8          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_deposit.ibd
         7          0          0          0          0          0          1          3          0          3          0          0 /usr/local/mysql/data/mysql-bin.000229
         6          2          2          0          0          0          0          0          0          2          0          0 /usr/local/mysql/data/mysql/proc.MYD
         6          0          0          0          0          6          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_captcha_log.ibd
         3          0          2          0          0          0          0          0          0          1          0          0 /usr/local/mysql/data/mysql-bin.index
         3          0          0          0          0          3          0          0          0          0          0          0 /usr/local/mysql/data/ib_logfile0
         3          0          0          0          0          3          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_withdraw_log.ibd
         3          0          0          0          0          3          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_online.ibd
         2          0          0          0          0          2          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_child_list.ibd
         1          1          0          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_log.ibd
         1          0          0          0          0          1          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_online_log.ibd

pt-ioprofile -p 8534 -c sizes
Sun Apr  3 14:23:12 CST 2016
Tracing process ID 21751
     total      pread       read     pwrite      write      fsync       open      close   getdents      lseek      fcntl filename
1130822069785          0     532078          0     532078          0          0          0          0 1130821005629          0 /usr/local/mysql/data/y-bin.005950
  34816000          0          0   34816000          0          0          0          0          0          0          0 /usr/local/mysql/data/ibdata1
  10942314          0    1064960          0    5072362          0          0          0    4804992          0          0 /usr/local/mysql/data/game/
   1772544          0          0    1772544          0          0          0          0          0          0          0 /usr/local/mysql/data/ib_logfile2
   1425408    1425408          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_trlog#P#p1603.ibd
    476444          0          0          0     476444          0          0          0          0          0          0 /usr/local/mysql/data/slowlog/20160403_slow.log
    309097          0          0          0     309097          0          0          0          0          0          0 /usr/local/mysql/data/master.info
     88930         62       5038         72      25630          0          0          0          0      58128          0 /tmp/#sql54f7_142b8717_1.frm
     76980       3416         40          0          0          0          0          0          0      73524          0 /usr/local/mysql/data/mysql/proc.MYD
     67742         62        886         72       8594          0          0          0          0      58128          0 /tmp/#sql54f7_142b8717_0.frm
     49152      49152          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_trlog#P#p1604.ibd
     49152      49152          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_delog.ibd
     37760          0          0          0      37760          0          0          0          0          0          0 /usr/local/mysql/data/relay-log.info
     32768      32768          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_withdraw_log.ibd
     16384      16384          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_child_list.ibd
      4963          0          0          0       4963          0          0          0          0          0          0 /usr/local/mysql/data/mysql-bin.000234
         0          0          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/ib_logfile0
         0          0          0          0          0          0          0          0          0          0          0 /usr/local/mysql/data/game/tb_player_stat.ibd


 pt-kill

是一个优秀的kill MySQL连接的一个工具,是percona toolkit的一部分,在因为空闲连接较多导致超过最大连接数、某个有问题的sql导致mysql负载很高时,都需要将一些连接kill掉,这个工具主要就是这个用途。

参数:
–busy-time运行时间
–idle-time空闲时间
–victims所有匹配的连接,对应有最久的连接
–interval间隔时间,默认30s,有点长,可以根据实际情况来调节
–print打印出来kill掉的连接
–match-command匹配当前连接的命令
QuerySleepBinlog DumpConnectDelayed insertExecuteFetchInit DBKillPrepareProcesslistQuitReset stmtTable Dump
–match-state匹配当前连接的状态Lockedlogincopy to tmp tableCopying to tmp tableCopying to tmp table on diskCreating tmp tableexecutingReading from netSending dataSorting for orderSorting resultTable lockUpdating
–match-info使用正则表达式匹配符合的sql
–match-db –match-user –match-host见名知意

常用用法:

杀掉空闲链接

pt-kill –match-command Sleep –idle-time 5 –host –port –interval –print –kill –victims all
杀掉运行时间超过5s的链接
pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all
杀掉匹配某个规则的正在运行的sql
pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all –match-info
杀掉正在进行filesort的sql
pt-kill –match-command Query –match-state “Sorting result” busy-time 5 –host –port –interval –print –kill –victims all
杀掉正在Copying to tmp table的sql
pt-kill –match-command Query –match-state “Copying to tmp table” busy-time 5 –host –port –interval –print –kill –victims all

pt-mext


并行查看SHOW GLOBAL STATUS的多个样本的信息。
pt-mext会执行你指定的COMMAND,并每次读取一行结果,把空行分割的内容保存到一个一个的临时文件中,最后结合这些临时文件并行查看结果。

pt-mext -r -- mysqladmin ext -i10 -c3
pt-mext -r -- cat mysqladmin-output.txt

pt-mysql-summary


查看mysql各个统计信息

# Percona Toolkit MySQL Summary Report #######################
              System time | 2016-04-03 06:52:08 UTC (local TZ: CST +0800)
# Instances ##################################################
  Port  Data Directory             Nice OOM Socket
  ===== ========================== ==== === ======
   3307 /usr/local/mysql/data_3307 0    0   /tmp/mysql_3307.sock
   3306 /var/lib/mysql/data        0    0   /var/tmp/mysql.sock
# MySQL Executable ###########################################
       Path to executable | /usr/local/mysql/bin/mysqld
              Has symbols | Yes
# Report On Port 3306 ########################################
                     User | [email protected]
                     Time | 2016-04-03 14:52:08 (CST)
                 Hostname | c33-01
                  Version | 5.5.21-log Source distribution
                 Built On | Linux x86_64
                  Started | 2016-02-18 12:36 (up 45+02:15:15)
                Databases | 6
                  Datadir | /var/lib/mysql/data/
                Processes | 8 connected, 3 running
              Replication | Is not a slave, has 1 slaves connected
                  Pidfile | /var/lib/mysql/data/c33-01.pid (exists)
# Processlist ################################################
  Command                        COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  Binlog Dump                           1       1    175000    175000
  Daemon                                1       1        25        25
  Query                                 1       1         0         0
  Sleep                                 6       0       450       125
  User                           COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  event_scheduler                       1       1        25        25
  GaMe                                  6       0         0         0
  repl                                  1       1    175000    175000
  root                                  1       1         0         0
  Host                           COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  127.0.0.1                             6       0         0         0
  192.168.53.9                          1       1    175000    175000
  localhost                             2       2        25        25
  db                             COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  game                                  6       0         0         0
  NULL                                  3       3    175000    175000
  State                          COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
                                        6       0         0         0
  Master has sent all binlog to         1       1    175000    175000
  NULL                                  1       1         0         0
  Waiting for next activation           1       1        25        25
# Status Counters (Wait 10 Seconds) ##########################
Variable                                Per day  Per second     11 secs
Aborted_clients                               4                        
Binlog_cache_use                         225000           2            
Bytes_received                        400000000        4500         400
Bytes_sent                            900000000       10000        3500
Com_admin_commands                       800000           9            
Com_alter_table                               6                        
Com_begin                                125000           1            
Com_change_db                                 1                        
Com_commit                                70000                        
Com_create_table                              3                        
Com_delete                                17500                        
Com_drop_table &

以上是关于Percona Toolkit 学习(heartbeat, index-usage,ioprofile,killmextmysql-summary)的主要内容,如果未能解决你的问题,请参考以下文章

Percona Toolkit 学习(heartbeat, index-usage,ioprofile,killmextmysql-summary)

percona-toolkit 杀死慢的SQL语句

安装percona-toolkit提示的报错

percona-toolkit工具的使用

安装percona toolkit

安装percona toolkit