pt-archiver 數據刪除、遷移工具使用
1. 數據庫連接參數
參數 |
說明 |
A |
字符編碼 |
D |
庫 |
F |
從文件讀取選項 |
L |
加載數據本地文件 |
P |
端口 |
S |
socket文件 |
a |
執行查詢的數據庫 |
b |
如果是true, 禁用SQL_LOG_BIN |
h |
數據庫地址 |
i |
查詢使用的索引 |
m |
插件模塊名稱 |
p |
數據庫密碼 |
t |
表 |
u |
用戶名 |
2. 常用參數
參數 |
默認值 |
說明 |
--limit 10000 |
每次取1000行數據用pt-archive處理,Number of rows to fetch and archive per statement. |
--txn-size 1000 |
設置1000行為一個事務提交一次,Number of rows pertransaction. |
--where 'id<3000' |
|
設置操作條件 |
--progress 5000 |
|
每處理5000行輸出一次處理信息 |
--statistics |
|
輸出執行過程及最后的操作統計 |
--charset=UTF8 |
|
指定字符集為UTF8 |
--bulk-delete |
|
批量刪除source上的舊數據(例如每次1000行的批量刪除操作) |
--bulk-insert |
|
批量插入數據到dest主機 (看dest的general log發現它是通過在dest主機上LOAD DATA LOCAL INFILE插入數據的) |
--replace |
|
將insert into 語句改成replace寫入到dest庫 |
--sleep 120 |
|
每次歸檔了limit個行記錄后的休眠120秒(單位為秒) |
--file '/root/test.txt' |
|
導出的文件路徑 |
--purge |
|
刪除source數據庫的相關匹配記錄 |
--header |
|
輸入列名稱到首行(和--file一起使用) |
-no-check-charset |
|
不指定字符集 |
--check-columns |
|
檢驗dest和source的表結構是否一致,不一致自動拒絕執行(不加這個參數也行。默認就是執行檢查的) |
--no-check-columns |
|
不檢驗dest和source的表結構是否一致,不一致也執行(會導致dest上的無法與source匹配的列值被置為null或者0) |
--chekc-interval |
|
默認1s檢查一次 |
--local |
|
不把optimize或analyze操作寫入到binlog里面(防止造成主從延遲巨大) |
--retries |
|
超時或者出現死鎖的話,pt-archiver進行重試的間隔(默認1s) |
--no-version-check |
|
目前為止,發現部分pt工具對阿里云RDS操作必須加這個參數 |
--analyze=ds |
|
操作結束后,優化表空間(d表示dest,s表示source) |
2. example
1. 刪除老數據
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
2. 復制數據到其他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
3. 復制數據到其他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
4. 復制數據到其他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
5. 導出數據到文件
/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
6. 導出數據到文件并刪除數據庫的相關行:
/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
