pt-archiver数据导入迁移工具

pt-archiver数据导入迁移工具

  一直想明白,如何将一个大表的数据,每多少行数据已提交,分批次的转储到另外的地方,幸好有现成的工具,赶紧把实验成功的操作记录下来。

原理就不解释了,直接上最常用的命令。

数据导入导出工具pt-archiver
工具可以将MySQL的表数据导出到一个新表或者一个文件,也有自己的应用场景,比如数据归档,删除数据,数据合并等。
具体用法:
pt-archiver [OPTIONS] --source DSN --where WHERE

PT工具链接参数DSN一般适用于所有toolkit工具。
DSN的详细参数:
a:查询
A:字符集
b:true代表禁用binlog
D:数据库
u:数据库链接账号
p:数据库链接密码
h:主机IP
F:配置文件位置
i:是否使用某索引
m:插件模块
P:端口号
S:socket文件
t:表

具体使用,从一张表导入到另外一张表,要注意的是新表必须是已经建立好的一样的表结构,不会自动创建表,而且where条件是必须指定的:

环境:
源表: t_user  
目标表: t_user_tg


pt-archiver --source h=192.168.65.128,P=3306,D=db222,t=t_user,u=root,p=rootpwd --dest h=192.168.65.128,P=3306,D=db223,t=t_user_tg,u=root,p=rootpwd --no-check-charset  --share-lock --sleep=1 --where 'id>=0'  --no-delete --progress 1000 --limit 1000 --txn-size 1000 --statistics --file '/tmp/arch_%Y-%m-%d-%D.%t.log'
[root@my3-224 ~]# pt-archiver --source h=192.168.65.128,P=3306,D=db222,t=t_user,u=root,p=rootpwd --dest h=192.168.65.128,P=3306,D=db223,t=t_user_tg,u=root,p=rootpwd --no-check-charset   --sleep=1 --where 'id>=0'  --no-delete --progress 1000 --limit 1000 --txn-size 1000 --statistics --file '/tmp/arch_%Y-%m-%d-%D.%t.log'

...
...
2018-05-24T05:55:43   21773 12967000
2018-05-24T05:55:45   21775 12968000
2018-05-24T05:55:46   21777 12969000
2018-05-24T05:55:48   21778 12970000
2018-05-24T05:55:49   21779 12970603
Started at 2018-05-23T23:52:49, ended at 2018-05-24T05:55:50
Source: D=db222,P=3306,h=192.168.65.128,p=...,t=t_user,u=root
Dest:   D=db223,P=3306,h=192.168.65.128,p=...,t=t_user_tg,u=root
SELECT 12970603
INSERT 12970603
DELETE 0
Action          Count       Time        Pct
sleep           12971 12986.5408      59.62
inserting    12970603  6889.6769      31.63
commit          25942   570.4564       2.62
print_file   12970603   129.9266       0.60
select          12972    97.4990       0.45
other               0  1106.9789       5.08

从中可以看出来,休眠12986秒,inserting 6889.6769秒.

所有时间加起来:

12986.5408
6889.6769
570.4564
129.9266
97.499
1106.9789
21781.0786

大约每秒执行效率: 12970603/2181.0786 = 5946.8755504730552
个人测试环境虚拟机,可以看出来,效率还是挺高的。



原文地址:https://www.cnblogs.com/bjx2020/p/9077686.html