Let's using SSH port forwarding to tunnel a service from a remote server to our local machine. For example, many servers restrict mysql connections to localhost only. Using this technique we can tunnel our mysql connection through the remote machine transparently and securely. Let's set up our example. We have an instance of MySQL running at mysql.remoteserver.com on the default port of 3306. To make things interesting (and perhaps easier to follow), we're going to assume that you're also running an instance of MySQL on your local machine on the same port. So, let's tunnel the mysql connection from the remote server to our local machine on port 3307 so there are no conflicts.
If you're not using public key authentication, you will be prompted for a password. By specifying the -N option, we suppress opening a shell session and your command prompt will merely hang after you've entered your password. This can be safely omitted if you like, and a shell session will be opened for you. You can read more about the ssh command at it's man page.
To connect to our MySQL server, we need to specify our local address and port (Note: Using "localhost" will NOT work in this case, make sure to 127.0.0.1), and the login information for the instance of MySQL on our remote server. In Python you might use the following:
Tags: One Liners