Synergy Codeworks

Tips, Tricks, and articles related to open source technology
Tags >> cloning database
Jan 11
2010

Cloning a MySQL Database from the Command Line

Posted by John Hobart in cloning database

Sometimes it's handy to make a quick copy of a database locally or on a remote machine from the command line.  This is a lot simpler and more reliable than exporting it from phpMyAdmin and then importing the data into a new database, though it does require command line access.

For a simple case where you're executing the command from an SSH session on the same machine on which your database resides and have the ability to run mysqldump and mysql:

mysqldump -uUSERNAME -pPASSWORD --no-create-db DB_NAME | mysql -uCLONE_USERNAME -pCLONE_PASSWORD CLONE_DB_NAME

There's a lot more information available on the MySQL website, but it's worth mentioning that you can use the -h option to run this remotely or make a copy of a remote database.

Keep in mind this might not be a suitable solution for your environment.  For databases with high transaction volumes your cloned copy might have some data left in an inconsistent state.  Your only option at that point would be to take the database down for maintenance during the export portion of the clone (which might not be acceptable) or setup replication.

Check out the blog post about Using lftp to Recursively Clone Your Website File Tree, it beats doing it the old fashioned way!

Hope this helps!

 

Client Login