mysql数据库传输数据,如何在2个MySQL数据库之间传输数据?

I want to do that using a code and not using a tool like "MySQL Migration Toolkit". The easiest way I know is to open a connection (using MySQL connectors) to DB1 and read its data. Open connection to DB2 and write the data to it. Is there a better/easiest way ?

解决方案

First I'm going to assume you aren't in a position to just copy the data/ directory, because if you are then using your existing snapshot/backup/restore will probably suffice (and test your backup/restore procedures into the bargain).

In which case, if the two tables have the same structure generally the quickest, and ironically the easiest approach will be to use SELECT...INTO OUTFILE... on one end, and LOAD DATA INFILE... on the other.

For trivial tables the following will work:

SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv'

FIELDS TERMINATED BY ','

OPTIONALLY ENCLOSED BY '"'

ESCAPED BY '\\\\'

LINES TERMINATED BY '\\n' ;

LOAD DATA INFILE '/tmp/mytable.csv' INTO TABLE mytable

FIELDS TERMINATED BY ','

OPTIONALLY ENCLOSED BY '"'

ESCAPED BY '\\\\'

LINES TERMINATED BY '\\n' ;

We have also used FIFO's to great effect to avoid the overhead of actually writing to disk, or if we do need to write to disk for some reason, to pipe it through gzip.

ie.

mkfifo /tmp/myfifo

gzip -c /tmp/myfifo > /tmp/mytable.csv.gz &

... SEL

ECT... INTO OUTFILE '/tmp/myfifo' .....

wait

gunzip -c /tmp/mytable.csv.gz > /tmp/myfifo &

... LOAD DATA INFILE /tmp/myfifo .....

wait

Basically, one you direct the table data to a FIFO you can compress it, munge it, or tunnel it across a network to your hearts content.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值