Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

Access Your MySQL Server Remotely Over SSH

So you've got MySQL on your web server, but it's only opened to local ports by default for security reasons. If you want to access your database from a client tool like the MySQL Query Browser, normally you'd have to open up access from your local IP address… but that's not nearly as secure.

So instead, we'll just use port-forwarding through an SSH tunnel, so your MySQL client thinks it's connecting to your localhost machine, but it's really connecting to the other server through the tunnel.

If you are using the command line ssh, the command would look like this. (You can do the same thing graphically in Putty or SecureCRT options if you need to)

ssh -L 3306:localhost:3306 geek@webserver.com

The syntax is ssh -L <localport>hostname<remoteport> <username>@<servername>. We're using localhost as the hostname because we are directly accessing the remote mysql server through ssh. You could also use this technique to port-forward through one ssh server to another server.

If you already have mysql running on your local machine then you can use a different local port for the port-forwarding, and just set your client tools to access MySQL on a different port.

image

Once you've got the ssh tunnel going, you can open up MySQL Query Browser and enter in the details for your remote server, using localhost as the server host, and adjust the port to whatever you used.

Once you get used to this method, you'll wonder why you ever used phpmyadmin or the command line version.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 06/29/07 and tagged with: Ubuntu

Comments (1)

  1. sazwqa

    Just remember if you already have MySQL installed on client machine then u need to change the localport to something else, since mysql will be using 3306, like:

    ssh -L 7777:localhost:3306 geek@webserver.com


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.