mysql toolkit 用法[备忘] (转)

命令列表

  1. /usr/bin/pt-agent  
  2. /usr/bin/pt-align  
  3. /usr/bin/pt-archiver  
  4. /usr/bin/pt-config-diff  
  5. /usr/bin/pt-deadlock-logger  
  6. /usr/bin/pt-diskstats  
  7. /usr/bin/pt-duplicate-key-checker  
  8. /usr/bin/pt-fifo-split  
  9. /usr/bin/pt-find  
  10. /usr/bin/pt-fingerprint  
  11. /usr/bin/pt-fk-error-logger  
  12. /usr/bin/pt-heartbeat  
  13. /usr/bin/pt-index-usage  
  14. /usr/bin/pt-ioprofile  
  15. /usr/bin/pt-kill  
  16. /usr/bin/pt-mext  
  17. /usr/bin/pt-mysql-summary  
  18. /usr/bin/pt-online-schema-change  
  19. /usr/bin/pt-pmp  
  20. /usr/bin/pt-query-digest  
  21. /usr/bin/pt-show-grants  
  22. /usr/bin/pt-sift  
  23. /usr/bin/pt-slave-delay  
  24. /usr/bin/pt-slave-find  
  25. /usr/bin/pt-slave-restart  
  26. /usr/bin/pt-stalk  
  27. /usr/bin/pt-summary  
  28. /usr/bin/pt-table-checksum  
  29. /usr/bin/pt-table-sync  
  30. /usr/bin/pt-table-usage  
  31. /usr/bin/pt-upgrade  
  32. /usr/bin/pt-variable-advisor  
  33. /usr/bin/pt-visual-explain  



语法及作用 (颜色红色表示需关注,蓝色表示作用不大)

/usr/bin/pt-align
用于对文件进行格式化输出

如文件 t 内容如下

  1. [root@db2 aaa]# cat t  
  2. a abc   aaa  123d2 5 d2wf wwwxcc  
  3. a233 ddcqq 55  23ds2  55aaa 5tty655 ccqqq  

格式化后输出如下

  1. [root@db2 aaa]# /usr/bin/pt-align t  
  2. a    abc   aaa 123d2 5     d2wf    wwwxcc  
  3. a233 ddcqq  55 23ds2 55aaa 5tty655 ccqqq  



/usr/bin/pt-archiver
把一个库中的表复制到另外一个服务器中

下面例子把 192.168.200.163 中 dbd.t1 复制至 192.168.200.171 中

  1. [root@db2 ~]# /usr/bin/pt-archiver --source h=192.168.200.163,D=dbd,t=t1,u=terry,p=123 --dest h=192.168.200.171,D=dbd,t=t1,u=terry,p=123 --where "1=1"  

注,必须在 source 中为表创建索引,否则报下面错误信息
Cannot find an ascendable index in table at /usr/bin/pt-archiver line 3175.

/usr/bin/pt-config-diff
用于显示两台数据库中配置文件不一样的地方

  1. [root@db2 ~]# /usr/bin/pt-config-diff h=192.168.200.163 h=192.168.200.171 --user=root --password=123  
  2. 5 config differences  
  3. Variable                  db2.mytest.com          db4  
  4. ========================= ======================= ============  
  5. general_log_file          db2.log                 db4.log  
  6. hostname                  db2.mytest.com          db4  
  7. log_error                 /mdb/db2.mytest.com.err /mdb/db4.err  
  8. slow_query_log_file       db2-slow.log            db4-slow.log  
  9. wsrep_node_name           db2.mytest.com          db4  


 

