percona-toolkit


前言

记录一次percona-toolkit的使用


一、下载地址

percona-toolkit.tar.gz 下载地址
找了好久,官网我都找到不到下载的链接,可能是卡的

二、安装

报错就百度,缺少包的问题基本都能解决

#安装依赖的包
yum -y install prel
yum -y install perl-ExtUtils-MakeMaker
yum -y install 'perl(Data::Dumper)'
yum -y install perl-Digest-MD5
yum -y install perl-DBI
yum -y install perl-DBD-MySQL
#解压
tar -zxvf percona-toolkit-3.3.1_i386.tar.gz
#进入目录
cd percona-toolkit-3.3.1
#编译安装
perl Makefile.PL
make && make install

三、主要工具使用介绍

以下属于搬运了,我只用了3.1,毕竟不是专业的dba
原文链接跳转

percona_tools重点使用的是:
pt-online-schema-change
pt-heartbeat
pt-table-checksum
pt-table-sync

3.1 pt-online-schema-change

#添加字段
pt-online-schema-change --alter "ADD COLUMN  c1 int" h=127.0.0.1,P=3306,p=123456,u=root,D=test,t=percona_tools --execute
#删除字段
pt-online-schema-change --alter "drop column c1" h=127.0.0.1,P=3306,p=123456,u=root,D=test,t=percona_tools --execute
#添加索引
pt-online-schema-change --alter "add  key name(name)" h=127.0.0.1,P=3306,p=123456,u=root,D=test,t=percona_tools --execute
#修改字段长度
pt-online-schema-change --alter "modify symbol varchar(10)" h=127.0.0.1,P=3306,p=123456,u=root,D=test,t=IDX_MARKETVALUE --execute
#修改表引擎
pt-online-schema-change --alter "ENGINE=InnoDB" h=127.0.0.1,P=3306,p=123456,u=root,D=test,t=T_MyIsam --execute

工作原理:

1) 创建一个和你要执行 alter 操作的表一样的空表结构
2) 执行表结构修改,然后从原表中的数据到copy到 表结构修改后的表
3) 在原表上创建触发器将 copy 数据的过程中,在原表的更新操作 更新到新表
4) copy 完成以后,用rename table 新表代替原表,默认删除原表。

注:

修改的表必须有主键,另外也建议加上字符集选项;
如果表有外键,除非使用 --alter-foreign-keys-method 指定特定的值,否则工具不予执行;
如果表中已经定义了触发器则这个工具就不能工作了

用法介绍:

pt-online-schema-change [OPTIONS] DSN
options 可以自行查看 help,DNS 为你要操作的数据库和表。这里有两个参数需要介绍一下:
–dry-run 这个参数不建立触发器,不拷贝数据,也不会替换原表。只是创建和更改新表。
–execute 这个参数的作用和前面工作原理的介绍的一样,会建立触发器,来保证最新变更的数据会影响至新表。注意:如果不加这个参数,这个工具会在执行一些检查后退出。

3.2 pt-heartbeat

#首先要在主服务器下创建一个定期更新heartbeat表
pt-heartbeat -D test1 --update -u root -p 123456 -P 3346 -h 106.14.184.46 --create-table --daemonize
#监控复制在slave上的落后程度
pt-heartbeat -D test --monitor -u root -p 123456 -P 3306 -h 106.14.184.46
#停止监控
pt-heartbeat --stop

2.3 pt-table-checksum

pt-table-checksum  --host=106.14.184.46 --port 3306 --databases=test1 -uroot -p123456   --no-check-binlog-format

注:如执行报错:

Diffs cannot be detected because no slaveswere found. Please read the–recursion-method documentation for information

从库配置文件 my.cnf中添加

report_host=slave_ip
report_port=slave_port

选项简介:

TS :完成检查的时间。
ERRORS :检查时候发生错误和警告的数量。
DIFFS :0表示一致,1表示不一致。当指定–no-replicate-check时,会一直为0,当指定–replicate-check-only会显示不同的信息。
ROWS :表的行数。
CHUNKS :被划分到表中的块的数目。
SKIPPED :由于错误或警告或过大,则跳过块的数目。
TIME :执行的时间。
TABLE :被检查的表名。

3.4 pt-table-sync

主库为 106.14.184.46 从库为 106.14.184.47

#测试删除一条记录 然后检查
SELECT  MAX(updateid) FROM test1.COST_RISKFREE
DELETE  FROM  COST_RISKFREE WHERE  updateid=51453
#1) 先校验
PTDEBUG=1 pt-table-checksum --user=root --password=123456 --host=106.14.184.46 --port=3346 --databases=test1 --tables=COST_RISKFREE --recursion-method=processlist
#2) 根据校验结果修复从库数据
PTDEBUG=1  pt-table-sync --execute  --databases=test1  --tables=COST_RISKFREE   --replicate percona.checksums --sync-to-master  h=106.14.184.46,u=root,p=123456

–replicate=:指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
–databases=:指定执行同步的数据库,多个用逗号隔开。
–tables=:指定执行同步的表,多个用逗号隔开。
–sync-to-master:指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。h=127.0.0.1
–host=:服务器地址,命令里有2个ip,第一次出现的是M的地址,第2次是Slave的地址。
–user=:帐号。
–password:密码。
–print:打印,但不执行命令。
–execute:执行命令。

3.5 pt_query_digest

#1)直接分析
pt-query-digest  slowlog20170925 > slow_report.log

#2)分析最近12小时内的查询
pt-query-digest  --since=12h  slow.log > slow_report2.log

#3)分析指定时间范围内的查询
pt-query-digest  /home/mysql_data/DBD_NOD05-slow.log --since '2017-09-24 09:30:00' --until '2017-09-25 14: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")' gjc-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_20180401
pt-query-digest  --user=root –password=abc123--review  h=localhost,D=test,t=query_history--create-review-table  slow.log_20180402

#9)通过tcpdump抓取mysql的tcp协议数据,然后再分析
tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3346 > 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

3.6 pt-diskstats 查看系统磁盘状态

#1) 查看本机所有的磁盘的状态情况:
pt-diskstats

#2) 查看指定磁盘状态
pt-diskstats --devices-regex  vda
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值