How can I transfer data between 2 MySQL databases?

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.

See and .../select.html for definitive details.

For trivial tables the following will work:

SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' 
  ESCAPED BY '\\\\' 

LOAD DATA INFILE '/tmp/mytable.csv' INTO TABLE mytable 
  ESCAPED BY '\\\\' 

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.


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

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

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

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.

The FEDERATED storage engine? Not the fastest one in the bunch, but for one time, incidental, or small amounts of data it'll do. That is assuming you're talking about 2 SERVERS. With 2 databases on one and the same server it'll simply be:

INSERT INTO databasename1.tablename SELECT * FROM databasename2.tablename;

You can use mysqldump and mysql (the command line client). These are command line tools and in the question you write you don't want to use them, but still using them (even by running them from your code) is the easiest way; mysqldump solves a lot of problems.

You can make selects from one database and insert to the other, which is pretty easy. But if you need also to transfer the database schema (create tables etc.), it gets little bit more complicated, which is the reason I recommend mysqldump. But lot of PHP-MySQL-admin tools also does this, so you can use them or look at their code.

Or maybe you can use MySQL replication.


As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another:

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

If you enabled binary logging on your current server (and have all the bin logs) you can setup replication for the second server

Need Your Help

Done key in iPhone app that closes keyboard (with UITextView)

iphone objective-c keyboard-layout

I'm working on an iPhone app and in the iPhone app the user is typing in an UITextView.

How do I add a new sourceset to Gradle?

java gradle build integration-testing source-sets

I want to add integration tests to my Gradle build (Version 1.0). They should run separately from my normal tests because they require a webapp to be deployed to localhost (they test that webapp). ...