溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL使用pt-archiver歸檔歷史數(shù)據(jù)

發(fā)布時(shí)間:2020-08-10 08:23:36 來源:ITPUB博客 閱讀:214 作者:feelpurple 欄目:MySQL數(shù)據(jù)庫
pt-archiver可以將表按照指定條件歸檔到歷史數(shù)據(jù)庫中,也支持文件導(dǎo)出,對(duì)于歸檔清理線上歷史數(shù)據(jù)非常方便。
如果要?dú)w檔表的數(shù)據(jù)到歷史數(shù)據(jù)庫的表中,需要預(yù)先在歷史數(shù)據(jù)庫中創(chuàng)建表結(jié)構(gòu)。

(1) 按照條件歸檔表中的歷史數(shù)據(jù)到歷史數(shù)據(jù)中,同時(shí)在本地生成歸檔文件

  1. # 在歷史數(shù)據(jù)庫(192.168.56.102)中創(chuàng)建歸檔表
  2. mysql> CREATE TABLE `emp` (
  3.     -> `id` int(11) NOT NULL,
  4.     -> `name` varchar(15) DEFAULT NULL,
  5.     -> PRIMARY KEY (`id`)
  6.     -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  7. Query OK, 0 rows affected (0.60 sec)

  8. # 查看源表
  9. mysql> select * from emp;
  10. +--------+---------+
  11. | id | name |
  12. +--------+---------+
  13. | 10 | Neo |
  14. | 10036 | test |
  15. | 10037 | test |
  16. | 10038 | test |
  17. | 10039 | test |
  18. | 10040 | MySQL01 |
  19. | 10041 | MySQL01 |
  20. | 10042 | MySQL01 |
  21. | 100100 | test |
  22. | 100101 | test |
  23. | 100103 | test |
  24. | 100104 | test |
  25. | 100105 | test |
  26. | 100106 | test |
  27. | 100107 | test |
  28. | 100108 | test |
  29. +--------+---------+

  30. # 歸檔id小于200000的數(shù)據(jù)到歷史庫和本地文件
  31. # 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'

  32. # 查看源表
  33. mysql> select * from emp where id < 200000;
  34. Empty set (0.05 sec)

  35. # 查看歷史表
  36. mysql> select * from emp limit 2;
  37. +--------+------+
  38. | id | name |
  39. +--------+------+
  40. | 100100 | test |
  41. | 100101 | test |
  42. +--------+------+
  43. 2 rows in set (0.00 sec)

  44. # 查看本地歸檔文件(相當(dāng)于select ... into導(dǎo)出)
  45. # cat /opt/2018-03-19-sale.emp
  46. 10    Neo
  47. 10036    test
  48. 10037    test
  49. 10038    test
  50. 10039    test
  51. 10040    MySQL01
  52. 10041    MySQL01
  53. 10042    MySQL01

(2) 清理過期歷史數(shù)據(jù)

  1. # 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

  2. # 查看清理后的表中數(shù)據(jù)
  3. mysql> select * from item_order where order_date < '2018-03-01';
  4. Empty set (0.00 sec)


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI