当前位置:Gxlcms > 数据库问题 > mysql主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理

mysql主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理

时间:2021-07-01 10:21:17 帮助过:23人阅读

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 mysql> use test; Database changed   mysql> CREATE TABLE query_review (     ->    checksum     BIGINT UNSIGNED NOT NULL PRIMARY KEY,     ->    fingerprint  TEXT NOT NULL,     ->    sample       TEXT NOT NULL,     ->    first_seen   DATETIME,     ->    last_seen    DATETIME,     ->    reviewed_by  VARCHAR(20),     ->    reviewed_on  DATETIME,     ->    comments     TEXT     -> ); Query OK, 0 rows affected (0.02 sec)    mysql> select * from query_review; Empty set (0.00 sec)    [root@master-server ~]# pt-query-digest --user=root --password=123456 --review h=localhost,D=test,t=query_review /data/mysql/data/mysql-slow.log   mysql> select * from query_review; +----------------------+------------------------------------------+-----------------------------------------------------------------+---------- | checksum | fingerprint | sample | first_see +----------------------+------------------------------------------+-----------------------------------------------------------------+---------- | 5816476304744969035 | create database if not exists `huanqiu` | CREATE DATABASE IF NOT EXISTS `huanqiu` /* pt-table-checksum */ | 2017-01-1 | 11295947304747079651 | create database if not exists `huanpc` | CREATE DATABASE IF NOT EXISTS `huanpc` /* pt-table-checksum */ | 2017-01-1 +----------------------+------------------------------------------+-----------------------------------------------------------------+---------- 2 rows in set (0.00 sec)

从tcpdump中分析:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@master-server ~]# tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txt tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes   然后打开另一个终端窗口: [root@master-server ~]# pt-query-digest --type tcpdump mysql.tcp.txt Pipeline process 3 (TcpdumpParser) caused an error: substr outside of string at /usr/bin/pt-query-digest line 3628, <> chunk 93. Will retry pipeline process 2 (TcpdumpParser) 100 more times.   # 320ms user time, 20ms system time, 24.93M rss, 204.84M vsz # Current date: Mon Jan 16 13:24:50 2017 # Hostname: master-server # Files: mysql.tcp.txt # Overall: 31 total, 4 unique, 4.43 QPS, 0.00x concurrency _______________ # Time range: 2017-01-16 13:24:43.000380 to 13:24:50.001205 # Attribute          total     min     max     avg     95%  stddev  median # ============     ======= ======= ======= ======= ======= ======= ======= # Exec time           30ms    79us     5ms   967us     4ms     1ms   159us # Rows affecte          14       0       2    0.45    1.96    0.82       0 # Query size         1.85k      17     200   61.16  192.76   72.25   17.65 .........

13)pt-slave-delay
功能介绍:
设置从服务器落后于主服务器指定时间。
用法介绍:
pt-slave-delay [OPTION...] SLAVE-HOST [MASTER-HOST]
原理:通过启动和停止复制sql线程来设置从落后于主指定时间。默认是基于从上relay日志的二进制日志的位置来判断,因此不需要连接到主服务器,如果IO进程不落后主服务器太多的话,这个检查方式工作很好,如果网络通畅的话,一般IO线程落后主通常都是毫秒级别。一般是通过--delay and --delay"+"--interval来控制。--interval是指定检查是否启动或者停止从上sql线程的频繁度,默认的是1分钟检查一次。
使用示例:
范例1:使从落后主1分钟,并每隔1分钟检测一次,运行10分钟

1 2 3 [root@master-server ~]# pt-slave-delay --user=root --password=123456 --delay 1m --run-time 10m --host=192.168.1.102 2017-01-16T13:32:31 slave running 0 seconds behind 2017-01-16T13:32:31 STOP SLAVE until 2017-01-16T13:33:31 at master position mysql-bin.000005/102554361

范例2:使从落后主1分钟,并每隔15秒钟检测一次,运行10分钟:

1 2 3 [root@master-server ~]# pt-slave-delay --user=root --password=123456 --delay 1m --interval 15s --run-time 10m --host=192.168.1.102 2017-01-16T13:38:22 slave running 0 seconds behind 2017-01-16T13:38:22 STOP SLAVE until 2017-01-16T13:39:22 at master position mysql-bin.000005/102689359

