pt-archiver工具的使用

小亮520cl發表於2015-11-10
pt-archiver工具其實就是用來清理,歸檔資料用的
下面列舉了三種常用的功能

1,只清理資料
mysql> use sb
Database changed

mysql> select * from sb2;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
|  5 | e    |
|  6 | f    |
|  7 | h    |
+----+------+

mysql> alter table sb2 add primary key (id);
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

把sb庫裡sb2表的資料,id >=5的資料清除
[root@goolen goolen]# pt-archiver --source h=192.168.100.50,D=sb,t=sb2 --user=root --password=root --where 'id>=5' --purge --limit=1 --no-check-charset

執行完後
觀察日誌輸出:
141128 11:47:52     8 Connect   root@192.168.100.50 on sb
                    8 Query     set autocommit=0
                    8 Query     SELECT @@SQL_MODE
                    8 Query     SHOW VARIABLES LIKE 'wait\_timeout'
                    8 Query     SET SESSION wait_timeout=10000
                    8 Query     SET @@SQL_QUOTE_SHOW_CREATE = 1/*!40101, @@SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/
                    8 Query     SHOW VARIABLES LIKE 'version%'
                    8 Query     SHOW ENGINES
                    8 Query     SHOW VARIABLES LIKE 'innodb_version'
                    8 Query     show variables like 'innodb_rollback_on_timeout'
                    8 Query     /*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, @@SQL_MODE := '', @OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, @@SQL_QUOTE_SHOW_CREATE := 1 */
                    8 Query     USE `sb`
                    8 Query     SHOW CREATE TABLE `sb`.`sb2`
                    8 Query     /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
                    8 Query     SHOW VARIABLES LIKE 'version%'
                    8 Query     SHOW ENGINES
                    8 Query     SHOW VARIABLES LIKE 'innodb_version'
                    8 Query     SELECT CONCAT(@@hostname, @@port)
                    8 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (id>=5) ORDER BY `id` LIMIT 1
                    8 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '5')
                    8 Query     commit
                    8 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (id>=5) AND ((`id` >= '5')) ORDER BY `id` LIMIT 1
                    8 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '6')
                    8 Query     commit
                    8 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (id>=5) AND ((`id` >= '6')) ORDER BY `id` LIMIT 1
                    8 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '7')
                    8 Query     commit
                    8 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (id>=5) AND ((`id` >= '7')) ORDER BY `id` LIMIT 1
                    8 Query     commit
                    8 Quit
141128 11:47:53     1 Query     SELECT 1 As Value
141128 11:47:56     1 Query     SELECT 1 As Value

查一下源表資料,id >=5 的資料已經從源表刪除了
mysql> select * from sb2;                   
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+

引數說明:
--purge 清理資料,直接刪除源表裡的資料,謹慎使用;
--where 指定刪除條件,必須的條件,有字串的,要用引號括起來
--limit 表示一次要刪除多少行資料,這裡設定為1,表示一次只刪除一行,根據說明的日誌跟蹤可以看出來,id>=5的有三條資料,分三次刪除,每次只刪除一條資料


2,只把資料匯出到外部檔案,但是不刪除源表裡的資料:
mysql> select * from sb2;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

[root@goolen goolen]# pt-archiver --source h=192.168.100.50,D=sb,t=sb2 --user=root --password=root --where '1=1' --no-check-charset --no-delete --file="/tmp/archiver.dat" 
[root@goolen goolen]# cat /tmp/archiver.dat 
1       a
2       b
3       c
4       d

檢視源表資料,由於加了--no-delete引數,源表資料還在
mysql> select * from sb2;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

檢視查詢日誌輸出:
9 Query     USE `sb`
9 Query     SHOW CREATE TABLE `sb`.`sb2`
9 Query     /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
9 Query     SHOW VARIABLES LIKE 'version%'
9 Query     SHOW ENGINES
9 Query     SHOW VARIABLES LIKE 'innodb_version'
9 Query     SELECT CONCAT(@@hostname, @@port)
9 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) ORDER BY `id` LIMIT 1
9 Query     commit
9 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` > '1')) ORDER BY `id` LIMIT 1
9 Query     commit
9 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` > '2')) ORDER BY `id` LIMIT 1
9 Query     commit
9 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` > '3')) ORDER BY `id` LIMIT 1
9 Query     commit
9 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` > '4')) ORDER BY `id` LIMIT 1
9 Query     commit

引數說明:
--no-delete 表示不刪除源表裡的資料;
--file 指定資料要匯出到哪個檔案;


3,把資料歸檔到其他表(支援遠端歸檔)
mysql> select * from sb2;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

[root@goolen goolen]# pt-archiver --source h=192.168.100.50,D=sb,t=sb2,u=root,p=root --dest h=192.168.100.92,D=goolen,t=sb250,u=root,p=root --where '1=1' --no-check-charset

檢視源表資料,由於未加--no-delete引數,源表資料清除了
mysql> select * from sb2;
Empty set (0.00 sec)

檢視目標資料庫裡的表,資料已經成功歸檔過來
mysql> select * from sb250;
+----+------+
| id | name |
+----+------+
|  1 | a    | 
|  2 | b    | 
|  3 | c    | 
|  4 | d    | 
+----+------+
4 rows in set (0.00 sec)

檢視查詢日誌:
10 Query     USE `sb`
10 Query     SHOW CREATE TABLE `sb`.`sb2`
10 Query     /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
10 Query     SHOW VARIABLES LIKE 'version%'
10 Query     SHOW ENGINES
10 Query     SHOW VARIABLES LIKE 'innodb_version'
10 Query     SELECT CONCAT(@@hostname, @@port)
10 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) ORDER BY `id` LIMIT 1
10 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '1')
10 Query     commit
10 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` >= '1')) ORDER BY `id` LIMIT 1
10 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '2')
10 Query     commit
10 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` >= '2')) ORDER BY `id` LIMIT 1
10 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '3')
10 Query     commit
10 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` >= '3')) ORDER BY `id` LIMIT 1
10 Query     DELETE FROM `sb`.`sb2` WHERE (`id` = '4')
10 Query     commit
10 Query     SELECT /*!40001 SQL_NO_CACHE */ `id`,`name` FROM `sb`.`sb2` FORCE INDEX(`PRIMARY`) WHERE (1=1) AND ((`id` >= '4')) ORDER BY `id` LIMIT 1
10 Query     commit


引數說明:
--dest 指定遠端資料庫資訊
--no-delete 如果不指定次引數,當資料歸檔到遠端庫後,會刪除源表的資料,使用時需要注意

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1826779/,如需轉載,請註明出處,否則將追究法律責任。

相關文章