1. pt工具安裝
[root@master ~]# yum install -y percona-toolkit-3.1.0-2.el7.x86_64.rpm
2. 常用工具使用介紹
pt-archiver 歸檔表
# 重要參數(shù)
--limit 100 每次取100行數(shù)據(jù)用pt-archive處理
--txn-size 100 設(shè)置100行為一個事務(wù)提交一次,
--where 'id<3000' 設(shè)置操作條件
--progress 5000 每處理5000行輸出一次處理信息
--statistics 輸出執(zhí)行過程及最后的操作統(tǒng)計。(只要不加上--quiet,默認(rèn)情況下pt- archive都會輸出執(zhí)行過程的)
--charset=UTF8 指定字符集為UTF8—這個最后加上不然可能出現(xiàn)亂碼。
--bulk-delete 批量刪除source上的舊數(shù)據(jù)(例如每次1000行的批量刪除操作)
使用案例:
1.歸檔到數(shù)據(jù)庫
pt-archiver --source h=10.0.0.11,D=world,t=city,u=root,p=123 --dest h=10.0.0.11,D=world,t=city2,u=root,p=123 --where 'id<1000' --no-check-charset --no-delete --limit=100 --commit-each --progress 200 --statistics
2.只清理數(shù)據(jù)
pt-archiver --source h=127.0.0.1,D=world,t=city2,u=root,p=123 --where 'id<100' --purge --limit=1 --no-check-charset
3.只把數(shù)據(jù)導(dǎo)出到外部文件,但是不刪除源表里的數(shù)據(jù)
pt-archiver --source h=10.0.0.11,D=world,t=city,u=root,p=123 --where '1=1' --no-check-charset --no-delete --file="/tmp/archiver.dat"
pt-osc
pt-osc工作流程:
1、檢查更改表是否有主鍵或唯一索引,是否有觸發(fā)器
2、檢查修改表的表結(jié)構(gòu),創(chuàng)建一個臨時表,在新表上執(zhí)行ALTER TABLE語句
3、在源表上創(chuàng)建三個觸發(fā)器分別對于INSERT UPDATE DELETE操作
4、從源表拷貝數(shù)據(jù)到臨時表,在拷貝過程中,對源表的更新操作會寫入到新建表中
5、將臨時表和源表rename(需要元數(shù)據(jù)修改鎖,需要短時間鎖表)
6、刪除源表和觸發(fā)器,完成表結(jié)構(gòu)的修改。
##=====================================================##
pt-osc工具限制
1、源表必須有主鍵或唯一索引,如果沒有工具將停止工作
2、如果線上的復(fù)制環(huán)境過濾器操作過于復(fù)雜,工具將無法工作
3、如果開啟復(fù)制延遲檢查,但主從延遲時,工具將暫停數(shù)據(jù)拷貝工作
4、如果開啟主服務(wù)器負(fù)載檢查,但主服務(wù)器負(fù)載較高時,工具將暫停操作
5、當(dāng)表使用外鍵時,如果未使用--alter-foreign-keys-method參數(shù),工具將無法執(zhí)行
6、只支持Innodb存儲引擎表,且要求服務(wù)器上有該表1倍以上的空閑空間。
pt-osc之a(chǎn)lter語句限制
1、不需要包含alter table關(guān)鍵字,可以包含多個修改操作,使用逗號分開,如"drop clolumn c1, add column c2 int"
2、不支持rename語句來對表進行重命名操作
3、不支持對索引進行重命名操作
4、如果刪除外鍵,需要對外鍵名加下劃線,如刪除外鍵fk_uid, 修改語句為"DROP FOREIGN KEY _fk_uid"
pt-osc之命令模板
## --execute表示執(zhí)行
## --dry-run表示只進行模擬測試
## 表名只能使用參數(shù)t來設(shè)置,沒有長參數(shù)
pt-online-schema-change \
--host="127.0.0.1" \
--port=3358 \
--user="root" \
--password="root@root" \
--charset="utf8" \
--max-lag=10 \
--check-salve-lag='xxx.xxx.xxx.xxx' \
--recursion-method="hosts" \
--check-interval=2 \
--database="testdb1" \
t="tb001" \
--alter="add column c4 int" \
--execute
例子:
pt-online-schema-change --user=root --password=123 --host=10.0.0.11 --alter "add column age int default 0" D=test,t=t1 --print --execute
pt-table-checksum
創(chuàng)建數(shù)據(jù)庫
Create database pt CHARACTER SET utf8;
創(chuàng)建用戶checksum并授權(quán)
GRANT ALL ON *.* TO 'checksum'@'10.0.0.%' IDENTIFIED BY 'checksum';
flush privileges;
--[no]check-replication-filters:是否檢查復(fù)制的過濾器,默認(rèn)是yes,建議啟用不檢查模式。
--databases | -d:指定需要被檢查的數(shù)據(jù)庫,多個庫之間可以用逗號分隔。
--[no]check-binlog-format:是否檢查binlog文件的格式,默認(rèn)值yes。建議開啟不檢查。因為在默認(rèn)的row格式下會出錯。
--replicate`:把checksum的信息寫入到指定表中。
--replicate-check-only:只顯示不同步信息
pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=pt.checksums --create-replicate-table --databases=test --tables=t1 h=10.0.0.11,u=checksum,p=checksum,P=3306
#!/bin/bash
date >> /root/db/checksum.log
pt-table-checksum --nocheck-binlog-format --nocheck-plan
--nocheck-replication-filters --replicate=pt.checksums --set-vars
innodb_lock_wait_timeout=120 --databases UAR_STATISTIC -u'checksum' -p'checksum'
-h'10.0.0.11' >> /root/db/checksum.log
date >> /root/db/checksum.log
pt-table-sync
主要參數(shù)介紹
--replicate :指定通過pt-table-checksum得到的表.
--databases : 指定執(zhí)行同步的數(shù)據(jù)庫。
--tables :指定執(zhí)行同步的表,多個用逗號隔開。
--sync-to-master :指定一個DSN,即從的IP,他會通過show processlist或show slave status 去自動的找主。
h= :服務(wù)器地址,命令里有2個ip,第一次出現(xiàn)的是Master的地址,第2次是Slave的地址。
u= :帳號。
p= :密碼。
--print :打印,但不執(zhí)行命令。
--execute :執(zhí)行命令。
pt-table-sync --replicate=pt.checksums h=10.0.0.11,u=root,p=123,P=3306 --print
mysql死鎖監(jiān)測
pt-deadlock-logger h='127.0.0.1' --user=root --password=123456
主鍵沖突檢查
pt-duplicate-key-checker --database=world h='127.0.0.1' --user=root --password=123456
pt-kill 語句
常用參數(shù)說明
--daemonize 放在后臺以守護進程的形式運行;
--interval 多久運行一次,單位可以是s,m,h,d等默認(rèn)是s –不加這個默認(rèn)是5秒
--victims 默認(rèn)是oldest,只殺最古老的查詢。這是防止被查殺是不是真的長時間運行的查詢,他們只是長期等待 這種種匹配按時間查詢,殺死一個時間最高值。
--all 殺掉所有滿足的線程
--kill-query 只殺掉連接執(zhí)行的語句,但是線程不會被終止
--print 打印滿足條件的語句
--busy-time 批次查詢已運行的時間超過這個時間的線程;
--idle-time 殺掉sleep 空閑了多少時間的連接線程,必須在--match-command sleep時才有效—也就是匹配使用 -- –match-command 匹配相關(guān)的語句。
----ignore-command 忽略相關(guān)的匹配。 這兩個搭配使用一定是ignore-commandd在前 match-command在后,
--match-db cdelzone 匹配哪個庫
command有:Query、Sleep、Binlog Dump、Connect、Delayed insert、Execute、Fetch、Init DB、Kill、Prepare、Processlist、Quit、Reset stmt、Table Dump
例子:
---殺掉空閑鏈接sleep 5秒的 SQL 并把日志放到/home/pt-kill.log文件中
/usr/bin/pt-kill --user=用戶名 --password=密碼 --match-command Sleep --idle-time 5 --victim all --interval 5 --kill --daemonize -S /tmp/mysql.sock --pid=/tmp/ptkill.pid --print --log=/tmp/pt-kill.log &
---查詢SELECT 超過1分鐘路
/usr/bin/pt-kill --user=用戶名 --password=密碼 --busy-time 60 --match-info "SELECT|select" --victim all --interval 5 --kill --daemonize -S -S /tmp/mysql.sock --pid=/tmp/ptkill.pid --print --log=/tmp/pt-kill.log &
--- Kill掉 select IFNULl.*語句開頭的SQL
pt-kill --user=用戶名 --password=密碼 --victims all --busy-time=0 --match-info="select IFNULl.*" --interval 1 -S /tmp/mysqld.sock --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/tmp/pt-kill.log &
----kill掉state Locked
/usr/bin/pt-kill --user=用戶名 --password=密碼 --victims all --match-state='Locked' --victim all --interval 5 --kill --daemonize -S /tmp/mysqld.sock --pid=/tmp/ptkill.pid --print --log=/tmp/pt-kill.log &
---kill掉 a庫,web為10.0.0.11的鏈接
pt-kill --user=用戶名 --password=密碼 --victims all --match-db='a' --match-host='10.0.0.11' --kill --daemonize --interval 10 -S /tmp/mysqld.sock --pid=/tmp/ptkill.pid --print-log=/tmp/pt-kill.log &
---指定哪個用戶kill
pt-kill --user=用戶名 --password=密碼 --victims all --match-user='root' --kill --daemonize --interval 10 -S /home/zb/data/my6006/socket/mysqld.sock --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log &
---查詢SELECT 超過1分鐘路
pt-kill --user=用戶名 --password=密碼 --busy-time 60 --match-info "SELECT|select" --victim all --interval 5 --kill --daemonize -S /tmp/mysqld.sock --pid=/tmp/ptkill.pid --print --log=/tmp/pt-kill.log &
----kill掉 command query | Execute
pt-kill --user=用戶名 --password=密碼 --victims all --match-command= "query|Execute" --interval 5 --kill --daemonize -S /tmp/mysqld.sock --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log &
其他
pt-find ---找出幾天之前建立的表
pt-slave-restart -----主從報錯,跳過報錯
pt-summary ---整個系統(tǒng)的的概述
pt-mysql-summary ---MySQL的表述,包括配置文件的描述
pt-duplicate-key-checker ---檢查數(shù)據(jù)庫重復(fù)索引