14)pt-slave-restart
功能介绍:
监视mysql复制错误,并尝试重启mysql复制当复制停止的时候
用法介绍:
pt-slave-restart [OPTION...] [DSN]
监视一个或者多个mysql复制错误,当从停止的时候尝试重新启动复制。你可以指定跳过的错误并运行从到指定的日志位置。
使用示例:
范例1:监视192.168.1.101的从,跳过1个错误

1 [root@master-server ~]# pt-slave-restart --user=root --password=123456 --host=192.168.1.101 --skip-count=1

范例2:监视192.168.1.101的从,跳过错误代码为1062的错误。

1 [root@master-server ~]# pt-slave-restart --user=root --password=123456 --host=192.168.1.101 --error-numbers=1062

15)pt-diskstats
功能介绍:
是一个对GUN/LINUX的交互式监控工具
用法介绍:
pt-diskstats [OPTION...] [FILES]
为GUN/LINUX打印磁盘io统计信息,和iostat有点像,但是这个工具是交互式并且比iostat更详细。可以分析从远程机器收集的数据。
使用示例:
范例1:查看本机所有的磁盘的状态情况:

1 [root@master-server ~]# pt-diskstats

范例2:只查看本机sdc1磁盘的状态情况:

1 2 3 4 [root@master-server ~]# pt-diskstats  --devices-regex vdc1   #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   0.9 vdc1       0.0     0.0     0.0     0%    0.0     0.0     5.9     4.0     0.0     0%    0.0     1.0   0%      0     5.9    0.6   0.4   1.0 vdc1       0.0     0.0     0.0     0%    0.0     0.0     2.0     6.0     0.0    33%    0.0     0.7   0%      0     2.0    0.0   0.7

16)pt-summary
功能介绍:
友好地收集和显示系统信息概况,此工具并不是一个调优或者诊断工具,这个工具会产生一个很容易进行比较和发送邮件的报告。
用法介绍:
pt-summary
原理:此工具会运行和多命令去收集系统状态和配置信息,先保存到临时目录的文件中去,然后运行一些unix命令对这些结果做格式化,最好是用root用户或者有权限的用户运行此命令。
使用示例:
查看本地系统信息概况

1 [root@master-server ~]# pt-summary

17)pt-stalk
功能介绍:
出现问题的时候收集mysql的用于诊断的数据
用法介绍:
pt-stalk [OPTIONS] [-- MYSQL OPTIONS]
pt-stalk等待触发条件触发,然后收集数据帮助错误诊断,它被设计成使用root权限运行的守护进程,因此你可以诊断那些你不能直接观察的间歇性问题。默认的诊断触发条件为SHOW GLOBAL STATUS。也可以指定processlist为诊断触发条件 ,使用--function参数指定。
使用示例:
范例1:指定诊断触发条件为status,同时运行语句超过20的时候触发,收集的数据存放在目标目录/tmp/test下:

1 [root@master-server ~]# pt-stalk  --function status --variable Threads_running --threshold 20 --dest /tmp/test  -- -uroot -p123456  -h192.168.1.101

范例2:指定诊断触发条件为processlist,超过20个状态为statistics触发,收集的数据存放在/tmp/test目录下:

1 2 3 4 5 6 7 8 9 [root@master-server ~]# pt-stalk  --function processlist --variable State --match statistics --threshold 20 --dest /tmp/test -- -uroot -p123456  -h192.168.1.101 ....... 2017_01_15_17_31_49-hostname 2017_01_15_17_31_49-innodbstatus1 2017_01_15_17_31_49-innodbstatus2 2017_01_15_17_31_49-interrupts 2017_01_15_17_31_49-log_error 2017_01_15_17_31_49-lsof 2017_01_15_17_31_49-meminfo

