pt-archiver可以將表按照指定條件歸檔到歷史數(shù)據(jù)庫(kù)中,也支持文件導(dǎo)出,對(duì)于歸檔清理線上歷史數(shù)據(jù)非常方便。
如果要?dú)w檔表的數(shù)據(jù)到歷史數(shù)據(jù)庫(kù)的表中,需要預(yù)先在歷史數(shù)據(jù)庫(kù)中創(chuàng)建表結(jié)構(gòu)。
(1) 按照條件歸檔表中的歷史數(shù)據(jù)到歷史數(shù)據(jù)中,同時(shí)在本地生成歸檔文件
-
# 在歷史數(shù)據(jù)庫(kù)(192.168.56.102)中創(chuàng)建歸檔表
-
MySQL> CREATE TABLE `emp` (
-
-> `id` int(11) NOT NULL,
-
-> `name` varchar(15) DEFAULT NULL,
-
-> PRIMARY KEY (`id`)
-
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
Query OK, 0 rows affected (0.60 sec)
-
-
# 查看源表
-
mysql> select * from emp;
-
+--------+---------+
-
| id | name |
-
+--------+---------+
-
| 10 | Neo |
-
| 10036 | test |
-
| 10037 | test |
-
| 10038 | test |
-
| 10039 | test |
-
| 10040 | MySQL01 |
-
| 10041 | MySQL01 |
-
| 10042 | MySQL01 |
-
| 100100 | test |
-
| 100101 | test |
-
| 100103 | test |
-
| 100104 | test |
-
| 100105 | test |
-
| 100106 | test |
-
| 100107 | test |
-
| 100108 | test |
-
+--------+---------+
-
-
# 歸檔id小于200000的數(shù)據(jù)到歷史庫(kù)和本地文件
-
# pt-archiver --source h=192.168.56.101,P=3307,u=neo,p=neo,D=sale,t=emp --dest h=192.168.56.102,P=3306,u=sale,p=sale,D=test,t=emp --where "id<=200000" --charset=utf8 --limit 1000 --commit-each --file '/opt/%Y-%m-%d-%D.%t'
-
-
# 查看源表
-
mysql> select * from emp where id < 200000;
-
Empty set (0.05 sec)
-
-
# 查看歷史表
-
mysql> select * from emp limit 2;
-
+--------+------+
-
| id | name |
-
+--------+------+
-
| 100100 | test |
-
| 100101 | test |
-
+--------+------+
-
2 rows in set (0.00 sec)
-
-
# 查看本地歸檔文件(相當(dāng)于select ... into導(dǎo)出)
-
# cat /opt/2018-03-19-sale.emp
-
10 Neo
-
10036 test
-
10037 test
-
10038 test
-
10039 test
-
10040 MySQL01
-
10041 MySQL01
-
10042 MySQL01
(2) 清理過期歷史數(shù)據(jù)
-
# pt-archiver --source h=192.168.56.101,P=3306,u=neo,p=neo,D=test,t=item_order --where "order_date < '2018-03-01'" --charset=utf8 --purge --limit 1000 --commit-each
-
-
# 查看清理后的表中數(shù)據(jù)
-
mysql> select * from item_order where order_date < '2018-03-01';
-
Empty set (0.00 sec)
本文標(biāo)題:MySQL使用pt-archiver歸檔歷史數(shù)據(jù)
網(wǎng)頁(yè)鏈接:http://www.rwnh.cn/article22/pgccjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)公司、微信公眾號(hào)、移動(dòng)網(wǎng)站建設(shè)、軟件開發(fā)、Google
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源:
創(chuàng)新互聯(lián)