How To: remote mySQL access, via SSH port forwarding
(NOTE: I’ve only tested this on Linux and OS X, Windows users running Putty, I’m not sure of the success rate or if it even works, give it a shot and drop it in the comments)
I’ve been doing web development for damn near a half decade now. Although I can navigate mySQL via command line over an SSH connection I’m a big GUI fan when it comes to building tables, configuring indexes and backing up catalogs or importing/creating dumps. With that said I know 24234 of you just said “Dude, PHPmyAdmin Newb!”… and now my turn… I hate PHPmyAdmin, you have a better chance of seeing me rooting around TuSSH on my Treo updating a schema than you do to see me using PHPmyAdmin… no seriously, I’m not joking I hate it that much.
Now with that bit of foreshadowing in place I will continue the article. I have a re-occuring problem, often times (smart) hosting companies lock down remote mySQL connections. Sometimes they won’t allow them at all, sometimes you have to call and manually give them an your IP address after proving who you are… usually to like 13 people. Anyway it’s a hassle to get the remote mySQL access opened up and reliably working in most cases, where as getting SSH only requires a few emails, a credit card and drivers license (if your hosting company is good). In my constant quest to update projects I have been locked out many a times, thus having to revert to my primal Command line instinct. Well… NO MORE MY FRIENDS!
Step 1: Open Up a Terminal Window
Step 2: Type in this command
sudo ssh -L 7777:127.0.0.1:3306 username@remoteserver.com
sudo or su is required to forward local ports
Whats Going On Here?
Lets dissect our request
sudo
requesting super user access in Unix (required for local port forwarding)
ssh
creating an encrypted secure connection between your machine and your server
-L 7777:127.0.0.1:3306
-L forward a local port
7777: the local port
127.0.0.1: your local machine
3306 what port on the server it should point to
username@remoteserver.com
login using this username at this server address
What did we just do?
We’ve created a secure tunnel between your local machine and your server. Meaning if I go to 127.0.0.1:7777 I’m really going to end up at remoteserver.com:3306. Meaning now if you have a mySQL GUI tool of your choice (mySQL Query Browser, mySQL Admin, Navicat, Etc.) and type in:
Host: 127.0.0.1
Port: 7777
User: yourusername
Pass: yourpassword
You will be connected to your servers mySQL schema.