18)pt-archiver
功能介绍:
将mysql数据库中表的记录归档到另外一个表或者文件
用法介绍:
pt-archiver [OPTION...] --source DSN --where WHERE
这个工具只是归档旧的数据,不会对线上数据的OLTP查询造成太大影响,你可以将数据插入另外一台服务器的其他表中,也可以写入到一个文件中,方便使用source命令导入数据。另外你还可以用它来执行delete操作。特别注意:这个工具默认的会删除源中的数据!!
使用示例:
范例1:将192.168.1.101上的huanqiu库的haha表id小于10的记录转移到192.168.1.102上的wangshibo库下的wang_test表内,并归档到/var/log/haha_archive_20170115.log文件中(注意:转移前后,两张表对应转移字段要相同,字段属性最好也要相同;)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 源数据库机器192.168.1.101的huanqiu库下的haha表在转移前的信息: mysql> select from huanqiu.haha;                                                                                                             +----+---------------+ | id | name          | +----+---------------+ |  1 | changbo       | |  2 | wangpengde    | |  4 | guocongcong   | |  5 | kevin         | |  8 | mamin         | |  9 | shihonge      | | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.01 sec)   目标数据库机器192.168.1.102的wangshibo库下的wang_test表在转移前的信息: mysql> select from wangshibo.wang_test; +------+-----------+ | id   | name      | +------+-----------+ |   20 | guominmin | |   21 | gaofei    | |   22 | 李梦楠    | +------+-----------+ rows in set (0.00 sec)   接着在192.168.1.101机器上执行转移命令: [root@master-server ~]# pt-archiver --source h=192.168.1.101,D=huanqiu,t=haha --user=root --password=123456 --dest h=192.168.1.102,D=wangshibo,t=wang_test --file ‘/var/log/haha_archive_20170115.log‘ --where "id<=10"  --commit-each   上面命令执行成功后,再次观察转移前后信息 发现源数据库机器192.168.1.101的huanqiu.haha表数据在转移后,源数据也删除了! mysql> select from huanqiu.haha; +----+---------------+ | id | name          | +----+---------------+ | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.00 sec)   查看归档日志: [root@master-server ~]# tail -f /var/log/haha_archive_20170115.log 1   changbo 2   wangpengde 4   guocongcong 5   kevin 8   mamin 9   shihonge   目标数据库192.168.1.102的wangshibo.wang_test表内已经移转到了新数据 mysql> select from wangshibo.wang_test; +------+-------------+ | id   | name        | +------+-------------+ |   20 | guominmin   | |   21 | gaofei      | |   22 | 李梦楠       | |    1 | changbo     | |    2 | wangpengde  | |    4 | guocongcong | |    5 | kevin       | |    8 | mamin       | |    9 | shihonge    | +------+-------------+ rows in set (0.00 sec)

范例2:将192.168.1.101上的huanqiu库的haha表里id小于10的记录归档到haha_log_archive_2017.10.10.log文件中:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 mysql> select from huanqiu.haha; +----+---------------+ | id | name          | +----+---------------+ |  1 | changbo       | |  2 | wangpengde    | |  4 | guocongcong   | |  5 | kevin         | |  8 | mamin         | |  9 | shihonge      | | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.00 sec)   [root@master-server ~]# pt-archiver --source h=192.168.1.101,D=huanqiu,t=haha --user=root --password=123456 --file ‘haha_log_archive_2017.10.10.log‘ --where "id<=10" --commit-each   转移后的源数据已被删除 mysql> select from huanqiu.haha; +----+---------------+ | id | name          | +----+---------------+ | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.00 sec)   查看归档文件 [root@master-server ~]# cat haha_log_archive_2017.10.10.log 1   changbo 2   wangpengde 4   guocongcong 5   kevin 8   mamin 9   shihonge

范例3:删除192.168.1.101上的huanqiu库的haha表中id小于10的记录:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 mysql> select from huanqiu.haha; +----+---------------+ | id | name          | +----+---------------+ |  1 | changbo       | |  2 | wangpengde    | |  4 | guocongcong   | |  5 | kevin         | |  8 | mamin         | |  9 | shihonge      | | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.00 sec)   [root@master-server ~]# pt-archiver --source h=192.168.1.101,D=huanqiu,t=haha --user=root --password=123456 --purge --where ‘id<=10‘ --no-check-charset <br>再次查看,发现数据已成功删除! mysql> select from huanqiu.haha; +----+---------------+ | id | name          | +----+---------------+ | 11 | zhanglei      | | 15 | zhanghongmiao | +----+---------------+ rows in set (0.00 sec)

