Tunnel MySQL through SSH

Recently I had the challenge, that the MySQL database of one of the Magento shops I maintain wasn’t accessible directly through the Net. The access was restricted to the Webserver only. So I had to tunnel MySQL through SSH. And that worked like that:

  • edit the ssh config (~/.ssh/config) on the remote Webserver
    Host *
      ForwardAgent yes
  • create  a port mapping through ssh (the local port 3307 is mapped to the MySQL standard port of the webserver)
    $> ssh -L 3307:localhost:3306 user@server.com
  • Add your key to the server to gain passwordless access to the Webserver
  • Now you can use the remote MySQL database in you local Magento installation as if it would be located on the local environment.
    <host><![CDATA[127.0.0.1]]></host>
    <port><![CDATA[3307]]></port>
    <username><![CDATA[username]]></username>
    <password><![CDATA[password]]></password>
    <dbname><![CDATA[database]]></dbname>
    <!-- ... -->

    Naturally you can use this port mapping als with other MySQL driven applications or clients.

That’s it. This was how I tunnel MySQL through SSH if required.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.