pt工具之pt-archiver


# tar -zxvf percona-toolkit-2.2.17.tar.gz
# yum -y install perl perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes perl-TermReadKey
# yum -y install perl-Digest-MD5

[root@hongquan1 bin]# pwd
/data/soft/percona-toolkit-2.2.17/bin

pt-archiver--將表數據歸檔到另一個表或文件中
刪除或歸檔一張大表,導出文件等,可以進行主從同步數據
[mysql@mysqlt1 bin]$ ./pt-archiver --help
Archive all rows from oltp_server to olap_server and to a file:
pt-archiver --source h=oltp_server,D=test,t=tbl --dest h=olap_server \
--file '/var/log/archive/%Y-%m-%d-%D.%t' \
--where "1=1" --limit 1000 --commit-each
Purge (delete) orphan rows from child table:
pt-archiver --source h=host,D=db,t=child --purge \
--where 'NOT EXISTS(SELECT * FROM parent WHERE col=child.col)'

參數說明:注意:至少需要指定--dest,--file,--purge 其中的一個
--ignore and --replace are mutually exclusive.
--txn-size and --commit-each are mutually exclusive.
--low-priority-insert and --delayed-insert are mutually exclusive.
--share-lock and --for-update are mutually exclusive.
--analyze and --optimize are mutually exclusive.
--no-ascend and --no-delete are mutually exclusive.

--source :指定要歸檔表的信息,兼容DSN選項
--source h=my_server,D=my_database,t=my_tbl
b:如果為true,則使用SQL_LOG_BIN禁用binlog--If true, disable binlog with SQL_LOG_BIN.
i:進行操作時,被指定使用的索引 --Index to use.
h=host,D=database,t=table,u=user,p=password,P=port,S=socket
A:Default character set
L:Explicitly enable LOAD DATA LOCAL INFILE.
m:Plugin module name
--source D=test,t=test1,m=My::Module1 --dest m=My::Module2,t=test2
--analyze:Run ANALYZE TABLE afterwards on --source and/or --dest ###--analyze=ds
--ascend-first:Ascend only first column of index ###升序索引優化,提供最左索引(多列主鍵)的升序。
--no-ascend:Do not use ascending index optimization.
--ask-pass:Prompt for a password when connecting to MySQL. ##連接mysql時輸入密碼
--buffer:緩沖區輸出到--file並在提交時刷新,每次事務提交禁止刷寫到磁盤,有操作系統決定刷寫。該參數可以提高刷寫到文件的性能,但崩潰可能會有數據丟失。
--commit-each:Commit each set of fetched and archived rows (disables --txn-size).##控制事務大小,每次提取、歸檔就提交。禁用--txn-size
--config:以逗號分隔的配置文件列表; 如果指定,則必須是命令行上的第一個選項
--delayed-insert:Add the DELAYED modifier to INSERT statements##在insert后面添加delayed,延遲寫入
--dry-run: Print queries and exit without doing anything##打印查詢並退出而不做任何事情
--file:File to archive to, with DATE_FORMAT()-like formatting
%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
Example:-file '/var/log/archive/%Y-%m-%d-%D.%t'
--for-update:Adds the FOR UPDATE modifier to SELECT statements.
--ignore:insert語句加入ignore
--no-delete:不要刪除存檔的行,默認會刪除。不允許--no-ascend,因為啟用它們都會導致無限循環。
--progress:每多少行打印進度信息:打印當前時間,已用時間以及每X行存檔的行數
--purge:清除而不是歸檔; 允許省略--file和--dest。如果只想清除行,請考慮使用--primary-key-only指定表的主鍵列。 這樣可以防止無緣無故地從服務器獲取所有列
--quick-delete:delete語句里添加quick
--replace:replace into代替insert into
--statistics:收集並打印時間統計信息
--txn-size:每個事務的行數,默認1。指定每個事務的大小(行數)。0完全禁用事務。在pt-archiver處理這么多行之后,如果指定該參數,它會提交--source和--dest,並刷新--file給出的文件。
--where:指定WHERE子句以限制存檔的行。 子句里不要包含單詞WHERE,不需要WHERE子句,請使用--where 1=1。如
--where 'ts < current_date - interval 90 day'
--limit:限制檢索要歸檔的行的SELECT語句返回的行數,默認是1。這可能會導致與其他查詢的更多爭用,具體取決於存儲引擎,事務隔離級別和--for-update等選項。
--bulk-delete:使用單個DELETE語句批量刪除每個行塊。該語句刪除塊的第一行和最后一行之間的每一行,隱含--commit-each.批量刪除source上的舊數據
--bulk-insert:批量插入數據到dest主機
--charset:-A,設置默認字符集
--why-quit:除非行耗盡,否則打印退出原因
使用場景
注意: 歸檔的表大小寫敏感,表必須至少有一個索引(Cannot find an ascendable index in table )。
這里需要注意的是,根據自增id進行歸檔的話,默認最大的id不會進行歸檔,需要添加參數:--no-safe-auto-increment 才能對最大id進行處理。
--where:刪除表中指定的數據,根據自己的需求限定,全部刪除就給1=1即可
--statistics:打印出整個歸檔過程的統計信息
--limit:每次fecth多少行數據,類似游標獲取,默認為1。增改該值,有助於加速歸檔 --limit 10000 每次取1000行數據給pt-archive處理
--progress:打印導出過程中的信息,當前時間,當前一共耗費多少時間,當前fetch數據行數,--progress 5000 每處理5000行輸出一次處理信息
--txn-size:每個事物提交的數據行數,批量提交。增加該值可以提升歸檔性能。 --txn-size 1000 設置1000行為一個事務提交一次
--local:不把optimize或analyze操作寫入到binlog里面(防止造成主從延遲巨大)
--analyze=ds:操作結束后,優化表空間(d表示dest,s表示source),默認情況下,pt-archiver操作結束后,不會對source、dest表執行analyze或optimize操作

