The other day I needed an SSH tunnel fast and knew that I was likely to reuse this tunnel several times throughout the project, and Meerkat came to the rescue.
About a month ago we began work on a project that involved a legacy database. The site had eight languages, including the South Caucasian language of Georgia, the Turkic language Azerbaijani spoken in Azerbaijan, as well as Russian and a few other languages. Not far into the project we noticed a familiar foe – mojibake, the phenomenon when malformed characters appear when the computer or application fails to render the characters due to encoding issues. Mojibake often occurs when you export or import a database that contains characters with character sets that the server or database client does not support, and given the many languages within the database, this wasn’t terribly surprising.
So what does this have to do with SSH tunnels? Well we had access to the web server, and through the web server we had access to the database server. However, the MySQL client on the web server was not built to handle the required character sets. This meant that whenever we made a database dump using the MySQL client on the web server, it would get “mojibaked.” The web server was the only host that had access to the MySQL server and database. This is where the SSH tunnel came in handy. What we needed to do was setup a tunnel to access MySQL on the MySQL server using a MySQL client on our local development machines and development servers, but still access the MySQL server as though we were using the IP of the web server. To do this we setup a tunnel that goes through the web server and the MySQL server, but through our local machines.
We needed a tunnel via the command line to connect to the MySQL server through a local MySQL client, and tunnel through the remote web server. In this case, the command may look something like:
ssh -L3307:127.0.0.1:3306 -p 22 -N -t -x firstname.lastname@example.org
In terms of commands you run on a daily basis, the SSH tunnel command has a fair number of arguments and is admittedly a pain to write. Sure, you could grep your .bash_history to re-run the command whenever you need it between logouts, if the command is still in your history, but that’s not much fun. However, if you’re running Max OS X, Meerkat, an application created by fellow Drupal developer Justin Miller, offers an elegant solution to manage all your SSH tunnels. With features like enabling the tunnel on startup, toggling on and off tunnels, and saving all tunnels with a unique name to use or edit later, Meerkat is a handy tool and its user interface makes setting up a tunnel and managing SSH tunnels a very easy task.
You may wonder why didn’t we just upgrade the MySQL client on the web server, or grant our IP access to the MySQL server/database directly. It’s not always convenient to do either of these based on what else is running on a given server and how their server clusters are setup. In this case, setting up the tunnel was the best option, and Meerkat made it wicked easy.