/ Code

mysql - copy database

There are a handful of tasks that I do infrequently enough that I don't reliably recall the process every time. Normally I'd create an alias or script but some processes don't lend themselves to that, or, I just don't do it often enough to justify it.

Copying a database from one host to another is such a task. It's simple but for some reason I always think it should be more complicated than it really is:

On the machine you want to replicate the database to:
$ mysql -e "CREATE IF NOT EXISTS dbname" $ ssh remotehost mysqldump dbname | mysql dbname

Or, if you are on the machine that has the database:
$ ssh remotehost mysql -e '"CREATE IF NOT EXISTS dbname"' # note extra quotes $ mysqldump dbname | ssh remotehost mysql dbname

If bandwidth is a concern use ssh -C.