1. 程式人生 > >pt-archiver歸檔工具的使用詳解

pt-archiver歸檔工具的使用詳解

pt-archiver使用

之前的percona-toolkit工具集的使用博文裏面也寫到pt-archiver這個工具的用法,但是不夠深入全面。這裏補充完善下。


註意:pt-archiver操作的表必須有主鍵。

查看幫助: perldoc pt-archiver

Specify at least one of "--dest","--file", or "--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.

常用的參數:

--limit10000 每次取1000行數據用pt-archive處理,Number of rows to fetch and archive per statement.

--txn-size 1000 設置1000行為一個事務提交一次,Number of rows pertransaction.

--where‘id<3000‘ 設置操作條件

--progress5000 每處理5000行輸出一次處理信息

--statistics 輸出執行過程及最後的操作統計。(只要不加上--quiet,默認情況下pt-archive都會輸出執行過程的)

--charset=UTF8 指定字符集為UTF8

--bulk-delete 批量刪除source上的舊數據(例如每次1000行的批量刪除操作)

--bulk-insert

批量插入數據到dest主機 (destgeneral log發現它是通過在dest主機上LOAD DATA LOCAL INFILE插入數據的)

--replace insert into 語句改成replace寫入到dest

--sleep120 每次歸檔了limit個行記錄後的休眠120秒(單位為秒)

--file‘/root/test.txt‘

--purge 刪除source數據庫的相關匹配記錄

--header 輸入列名稱到首行(和--file一起使用)

--no-check-charset 不指定字符集

--check-columns 檢驗destsource的表結構是否一致,不一致自動拒絕執行(不加這個參數也行。默認就是執行檢查的)

--no-check-columns 不檢驗destsource的表結構是否一致,不一致也執行(會導致dest上的無法與source匹配的列值被置為null或者0

--chekc-interval 默認1s檢查一次

--local 不把optimizeanalyze操作寫入到binlog裏面(防止造成主從延遲巨大)

--retries 超時或者出現死鎖的話,pt-archiver進行重試的間隔(默認1s

--no-version-check 目前為止,發現部分pt工具對阿裏雲RDS操作必須加這個參數

--analyze=ds 操作結束後,優化表空間(d表示dests表示source

默認情況下,pt-archiver操作結束後,不會對sourcedest表執行analyzeoptimize操作,因為這種操作費時間,並且需要你提前預估有足夠的磁盤空間用於拷貝表。一般建議也是pt-archiver操作結束後,在業務低谷手動執行analyze table用以回收表空間。

pt-archiverBug不會遷移max(id)那條數據的解決方法:

參考:http://www.ttlsa.com/mysql/pt-archiver-bug-cannot-migration-max-id-record/

vim/usr/bin/pt-archiver +6285 ,如下:

修改前: $first_sql .= " AND ($col < " . $q->quote_val($val) . ")";

修改後: $first_sql .= " AND ($col <= " . $q->quote_val($val) .")";

刪除老數據(單獨的刪數據操作不用指定字符集)

/usr/bin/pt-archiver \

--source h=localhost,u=root,p=1234,P=3306,D=test,t=t \

--no-check-charset --where ‘a<=376‘ --limit 10000 --txn-size 1000 --purge

復制數據到其他mysql實例,且不刪除source的數據(指定字符集)

/usr/bin/pt-archiver \

--source h=localhost,u=root,p=1234,P=3306,D=test,t=t1\

--dest h=192.168.2.12,P=3306,u=archiver,p=archiver,D=test,t=t1_bak \

--progress 5000 --where ‘mc_id<=125‘ \

--statistics --charset=UTF8 --limit=10000 --txn-size 1000 --no-delete

復制數據到其他mysql實例,並刪source上的舊數據(指定字符集)

/usr/bin/pt-archiver \

--source h=localhost,u=root,p=1234,P=3306,D=test,t=t1 \

--dest h=192.168.2.12,P=3306,u=archiver,p=archiver,D=test,t=t1_his \

--progress 5000 --where "CreateDate <‘2017-05-01 00:00:00‘ " \

--statistics --charset=UTF8 --limit=10000 --txn-size 1000 --bulk-delete

### 官方文檔說明:The normal method isto delete every row by its primary key. Bulk deletes might be a lot faster.They also

mightnot be faster if you have a complex WHERE clause.

復制數據到其他mysql實例,不刪除source數據,但是使用批量插入dest上新的數據(指定字符集)

/usr/bin/pt-archiver \

--source h=localhost,u=archiver,p=archiver,P=3306,D=test,t=t1 \

--dest h=192.168.2.12,P=3306,u=archiver,p=archiver,D=test,t=t1_his \

--progress 5000 --where "c <‘2017-05-01 00:00:00‘ " \

--statistics --charset=UTF8 --limit=10000 --txn-size 1000 --no-delete --bulk-insert

### 測試用的一張只有3列元素的表,共計9萬行數據。使用bulk-insert用時7秒鐘。而常規insert用時40秒。

導出數據到文件:

/usr/bin/pt-archiver \

--source h=10.0.20.26,u=root,p=1234,P=3306,D=test,t=t \

--file ‘/root/test.txt‘ \

--progress 5000 --where ‘a<12000‘ \

--no-delete --statistics --charset=UTF8 --limit=10000 --txn-size 1000


導出數據到文件並刪除數據庫的相關行:

/usr/bin/pt-archiver \

--source h=10.0.20.26,u=root,p=1234,P=3306,D=test,t=t \

--file ‘/root/test.txt‘ \

--progress 5000 --where ‘a<12000‘ \

--statistics --charset=UTF8 --limit=10000 --txn-size 1000 --purge




本文出自 “一只菜雞的筆記” 博客,請務必保留此出處http://lee90.blog.51cto.com/10414478/1947357

pt-archiver歸檔工具的使用詳解