10.15.7.114 :mysql version=5.6.15,charset=utf8

192.168.19.145:mysql version=5.7.22,charset=utf8mb4


1導出到文件,不刪除源數據
2019-03-26T02:02:14 0 0
Cannot find encoding "utf8mb4" at /usr/lib64/perl5/IO/File.pm line 182.
Cannot open :encoding(utf8mb4) /tmp/2019-03-26-test.t1: Invalid argument

[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --file=/tmp/%Y-%m-%d-%D.%t --where="1=1" \
> --no-delete --no-safe-auto-increment --progress=1000 --statistics --no-check-charset
TIME ELAPSED COUNT
2019-03-26T02:04:10 0 0
2019-03-26T02:04:10 0 1000
2019-03-26T02:04:10 0 1000
Started at 2019-03-26T02:04:10, ended at 2019-03-26T02:04:10
Source: D=test,P=3306,h=10.15.7.114,p=...,t=t1,u=system
SELECT 1000
INSERT 0
DELETE 0
Action Count Time Pct
select 1001 0.1258 58.07
commit 1001 0.0316 14.57
print_file 1000 0.0015 0.68
other 0 0.0578 26.68
[mysql@mysqlt1 bin]$ ll /tmp/2019-03-26-test.t1
-rw-rw-r-- 1 mysql mysql 11679 Mar 26 02:04 /tmp/2019-03-26-test.t1
2刪除,不導出和遷移
[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --where="1=1" --purge \
> --no-safe-auto-increment --progress=1000 --statistics --no-check-charset
TIME ELAPSED COUNT
2019-03-26T02:07:49 0 0
2019-03-26T02:07:50 1 1000
2019-03-26T02:07:50 1 1000
Started at 2019-03-26T02:07:49, ended at 2019-03-26T02:07:50
Source: D=test,P=3306,h=10.15.7.114,p=...,t=t1,u=system
SELECT 1000
INSERT 0
DELETE 1000
Action Count Time Pct
commit 1001 1.5619 81.38
deleting 1000 0.1455 7.58
select 1001 0.1315 6.85
other 0 0.0804 4.19
(system@127.0.0.1:3306) [test]> select count(*) from test.t1;
+----------+
| count(*) |
+----------+
| 0 |
3全表歸檔,源表不刪除,非批量
DBD::mysql::st execute failed: Duplicate entry '1' for key 'PRIMARY' [for Statement "INSERT INTO `test`.`t37`(`id`,`a`,`b`) VALUES (?,?,?)" with ParamValues: 0='1', 1='1', 2='1'] at ./pt-archiver line 6563.
'1', 1='1', 2='1'] at ./pt-archiver line 6563.
[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 --where="1=1" --progress=1000 --statistics --no-delete --no-check-charset
TIME ELAPSED COUNT
2019-03-26T02:20:09 0 0
2019-03-26T02:20:11 2 1000
2019-03-26T02:20:11 2 1000
Started at 2019-03-26T02:20:09, ended at 2019-03-26T02:20:11
Source: D=test,P=3306,h=10.15.7.114,p=...,t=t1,u=system
Dest: D=test,P=3306,h=192.168.19.145,p=...,t=t37,u=system
SELECT 1000
INSERT 1000
DELETE 0
Action Count Time Pct
commit 2002 1.6199 70.67
inserting 1000 0.4391 19.16
select 1001 0.1419 6.19
other 0 0.0913 3.98
全表歸檔,源表不刪除,批量插入
[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 \
> --where="1=1" --limit=1000 --statistics --bulk-insert --txn-size=1000 --no-delete --no-check-charset
Started at 2019-03-26T02:22:48, ended at 2019-03-26T02:22:48
Source: D=test,P=3306,h=10.15.7.114,p=...,t=t1,u=system
Dest: D=test,P=3306,h=192.168.19.145,p=...,t=t37,u=system
SELECT 1000
INSERT 1000
DELETE 0
Action Count Time Pct
bulk_inserting 1 0.0544 61.78
commit 4 0.0034 3.84
select 2 0.0015 1.69
print_bulkfile 1000 -0.0017 -1.91
other 0 0.0304 34.61
5全表歸檔,源表刪除,批量插入,批量刪除
./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 \
--where="1=1" --limit=1000 --statistics --bulk-insert --bulk-delete --txn-size=1000 --no-delete --no-check-charset
6指定條件歸檔,源表刪除,批量(每1000個插入提交一次)
./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1 --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 \
--where="id<=49999" --limit=1000 --statistics --bulk-insert --bulk-delete --txn-size=1000 --no-delete --no-check-charset
7指定索引的歸檔,不走自增主鍵索引。參數:i
./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1,i=a --dest u=system,p=***,h=192.168.19.145,P=3306,D=test,t=t37 \
--where="a >=80000 and a<100000" --limit=1000 --statistics --bulk-insert --bulk-delete --txn-size=1000 --no-delete --no-check-charset
8有從庫的歸檔
./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1,i=a --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 \
--where="a >=80000 and a<100000" --limit=1000 --statistics --bulk-insert --bulk-delete --txn-size=1000 --no-delete --no-check-charset \
--max-lag=1 --check-slave-lag u=system,p=**,h=10.15.7.115,P=3306
9不做任何操作,只打印要執行的查詢語句
[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1,i=a --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 \> --where="a >=1000 and a<3000" --limit=1000 --replace --statistics --txn-size=1000 --no-delete --no-check-charset --dry-run
SELECT /*!40001 SQL_NO_CACHE */ `id`,`a`,`b` FROM `test`.`t1` FORCE INDEX(`a`) WHERE (a >=1000 and a<3000) ORDER BY `a` LIMIT 1000
SELECT /*!40001 SQL_NO_CACHE */ `id`,`a`,`b` FROM `test`.`t1` FORCE INDEX(`a`) WHERE (a >=1000 and a<3000) AND ((((? IS NULL AND `a` IS NOT NULL) OR (`a` > ?)))) ORDER BY `a` LIMIT 1000
REPLACE INTO `test`.`t37`(`id`,`a`,`b`) VALUES (?,?,?)
10常用的命令:歸檔到另一個數據庫,源表刪除,批量刪除和插入,每1000次修改進行提交。跳過錯誤並且指定字符集連接
[mysql@mysqlt1 bin]$ ./pt-archiver --source u=system,p=**,h=10.15.7.114,P=3306,D=test,t=t1,i=a --dest u=system,p=**,h=192.168.19.145,P=3306,D=test,t=t37 --no-version-check\
> --where="a >=500 and a<800" --ignore --txn-size=200 --limit=200 --bulk-delete --bulk-insert --progress=5000 --statistics --why-quit --no-check-charset
TIME ELAPSED COUNT
2019-03-26T03:13:10 0 0
2019-03-26T03:13:11 0 300
Started at 2019-03-26T03:13:10, ended at 2019-03-26T03:13:11
Source: D=test,P=3306,h=10.15.7.114,i=a,p=...,t=t1,u=system
Dest: D=test,P=3306,h=192.168.19.145,i=a,p=...,t=t37,u=system
SELECT 300
INSERT 300
DELETE 300
Action Count Time Pct
commit 4 0.0081 23.57
bulk_inserting 2 0.0061 17.83
bulk_deleting 2 0.0032 9.38
select 3 0.0013 3.74
print_bulkfile 300 -0.0011 -3.28
other 0 0.0168 48.75
Exiting because there are no more rows.
可以根據實際情況,進行相關參數的調整。另外其他相關參數說明
--ignore或則--replace:歸檔沖突記錄跳過或則覆蓋,批量插入的時候因為是load data,索引看不到主鍵沖突記錄的報錯。要是非批量插入,則需要添加。
--sleep:指定兩次SELECT語句的sleep時間.默認是沒有sleep的。
--why-quit:打印退出的原因,歸檔數據正常完成的除外。
--charset=UTF8:指定字符集。
--analyze:結束歸檔后,優化表空間。

文檔參考

https://www.percona.com/doc/percona-toolkit/LATEST/pt-archiver.html#cmdoption-pt-archiver-source
https://www.cnblogs.com/zhoujinyi/p/9925508.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM