This guide will show you one of the easiest methods to securely access a remote MySQL database.
These days there are a number of really cool MySQL front-end tools available for all platforms. Some are web based and can be installed on the MySQL server, while there are others which need to be installed on your local laptop or desktop computer. If you have a MySQL server running somewhere outside your local network you will most likely need to access it over the Internet to allow your super cool MySQL desktop client talk to it. You might be tempted to allow the MySQL server to run openly over the Internet to allow you access. That would be an extremely bad idea. Here’s a much better solution to allow you access without compromising much on security.
Let’s see how we can do this using an SSH tunnel based connection to your MySQL server. There are two steps to do this. The first is to create an SSH tunnel connection from your computer to your MySQL server while piping the data to and from the MySQL server though the SSH tunnel. The next step is to make a MySQL connection to a local port and gain access to your remote MySQL server. The following command will create an ssh tunnel from remoteserver.net to your local computer. It will forward the incoming and outgoing traffic to that computer’s port 3306 to your local computer’s port 6666.
ssh user@remoteserver.net -L 6666:localhost:3306
Modify the command shown above, replacing the user@remoteserver.net part with the credentials of your remote server, and -L 6666 with the port on which you want to access MySQL on your local machine. Once you have run this command you will be asked to enter the password for the user you used to log in as on the remote server. After you successfully enter the password your ssh tunnel should be up and running.
Now launch your favorite MySQL client and make a connection with you remote MySQL server using the ssh tunnel you just created. Assuming you setup the tunnel on port 6666 you will need to use the following credentials to connect.
host: 127.0.0.1
port: 6666
You will need to use the database authentication that you use with your remote server. If you did everything right you should now be able to see your remote databases in your MySQL client. You are now on a secure remote connection. Remember to close the ssh tunnel when you are done with using MySQL by quitting the ssh session you setup initially.