Remote Database Access – SSH Tunnel

It is possible to connect directly to a database on Servebolt from a remote server or your local computer, if you run the traffic through an SSH tunnel.

Our firewall and servers do not accept direct connections on port 3306, but you can get past this by initiating an SSH tunnel and to configure your application to connect to the database through this tunnel. Application configuration is different for every client, but both MySQL and SSH run on default ports.

An SSH tunnel needs the site username and password, and MySQL/MariaDB (the database) needs the database credentials.

Moving Databases over SSH with the Command Line

Please note that these examples use passwords on the command line, which may be considered unsafe. It is possible to get rid of password usage by making the database check a safe config file for instead, but that is not covered in this article.

Import local database file directly to remote database over SSH tunnel

If you have a database dump on your computer, you can use the following command to import it directly into a remote database over an SSH tunnel.

cat mysqldumpname.sql | ssh [email protected] mysql -u dbusername -p dbdbname --password='mydbpassword'

You will be prompted for the site ssh password if you are not using keys, and then you’ll just have to wait until it finishes.

Dump local database and import it directly to remote database over SSH tunnel

If you want to dump a local database, and import it directly to the remote database over SSH you need the mysqldump command on your local computer (usually available if you have mysql installed).

mysqldump -u localdbuser -p localdbname --password='localdbpassword'  | ssh [email protected] mysql -u dbusername -p dbdbname --password='mydbpassword'

Just input the SSH password for the site if you are not using keys when prompted, and wait for the transfer to finish!