·

photo credit: masteruser1999 via photopin cc

Create a (MySQL) SSH tunnel from within Vagrant

Tags: , ,

Let’s say you have the scenario where you need to connect to a remote (MySQL) database. If you have SSH access to the server you can simply create a SSH tunnel and forward the MySQL  port from the remote machine to a port on your local machine. For example:

But what if you need to do this in Vagrant? What if you got your development environment set up in Vagrant and you need to create the SSH tunnel in your Vagrant box? The private key in your Vagrant machine will most likely not match with the allowed keys on the remote machine. The answer to this is fairly easy.


The only thing you need to do is edit (or create) the file ~/.ssh/config  on your local machine (not in your Vagrant box!) to forward the private keys of your host machine to the Vagrant box:

Now you can use the exact same command in Vagrant to create your SSH tunnel:

Connect to the database

When you want to make the connection in your Vagrant box with the remote MySQL database, just connect to the host ‘localhost’ and use port 3307 to connect to. Your SSH agent will tunnel traffic through this port to the established SSH connection.

Magento example

To use this method in Magento, you need to add a <port> -tag to your app/code/local.xml  to set a part (this tag is not there by default):

Visitors give this article an average rating of 4.0 out of 5.

How would you rate this article?

One thought on “Create a (MySQL) SSH tunnel from within Vagrant”

  1. oliver says:

    thank you for the post. it is a good idea to set up the config file to forward ssh agent. I recently switched to vagrant vm and was looking for a gui to connect to the database in the virtual machine. It works on my setup of vagrant box on a mac ox host machine running sequel pro. thanks for sharing the information.

    Oliver

Leave a Reply