Percona Toolkit
Percona Toolkit is a collection of advanced command-line tools used by Percona (http://www.percona.com/) support staff to perform a variety of MySQL and system tasks that are too difficult or complex to perform manually.
以下是Percona Toolkit可以做的事情:
检查主从的数据一致性:
有效的对记录进行归档:
查找重复的索引
对服务器信息进行归档
分析来自日志和tcpdump的查询
当系统出问题的时候收集重要的系统信息
可以使用的命令有:
pt-align
pt-archiver
pt-config-diff
pt-deadlock-logger
pt-diskstats
pt-duplicate-key-checker
pt-fifo-split
pt-find
pt-fingerprint
pt-fk-error-logger
pt-heartbeat
pt-index-usage
pt-ioprofile
pt-kill
pt-mext
pt-mysql-summary
pt-online-schema-change
pt-pmp
pt-query-digest
pt-show-grants
pt-sift
pt-slave-delay
pt-slave-find
pt-slave-restart
pt-stalk
pt-summary
pt-table-checksum
pt-table-sync
pt-table-usage
pt-upgrade
pt-variable-advisor
pt-visual-explain
pt-align - Align output from other tools to columns.
ex: netstat -aniv |pt-align vmstat 2 5 | pt-align pt-align [file]
pt-archiver Archive rows from a MySQL table into another table or a file.
ex:
1.从线上服务器把数据归档到线下服务器和本地文件
pt-archiver --source h=localhost,D=test,t=tbl --dest h=localhost --file ‘/var/log/archive/%Y-%m-%d-%D.%t‘ --where "id=1" --limit 1000 --commit-each
2.只做清理
pt-archiver --source h=host,D=db,t=child --purge --where ‘NOT EXISTS(SELECT * FROM parent WHERE col=child.col)‘
3.每1000行提交一次,每2000行打印一个标题显示进度,不删除源表数据,在归档完成后,打印统计信息.
pt-archiver --source h=127.0.0.1,P=3306,u=‘xxxx‘,p=‘xxxx‘,D=db,t=tbl --file ‘/tmp/%D.%t_%Y%m%d%H%i%s.txt‘ --charset ‘utf8‘ --where "Time < ‘2015-01-01 00:00:00‘" --limit 1000 --commit-each --progress 2000 --statistics --no-delete
OPTIONS 注意: 至少指定--dest, --file 或者 --purge三个参数中的一个 --ignore and --replace 不能同时指定 --txn-size and --commit-each 不能同时指定 --low-priority-insert and --delayed-insert 不能同时指定 --share-lock and --for-update 不能同时指定 --analyze and --optimize 不能同时指定 --no-ascend and --no-delete 不能同时指定 默认情况下,--dest从--source中复制DSN字符串.即,在同一个MySQL Server上面把数据归档到另外一个表. --analyze在数据归档完成后,执行ANALYZE TABLE命令.d是在目的端执行,s是在源端执行. --analyze=ds --ascend-first只使用第一列为升序的索引. --ask-pass交互模式输入密码 --buffer指定--file参数时,提供缓冲功能.性能可能会提高(5-15)%风险: 在归档大事务的时候,如果发生宕机,可能会造成数据丢失. --bulk-delete用单独的sql语句,每次删除一个块的数据.可以加快删除的速度.不推荐使用.正常情况下,是根据主键,一行一行的删除. --[no]bulk-delete-limit为--bulk-delete参数添加limit选项 --bulk-insert使用"LOAD DATA INFILE"方式代替INSERT方式写入. --charset指定字符集,例如 --charset ‘utf8‘ --[no]check-charset默认开启,检查连接的字符集与表的字符集是否一致. --[no]check-columns检查source和dest是否具有相同的列(不减查列的顺序,数据类型等).如果不同,则报错退出. --check-interval默认1s如果指定了--check-slave-lag 参数,那么每秒都会检查从库的延迟情况(帐号要有权限连接从库). --check-slave-lag指定一个从库的DSN串,检查复制延迟的情况,如果大于--max-lag,就会暂停归档. --columns指定归档的列(用逗号分割),写入文件和目的库表.注意: 没有指定列,在原表也会被删除.也就说,未选择列的数据,就会丢失. --commit-each配合--limit参数,一组一组的归档数据 --config参考下面的"如何读取配置文件" --delayed-insert增加DELAYED属性 --dry-run不做任何操作,只打印要执行的查询语句. --file归档到文件,文件内容相当于是SELECT INTO OUTFILE语法导出的数据,文件名可以增加时间戳和库名: %d Day of the month, numeric (01..31) %H Hour (00..23) %i Minutes, numeric (00..59) %m Month, numeric (01..12) %s Seconds (00..59) %Y Year, numeric, four digits %D Database name %t Table name 例如 --file ‘/var/log/archive/%Y-%m-%d-%D.%t‘ --for-update为SELECT语句增加FOR UPDATE属性 --header在归档文件的第一行加入列名.注意,在LOAD DATA INFILE时,别写入多余的数据. --high-priority-select增加HIGH_PRIORITY 修饰符See http://dev.mysql.com/doc/en/select.html for details. --limit默认值1指定每次归档多少行. --local执行OPTIMZE或者ANALYZE语句时,不写binlog,只在本地执行. --low-priority-delete Adds the LOW_PRIORITY modifier to DELETE statements.See http://dev.mysql.com/doc/en/delete.html for details. --low-priority-insertAdds the LOW_PRIORITY modifier to INSERT or REPLACE statements.See http://dev.mysql.com/doc/en/insert.html for details. --max-lag默认是1s, 从库延迟超过这个设置值就会自动暂停. --no-ascend不使用顺序索引优化 --no-delete不在source上面删除归档数据 --optimize归档完成后,执行 OPTIMIZE TABLE --pid指定pid文件 --progress每隔多少行,打印一次信息. --purge只删除,不做归档.可以省略--file和--dest选项 --quiet不打印任何输出 --replace在dest端,使用REPLACE INSERT语句 --retries遇到超时或死锁时,重试的次数.默认是1次 --run-time运行多长时间后退出.可以指定如下后缀,如果没有后缀,默认是秒 s=seconds, m=minutes, h=hours, d=days --sentinel默认路径: /tmp/pt-archiver-sentinel如果这个文件存在,则直接退出. --set-vars可以设置mysql的变量,多个变量用逗号分割. --set-vars wait_timeout=500 --skip-foreign-key-checks禁用外键检查,相当于执行了 SET FOREIGN_KEY_CHECKS=0 --sleep指定两次SELECT语句的sleep时间.默认是没有sleep的.如果指定commit-each参数,commit和flush会在sleep之前发生. --stop创建一个哨兵文件,停止正在运行的pt-archiver进程.例如session1正在执行归档操作,然后我用session2创建一个哨兵文件,那么session1的操作会立刻停止. --txn-size指定每个事务的行数. --where (重要)指定归档数据的过滤条件. --why-quit打印退出的原因,归档数据正常完成的除外. 如何读取配置文件
参数 --dry-run 只打印命令,不执行 --file 归档到文件 --limit和--commit-each配合使用,多少行执行一次commit --purge 清理
cat /etc/percona-toolkit/pt-archiver.conf <<EOF purge limit = 10 commit-each source = A=utf8,D=test,P=3306,h=127.0.0.1,p=...,t=t1,u=username where = id<515
命令 pt-archiver --config /etc/percona-toolkit/pt-archiver.conf
本文出自 “孜孜不倦的学习着...” 博客,请务必保留此出处http://jonyisme.blog.51cto.com/3690784/1754250
原文:http://jonyisme.blog.51cto.com/3690784/1754250