Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> MySQL -> MySQL Tip - Fastest way to copy MySQL databases across hosts/servers

One of the fastest, easiest, most reliable ways to copy MySQL databases across different hosts/servers

There are many ways to copy MySQL databases across hosts. One of the easiest is to use a graphical tool such as SQLYog. However, if you want to copy from one remote server to another remote server, SQLYog imposes an annoying overhead that you could live without. The overhead involved is that SQLYog will first download data from one remote server to your local machine, and then send it from there to the other remote server.

A faster way to copy databases is to copy the MySQL database files directly from one server to another. There are at least two reasons why you might not want to use this approach. One reason is that views are often not replicated correctly in this manner. Another is that, as far as I know, you can't really do this with InnoDB based tables/databases because these are all stored within a single big file.

At any rate, the most reliable way to copy/recreate a database is to do a mysqldump that outputs the SQL statements needed to create the table. A quick, painless way to do this is to pipe the output of mysqldump directly to the mysql client which will send the commands directly to the server on the remote end. Remote hosting servers usually have very fast connectivity to the 'Net, and you completely avoid the overhead of having to go through your own ISP. Outlined below are the steps on how to do this:

1. Login to the host where the MySQL database to be copied resides.

2. From there, create the database to copy to on the other host. -u user refers to a user on the destination server with the proper permissions to create a database, -p means ask for the password for that user.

mysqladmin -h 'destination_host' -u user -p create dest_db

3. After the database is present on the destination server, it is a simple matter to just pipe the output of mysqldump over to it via the mysql client, without the need to create a temporary text file.

mysqldump -u user --password=XXXX --opt source_db | mysql -h 'destination_host' -u user -p dest_db

4. To apply compression and make the copying even faster, you can try the method below

mysqldump -u user --password=XXXX --opt --compress source_db | mysql -h 'destination_host' -u user -p dest_db

This tip can actually be found in the MySQL reference documentation itself, but it is cool enough to be worthy of a mention here. See http://dev.mysql.com/doc/refman/5.1/en/copying-databases.html for the original source which also shows the quickest way to dump a MySQL database to a gzipped file:

mysqldump --quick db_name | gzip > db_name.gz

Alternative tip: For very large databases, you may want to dump to a .gz file - as shown above - and do a file transfer of the dumpfile rather than directly piping the output of mysqldump into mysql across the network. When you do it this way, you can avoid having to take the extra step of decompressing the dumpfile by doing the below:

gunzip -c dbname.sql.gz | mysql -D[database name] -h[database host] -u[username] -p[password]
 Want to tip via bitcoin?

(address string below for double checking)
 1KDgw4y5AUSUhmRVQSWiVS43YvrrxLmc8u

© 2017 by Andy Sy
last updated: 2010-Jul-12


Web Development / Rich Internet Applications (RIA) Development

Programming Languages

Platforms

Database Development


SEO Consultants, Offshore SEO/SEM Philippines
   © 2003-2015 Neotitans Technologies Inc.