19)pt-find
功能介绍:
查找mysql表并执行指定的命令,和gnu的find命令类似。
用法介绍:
pt-find [OPTION...] [DATABASE...]
默认动作是打印数据库名和表名
使用示例:
查找192.168.1.101中1天以前创建的InnoDB的表 ,并打印。

1 2 3 4 5 6 7 8 9 10 11 [root@master-server ~]# pt-find --ctime +1  --host=192.168.1.101 --engine InnoDB --user=root --password=123456 `huanpc`.`_heihei_new` `huanpc`.`checksums` `huanqiu`.`_haha_new` `huanqiu`.`checksums` `huanqiu`.`heartbeat` `mysql`.`innodb_index_stats` `mysql`.`innodb_table_stats` `mysql`.`slave_master_info` `mysql`.`slave_relay_log_info` `mysql`.`slave_worker_info`

范例2:查找192.168.1.101中1天以前更改过的数据库名字匹配%huanqiu%的并且引擎为Myisam的表,并将表的引擎更改为Innodb引擎。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 先查找出192.168.1.101上1天以前更改过的数据库名字匹配%huanqiu%的并且引擎为Myisam的表 [root@master-server ~]# pt-find --ctime +2 --dblike huanqiu --host=192.168.1.101 --engine Myisam --user=root --password=123456 `huanqiu`.`_haha_new` `huanqiu`.`checksums` `huanqiu`.`heartbeat` [root@master-server ~]# pt-find --ctime +2 --dblike huanpc --host=192.168.1.101 --engine Myisam --user=root --password=123456 `huanpc`.`_heihei_new` `huanpc`.`checksums`   再将查找出的表的引擎改为Innodb [root@master-server ~]# pt-find --ctime +2 --dblike huanqiu --host=192.168.1.101 --engine Myisam --user=root --password=123456 --exec "ALTER TABLE %D.%N ENGINE=InnoDB" [root@master-server ~]# pt-find --ctime +2 --dblike huanpc --host=192.168.1.101 --engine Myisam --user=root --password=123456 --exec "ALTER TABLE %D.%N ENGINE=InnoDB"   最后再检查对应数据表的引擎

范例3:查找192.168.1.101中huanqiu库和huanpc库中的空表,并删除。

1 [root@master-server ~]# pt-find --empty huanqiu huanpc --host=192.168.1.101 --user=root --password=123456  --exec-plus "DROP TABLE %s"

范例4:查找192.168.1.101中超过100M的表

1 [root@master-server ~]# pt-find --tablesize +100M --host=192.168.1.101 --user=root --password=123456

20)pt-kill
功能介绍:
Kill掉符合指定条件mysql语句
用法介绍:
pt-kill [OPTIONS]
加入没有指定文件的话pt-kill连接到mysql并通过SHOW PROCESSLIST找到指定的语句,反之pt-kill从包含SHOW PROCESSLIST结果的文件中读取mysql语句
使用示例:
范例1:查找192.168.1.101数据库服务器运行时间超过60s的语句,并打印

1 [root@master-server ~]# pt-kill --busy-time 60 --print --host=192.168.1.101 --user=root --password=123456

范例2:查找192.168.1.101数据库服务器运行时间超过60s的语句,并kill

1 [root@master-server ~]# pt-kill --busy-time 60 --kill --host=192.168.3.135 --user=root --password=123456

范例3:从proccesslist文件中查找执行时间超过60s的语句

1 2 3 [root@master-server ~]# mysql -uroot -p123456 -h192.168.1.101 -e "show processlist" > processlist.txt Warning: Using a password on the command line interface can be insecure. [root@master-server ~]# pt-kill --test-matching processlist.txt --busy-time 60 --print

mysql主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理

标签:表示   linux c   ted   接下来   mysql语句   visual   没有   远程   oss   

人气教程排行