photo credit: Grenwich Foot Tunnel via photopin (license)

How to connect to MySQL in Vagrant from your host

Tags: ,

Recently I got the following question from Mostafa Hashem:


First I need to thank you for sharing your experience with us.

I need to ask you about something related to Vagrant & MySQL service.

I need to connect to MySQL databases outside SSH for example fromSqlYog. 

But to be honest with you I spent a lot of time on it and I don’t have a good experience on server side.

Could you help me in that , I will be appreciated.

Best regards,

Mostafa Hashem

Back-End  Developer

Well Mostafa, the answer is actually pretty easy…

Connect to MySQL in Vagrant

When you instantiate a Vagrant box, Vagrant automatically forwards port 2222 to your guest machines’ 22. This is also how you can connect to your Vagrant Boxes SSH client when you type vagrant ssh  in your terminal. The same port can be set with most MySQL GUI’s when entering the credentials. So when you’re connecting to MySQL, you have to:

  • Enter as the host
  • Enter your MySQL username and password
  • Enable the SSH-tunnel
  • Enter the default vagrant username and password (this is vagrant and vagrant).

Example for Sequel Pro

Screen Shot 2015-04-07 at 20.27.31

Other software

Unfortunately, I can’t show an example for SqlYog (or any other Windows program) since I work on OSX. But in my experience, all MySQL Management programs have the option to enable SSH and set the port that is used for that. So you just have to look for that and it will work!

More advanced tunneling

If you’re looking for more advanced SSH tunneling in Vagrant, you might be interested in this article I wrote a while ago. It describes how to setup an SSH tunnel from the command line so you can access MySQL on a different port.

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

How would you rate this article?

6 thoughts on “How to connect to MySQL in Vagrant from your host”

  1. Enabling and setting up SSH option on MySQL management programs was a great tip. Thanks. I do not have to install PHPMyAdmin within my Vagrant Virtual Machines and connect to it for quick work from UI.

    1. Giel Berkers says:

      That’s right! Since I connect to my databases with SSH tunneling I never used PhpMyAdmin again. Whenever I see PhpMyAdmin now my neckhair rises. 😉

  2. Mostafa Hashim says:

    Thanks Giel

    but when i tried to connect i got this massage

    access denied for user ‘root’@’localhost’ using password no

    i’m logged in vagrant with the default details (vagrant as username ) and using the private key file

    1. Giel Berkers says:

      That’s a message coming from your MySQL server, so the SSH port is working. Are you using the correct MySQL credentials? What happens when you go into the Vagrant shell (by using vagrant ssh) and try to connect to mysql? (mysql -u root -p(password))

  3. you are right , this because of Mysql credentials was wrong

    it works now 🙂 , and it’s become easy now

    Thanks Giel

    1. Giel Berkers says:

      You’re welcome!

Leave a Reply