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.
https://www.percona.com/doc/percona-toolkit/2.2/index.html
以下是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