Prev/Next links

The remote (mysql) database accessed locally

Recently I've left a very nice web Development company, located near my home town. I love the company even though they're using ASP.net and all those closed source Microsoft tools I so sincerely like! Don't get me wrong, most off my live I've been working with open source ... but development always has come easily with the tools Microsoft provides.

It's a breeze to work with VCS, DBMS and integrate them with their well written tools.

Having left the company I could revert to torrent those nice Microsoft tools, download their express editions or do what I do best, create a working/development environment with the tools I've at hand. I currently develop with languages such as perl, php, javascript, css, html, sql and some python, bash, java as well. I don't know how it's with you guys, but in general I dislike the way I've to Query and Testdrive my databases.

MySQL developed some nice tools which make Querying you database feel comfortable, in order to use their software you've to be able to connect to you development/test/live databases through a tcp/ip connection. Mostly this means you've to be connected to the network where those machines can be found. Especially for those live databases this proves to be a nuisance. Most developers have no controll over those live database machines and are stuck with a console. (I love bash, so no complaints here ;))

I'm a fairly experienced bash user (perhaps even a power user ;)) and am able to perform most administration and query tasks by hand using a the mysql console client. But with age comes wisdom they say, so having experienced the nice way Microsoft products integrate I felt obligated to make my life more comfortable and to try to get a nice GUI working with my live MySQL Database.

I'm using the following software

  • MySQL Administrator/MySQL Query Browser (or perhaps even MySQL Workbench)
  • xterm/bash/
  • ssh

Yes, you could use the MySQL Workbench, Sequel Pro or simply stick to your console based client. Or use Putty and make it work with a Windows machine. Right now my Windows laptop is connected to my development machine and my development machine connects to my live server. This way I can run all those pesky Queries using a second screen (the laptop) while development is happening using Ubuntu and a widescreen.

Let's get down to business, in order to connect our (local) MySQL Client to our (remote) MySQL Server without compromising server security. We'll have to setup a secure port forward. We wouldn't want to open up any other ports to the internet except the ones we really really need such as http(s). Nowadays I use a rather simple Gnome Tool, aptly named gTSM (Gnome SSH Tunnel Manager) to manage my SSH Tunnels, previously I used any shell combined with the openssh ssh-client. The command should be something like: ssh -v -L 4242:127.0.0.1:3306 {databaseserver} It simply means,, forward our local (-L) port 4242 to the port 3306 on listening on the localhost interface after connecting to {databaseserver}. We can automate the SSH Tunnel to start when we're login onto a machine, we could also use public/private key authentication in order to simplify the login process (with other words, no need to enter the password in order to login).

Keep in mind, you should be connecting to your MySQL Server using the tcp/ip protocol (when you've no clue where I'm talking about always use 127.0.0.1 for the hostname to connect to or you could end up like this guy)

You could also do this the other way around, forward a remote port to a local port using a SSH Tunnel. The advantage is you don't have to upload your public key, the hassle is you'll have to configure database security settings.

We can do this trick using any OS. With Windows you could use putty, it's really easy to setup, using OSX we would rather use a tool such as Sequel Pro. When you like to delve more deeply into SSH Tunnels, read the following Symantec Article.

Keep in mind, this is a developers solution to solve a development problem. It's meant to keep things simple and productive. Don't forward your live database continuously towards your development environment. It would be a security risk, not to mention the fact you could be forgetting you're using the live database and think you're dropping the development database. It's main use is during development. For most simple system administration tasks I revert back to use a console based mysql client, running within the live server confiments.