coderholic

Copy MySQL database between servers

I've recently needed to move some databases from one server to another. I found the simplest way to do this was to use mysqldump. The small shell script below copies a given database from a remote host to the local host. Just fill in the variables and run it to copy the whole database:

#!/bin/bash
# Copy a remote database to a local one

REMOTE_HOST=''
REMOTE_USER=''
REMOTE_PWD=''
REMOTE_DB=''

LOCAL_USER=''
LOCAL_PWD=''
LOCAL_DB=''

mysqldump -h$REMOTE_HOST -u$REMOTE_USER -p$REMOTE_PWD --opt --compress $REMOTE_DB | mysql -u$LOCAL_USER -p$LOCAL_PWD $LOCAL_DB
Posted on 01 May 2008
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.