Snippet: Copy MySQL Databases Over SSH

I needed to copy a database and the idea of backing it up just to re-import1 seemed like double the work. Here’s a snippet to pipe a mysqldump into a remote database. Keep an eye on the user names and passwords – you’ll need 3 sets; one for the database your copying, one to get into your remote server, and one for the remote, target database.


1. Backing up is a good thing. Why aren’t you doing it? Here’s a script for that.

4 Replies to “Snippet: Copy MySQL Databases Over SSH”

  1. I’m trying to the reverse of this command but the remote server is not registering the password.

    mysql -u USER -pPASSWORD DATABASE_NAME | ssh USER@REMOTE_HOSTNAME mysqldump -v -u USER -pPASSWORD -h HOST --opt --compress DATABASE_NAME

    I am prompted for my ssh password. After entering it and pressing return, nothing happens. If I hit return again, it tells my my password is incorrect, when I know it’s not.

    If I break the command up it works as expected.

    ssh USER@ REMOTE_HOSTNAME mysqldump -v -u USER -pPASSWORD -h HOST --opt --compress DATABASE_NAME > db.sql

    Any idea’s what’s causing this hang up?


  2. It may be a bit late but you could try:

    ssh REMOTE_USER@host mysqldump -uREMOTE_DB_USER -pPASS OLD_DB | mysql -uDB_USER -pPASS NEW_DB

    This way your are feeding the remote DB dump into the local mysql command.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.