Import Remote Database Directly over SSH

Developing, maintaining and debugging web-applications often involves copying a remote database to a local or another remote machine. This post lists a number of methods that I find useful. It acts as a reference for myself which I happily update and improve based on comments and experience. My favourite method:  stream the database dump directly to the target machine.

I assume a MySQL or MariaDB database on a Unix or Mac OS machine but by adjusting the appropriate commands most of these methods apply to other databases as well.

When using SSH and MySQL on a regular basis you could take a look at the following posts to simplify your work/life:

Export to a file, copy with rsync, import into MySQL

At the source machine, dump the relevant database to a file.

If the default username and password are supplied in .my.cnf, the -u and -p options can be omitted. It is assumed that default credentials are supplied. Otherwise, just provide the -u and -p arguments to the mysql and mysqldump command.

Now, connect to the target machine. If the target database does not exist, create it:

Download the dump and import it to the target database:

Rsync synchronises files over an SSH connection. The -z option enables compression on the connection. If you don’t use key-based authentication, provide the -p option to rsync to supply a password.

Stream a database directly from source to target machine.

This method is faster and more elegant but somewhat harder to remember. The output of the mysqldump is streamed over ssh and piped to the target mysql server.

Assuming default credentials have been provided in .my.cnf, otherwise, provide the -u (user), -p (password) and -h (host) options to the mysql command.

If the target database does not exist, create it:

Now, stream the remote database dump directly into the target mysql server.

The -C option enables compression.

Using pipe viewer to display progress

Additionally, pipe the dump through the pv (pipe-view) utility to show the dump progress. Install pipeview if it is not available on your system.

Mac OS (Homebrew): brew install pv
Debian/Ubuntu: apt-get install pv

Piping through GZIP

Applying explicit gzip compression to the mysqldump might improve performance:

Sponsored content