/usr/bin/pt-deadlock-logger
能够长期记录死锁信息到另外一个表中,  创建表格式如下

  1. CREATE TABLE monitor.deadlocks (  
  2.   server char(20) NOT NULL,  
  3.   ts datetime NOT NULL,  
  4.   thread int unsigned NOT NULL,  
  5.   txn_id bigint unsigned NOT NULL,  
  6.   txn_time smallint unsigned NOT NULL,  
  7.   user char(16) NOT NULL,  
  8.   hostname char(20) NOT NULL,  
  9.   ip char(15) NOT NULL, -- alternatively, ip int unsigned NOT NULL  
  10.   db char(64) NOT NULL,  
  11.   tbl char(64) NOT NULL,  
  12.   idx char(64) NOT NULL,  
  13.   lock_type char(16) NOT NULL,  
  14.   lock_mode char(1) NOT NULL,  
  15.   wait_hold char(1) NOT NULL,  
  16.   victim tinyint unsigned NOT NULL,  
  17.   query text NOT NULL,  
  18.   PRIMARY KEY  (server,ts,thread)  
  19. ENGINE=InnoDB  

语法

  1. pt-deadlock-logger h=10.1.1.29 --dest h=10.1.1.29,D=monitor,t=deadlocks --user=terry --ask-pass  


注: 当前需安装 perl-Term-ReadKey 应用才能够支持密码输入
用户  terry 除授权对  daedlocks 表具备写操作还需要具备 process 权限

参考发生死锁时候的信息:

  1. mysql> select * from monitor.deadlocks G  
  2. *************************** 1. row ***************************  
  3.    server: 10.1.1.29  
  4.        ts: 2013-12-16 03:41:05  
  5.    thread: 24473  
  6.    txn_id: 0  
  7.  txn_time: 5  
  8.      user: mini189  
  9.  hostname:  
  10.        ip: 10.1.1.25  
  11.        db: 189mini  
  12.       tbl: index_species_situaction  
  13.       idx: GEN_CLUST_INDEX  
  14. lock_type: RECORD  
  15. lock_mode: X  
  16. wait_hold: w  
  17.    victim: 0  
  18.     query: update index_species_situaction s set s.new_version= '13121603410024300001' where s.new_version != 'DELETE' and s.species_id ='W6021601'  
  19. *************************** 2. row ***************************  
  20.    server: 10.1.1.29  
  21.        ts: 2013-12-16 03:41:05  
  22.    thread: 25270  
  23.    txn_id: 0  
  24.  txn_time: 5  
  25.      user: mini189  
  26.  hostname:  
  27.        ip: 10.1.1.25  
  28.        db: 189mini  
  29.       tbl: index_species_situaction  
  30.       idx: GEN_CLUST_INDEX  
  31. lock_type: RECORD  
  32. lock_mode: X  
  33. wait_hold: w  
  34.    victim: 1  
  35.     query: update INDEX_SPECIES_SITUACTION h set h.new_version='DELETE' where h.specification_id = NAME_CONST('_specification_id',_utf8'S5F60811' COLLATE 'utf8_general_ci') and h.species_id= NAME_CONST('_species_id',_utf8'W6045FF3' COLLATE 'utf8_general_ci')  
  36. 2 rows in set (0.00 sec)  


/usr/bin/pt-diskstats
调用 /proc/diskstats 文件用于监控磁盘io
直接执行命令则返回下面信息

  1. <span style="font-size:10px;"</span>  
  1. <span style="font-size:10px;">/usr/bin/pt-diskstats  
  2. </span><span style="font-size:10px;">  #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  
  3.   1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     7.0     8.0     0.1    50%    0.1     8.6   4%      0     7.0    5.7   2.9  
  4.   1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     7.0     8.0     0.1    50%    0.1     8.6   4%      0     7.0    5.7   2.9  
  5.   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  
  6.   1.0 dm-2       0.0     0.0     0.0     0%    0.0     0.0     4.0     4.0     0.0     0%    0.1    20.0   3%      0     4.0   11.2   8.8  
  7.   1.0 dm-3       0.0     0.0     0.0     0%    0.0     0.0    10.0     4.0     0.0     0%    0.2    19.0   4%      0    10.0   14.9   4.1  
  8.   1.0 dm-4       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  
  9.   1.0 dm-6       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  
  10.   1.0 drbd0      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  
  11.   
  12.   1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     4.0     6.0     0.0    33%    0.0     5.3   3%      0     4.0    0.0   5.3  
  13.   1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     4.0     6.0     0.0    33%    0.0     5.3   3%      0     4.0    0.0   5.3  
  14.   1.0 dm-0       0.0     0.0     0.0     0%    0.0     0.0     3.0     4.0     0.0     0%    0.0     7.3   1%      0     3.0    2.7   4.7  
  15.   1.0 dm-2       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  
  16.   1.0 dm-3       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  
  17.   1.0 dm-4       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  
  18.   1.0 dm-6       0.0     0.0     0.0     0%    0.0     0.0     3.0     4.0     0.0     0%    0.0    10.0   2%      0     3.0    4.0   6.0  
  19.   1.0 drbd0      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</span>  


 


/usr/bin/pt-duplicate-key-checker
能够快速校验某个数据库或表具有多少个索引

  1. [root@mini189a ~]# pt-duplicate-key-checker h=10.1.1.29 --database=189mini --user=terry --password=terry123  
  2. # ########################################################################  
  3. # Summary of indexes  
  4. # ########################################################################  
  5.   
  6. # Total Indexes  262  


 

/usr/bin/pt-find
类似 linux 下 find 功能,能够根据需要对数据库中表进行过滤,搜索

查询表大小

  1. [root@mini189a ~]#  pt-find  --socket=/var/run/mysqld/mysql5.socket --user=terry --password=terry123 --tablesize +1M  
  2. `189mini`.`pre_ware_broswers`  
  3. `189mini`.`system_regions`  
  4. `crazy`.`sys_log_bs`  


 

最近 3 天内创建的表

  1. [root@mini189a ~]#  pt-find  --socket=/var/run/mysqld/mysql5.socket --user=terry --password=terry123 --ctime -3  
  2. `monitor`.`deadlocks`  
  3. `xszl`.`appreleasetable`  
  4. `xszl`.`bulletintable`  
  5. `xszl`.`companytable`  


 

最近 35 分钟内修改过的表

  1. [root@mini189a ~]#  pt-find  --socket=/var/run/mysqld/mysql5.socket --user=terry --password=terry123 --mmin -35  
  2. `mysql`.`db`  
  3. `mysql`.`user`  


 

数据库中空的表

  1. [root@mini189a ~]# pt-find  --socket=/var/run/mysqld/mysql5.socket --user=terry --password=terry123  --empty  
  2. `189mini`.`ware_tag_assign`  
  3. `189mini`.`ware_tag_content`  
  4. `monitor`.`deadlocks`  
  5. `mysql`.`columns_priv`  
  6. `mysql`.`event`  


 


/usr/bin/pt-fk-error-logger
用于检测外键报错信息, 需创建下面相关表进行数据存储

  1. CREATE TABLE foreign_key_errors (  
  2.  ts datetime NOT NULL,  
  3.  error text NOT NULL,  
  4.  PRIMARY KEY (ts)  
  5. )  


语法测试

  1. pt-fk-error-logger h=10.1.1.29 --dest h=10.1.1.29,D=monitor,t=foreign_key_errors --user=terry --ask-pass  


注: 用户  terry 除授权对  foreign_key_errors 表具备写操作还需要具备 process 权限

参考下面返回信息

  1. MariaDB [terry]> select * from monitor.foreign_key_errors G  
  2. *************************** 1. row ***************************  
  3.    ts: 2013-12-20 10:32:42  
  4. error: Transaction:  
  5. TRANSACTION 2F1D0, ACTIVE 0 sec inserting  
  6. mysql tables in use 1, locked 1  
  7. 3 lock struct(s), heap size 376, 1 row lock(s), undo log entries 1  
  8. MySQL thread id 1329, OS thread handle 0x7fe89805a700, query id 7612 localhost root update  
  9. insert into t4 values (5, 'aaaaa')  
  10. Foreign key constraint fails for table `terry`.`t4`:  
  11. ,  
  12.   CONSTRAINT `t4_ibfk_1` FOREIGN KEY (`id`) REFERENCES `t3` (`id`)  
  13. Trying to add in child table, in index `id` tuple:  
  14. DATA TUPLE: 2 fields;  
  15.  0: len 4; hex 80000005; asc     ;;  
  16.  1: len 6; hex 0000000007e9; asc       ;;  
  17.   
  18. But in parent table `terry`.`t3`, in index `PRIMARY`,  
  19. the closest match we can find is record:  
  20. PHYSICAL RECORD: n_fields 4; compact format; info bits 0  
  21.  0: len 4; hex 80000004; asc     ;;  
  22.  1: len 6; hex 00000002f1cd; asc       ;;  
  23.  2: len 7; hex f4000001a90134; asc       4;;  
  24.  3: len 1; hex 64; asc d;;  
  25.   
  26. 1 row in set (0.00 sec)  


 


/usr/bin/pt-heartbeat
用于监控 ab 复制延时信息,需创建下面表用于信息存储

  1. CREATE TABLE heartbeat (  
  2.   ts                    varchar(26) NOT NULL,  
  3.   server_id             int unsigned NOT NULL PRIMARY KEY,  
  4.   file                  varchar(255) DEFAULT NULL,    -- SHOW MASTER STATUS  
  5.   position              bigint unsigned DEFAULT NULL, -- SHOW MASTER STATUS  
  6.   relay_master_log_file varchar(255) DEFAULT NULL,    -- SHOW SLAVE STATUS  
  7.   exec_master_log_pos   bigint unsigned DEFAULT NULL  -- SHOW SLAVE STATUS  
  8. );  


 

/usr/bin/pt-index-usage
根据日志分析查询过程中是否使用到索引

/usr/bin/pt-ioprofile
分析并打印最近活跃的 IO 与相关进程信息 (只针对 mysql 进程)

  1. [root@mini189a ~]# pt-ioprofile  
  2. 2013年 12月 20日 星期五 11:27:52 CST  
  3. Tracing process ID 44802  
  4.      total       read     pwrite      write      fsync       open      close   getdents      lseek  ftruncate filename  
  5.   0.190553   0.000000   0.000903   0.000000   0.189650   0.000000   0.000000   0.000000   0.000000   0.000000 /data/ibdata1  
  6.   0.107560   0.000000   0.000297   0.000000   0.107263   0.000000   0.000000   0.000000   0.000000   0.000000 /data/ib_logfile0  
  7.   0.027070   0.000000   0.000000   0.027070   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 /var/log/mysqld/mysql5-access.log  
  8.   0.015691   0.000000   0.000000   0.000000   0.000000   0.000689   0.000480   0.014522   0.000000   0.000000 /data/189mini/  
  9.   0.000362   0.000058   0.000000   0.000159   0.000000   0.000000   0.000000   0.000000   0.000081   0.000064 /tmp/ib4I3wV6  
  10.   0.000192   0.000000   0.000000   0.000192   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 /data/mysql5.000097  
  11.   0.000030   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000030   0.000000 /tmp/ibiybk6Q  



/usr/bin/pt-kill
用于 kill 某些指定的 SQL 查询


/usr/bin/pt-mext
用于记录固定时间间隔内 status 返回值的变化 (下面例子将会计算 10 秒内的变化)

  1. pt-mext -r -- mysqladmin -p'password' --socket=/var/run/mysqld/mysql5.socket  ext -i10 -c2  | awk '{ if ($3 !~0) print $0}'  
  2. Com_select                             19446417         198  
  3. Com_show_status                              11           1  
  4. Created_tmp_tables                      1015031           1  
  5. Handler_commit                         20829923         198  
  6. Handler_read_first                     21935275         198  
  7. Handler_read_key                       83253660         198  
  8. Handler_read_rnd_next               25490412028      257632  
  9. Handler_write                          63925147         294  
  10. Innodb_buffer_pool_read_requests    25739644905      259716  
  11. Innodb_rows_read                    25421266296      257116  
  12. Qcache_not_cached                      18701233         198  
  13. Queries                                28860341         199  
  14. Questions                              22720514         199  
  15. Select_scan                            19915354         199  
  16. Table_locks_immediate                  21238032         19  


 

/usr/bin/pt-mysql-summary
给当前数据库进行一些数据统计,不一一对统计进行讨论
注: 执行该命令时,需要调用 mysqldump, 需具备 mysqldump 命令执行路径

  1. pt-mysql-summary --socket=/var/run/mysqld/mysql5.socket  --password='password'  
  2.   
  3.   Database            Tables Views SPs Trigs Funcs   FKs Partn  
  4.   189mini                266    10  
  5.   crazy                   14  
  6.   login                    1  
  7.   monitor                  1  
  8.   mysql                   24  
  9.   #mysql50#lost+found  
  10.   openne                  18  
  11.   test  
  12.   xszl                    29                          60  
  13.   
  14.   Database            InnoDB MyISAM CSV  
  15.   189mini                266     10  
  16.   crazy                   14  
  17.   login                    1  
  18.   monitor                  1  
  19.   mysql                          22   2  
  20.   #mysql50#lost+found  
  21.   openne                  18  
  22.   test  
  23.   xszl                    29  
  24.   
  25.   Database            BTREE  
  26.   189mini               280  
  27.   crazy                  14  
  28.   login                   1  
  29.   monitor                 1  
  30.   mysql                  31  
  31.   #mysql50#lost+found  
  32.   openne                 25  
  33.   test  
  34.   xszl                  149  


 

/usr/bin/pt-online-schema-change
允许在不锁定表条件下修改表


/usr/bin/pt-query-digest
从日志中进行 SQL 分析


/usr/bin/pt-show-grants
必须要以管理员身份登录,能够把数据库之前授权信息进行打印

  1. pt-show-grants -p'mini189!QAZ'  --socket=/var/run/mysqld/mysql5.socket  
  2. -- Grants dumped by pt-show-grants  
  3. -- Dumped from server Localhost via UNIX socket, MySQL 5.5.34-debug-log at 2013-12-20 14:43:22  
  4. -- Grants for 'crazy'@'10.1.1.%'  
  5. GRANT USAGE ON *.* TO 'crazy'@'10.1.1.%' IDENTIFIED BY PASSWORD '*4C1B9FACE717B2947CB8D52B32C3CFE8DA8DD8CB';  
  6. GRANT ALL PRIVILEGES ON `crazy`.* TO 'crazy'@'10.1.1.%';  
  7. -- Grants for 'mini145'@'%'  
  8. GRANT ALL PRIVILEGES ON *.* TO 'mini145'@'%' IDENTIFIED BY PASSWORD '*AA2442B2AE9D3C29F895E57F366092819A3F6738';  
  9. -- Grants for 'mini145'@'10.1.1.%'  
  10. GRANT ALL PRIVILEGES ON *.* TO 'mini145'@'10.1.1.%' IDENTIFIED BY PASSWORD '*AA2442B2AE9D3C29F895E57F366092819A3F6738';  
  11. -- Grants for 'minibackup'@'10.1.1.%'  
  12. GRANT REPLICATION SLAVE, SUPER ON *.* TO 'minibackup'@'10.1.1.%' IDENTIFIED BY PASSWORD '*D5A75D5F1208A6763F64193744F94E57373C128E';  



/usr/bin/pt-slave-delay
令从服务器延时复制

/usr/bin/pt-slave-find
校验 mysql 从服务器信息

/usr/bin/pt-slave-restart
监控并重启从服务器

/usr/bin/pt-summary
统计计算机信息,如内存,CPU,网卡,分区,文件节点,IP地址,网线连接状态,端口连接情况,网络连接状态

  1. /usr/bin/pt-summary  
  2. # Percona Toolkit System Summary Report ######################  
  3.         Date | 2013-12-20 07:03:47 UTC (local TZ: CST +0800)  
  4.     Hostname | mini189a  
  5.       Uptime | 42 days, 15:11,  2 users,  load average: 0.49, 0.47, 0.39  
  6.       System | HP; ProLiant DL380p Gen8; vNot Specified (Rack Mount Chassis)  
  7.  Service Tag | 6CU304WPZN  
  8.     Platform | Linux  
  9.      Release | Red Hat Enterprise Linux Server release 6.3 (Santiago)  
  10.       Kernel | 2.6.32-358.23.2.el6.centos.plus.x86_64  
  11. Architecture | CPU = 64-bit, OS = 64-bit  
  12.    Threading | NPTL 2.12  
  13.     Compiler | GNU CC version 4.4.7 20120313 (Red Hat 4.4.7-4).  
  14.      SELinux | Disabled  
  15.  Virtualized | No virtualization detected  
  16. # Processor ##################################################  
  17.   Processors | physical = 2, cores = 12, virtual = 24, hyperthreading = yes  
  18.       Speeds | 24x1994.866  
  19.       Models | 24xIntel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz  
  20.       Caches | 24x15360 KB  


 

 /usr/bin/pt-table-checksum
MySQL 主从复制校验


/usr/bin/pt-table-sync
进行两个主机中的 MySQL 数据库,或者表数据同步
注, 目标服务器中必须存在与源服务器一样的数据库及表,否则,不存在的不表无法进行同步
pt-table-sync --execute h=192.168.200.163 --database terry  h=192.168.200.171 --user=root --password=123


/usr/bin/pt-variable-advisor
给与对数据库参数定义的建议

  1. pt-variable-advisor h=localhost -p'password' --socket=/var/run/mysqld/mysql5.socket  
  2. # WARN delay_key_write: MyISAM index blocks are never flushed until necessary.  
  3.   
  4. # WARN innodb_log_file_size: The InnoDB log file size is set to its default value, which is not usable on production systems.  
  5.   
  6. # NOTE log_warnings-2: Log_warnings must be set greater than 1 to log unusual events such as aborted connections.  
  7.   
  8. # NOTE max_connect_errors: max_connect_errors should probably be set as large as your platform allows.  
  9.   
  10. # WARN slave_net_timeout: This variable is set too high.  


 /usr/bin/pt-visual-explain
对 sql 进行 explain 分析并以树目录结构显示

原文地址:https://www.cnblogs.com/moss_tan_jun/p/5768064.html