Connect to a MySQL Server inside of a Vagrant Machine from the Host Machine

For all our projects we use Vagrant, we don’t waste more time setting up environments in physical machines since is a really huge waste of time. The only things that I have installed in my host machine are the IDE, Vagrant, Virtual Box and Browsers, that’s all I usually need.

Marcos did like two year ago a great Vagrant configuration file with a very curated selection of Cheff recipes to bootstrap our machines, over this time we have created several variations for Drupal, Symfony, Laravel and WordPress projects. I’ll write in another post about this.

In the Vagrantfile for our virtual machines we define two interfaces, one that is exposed directly to the primary network and a second one that assigns a specific IP address for that machine, that’s is for all us to know which is going to be the machine IP address no matter the current network configuration.

To do that we define the network as public :public_network

I’m telling you this because there is another Vagrant setup that hides the machine into a private network and in order to access to the machine services we need to declare ports forwarding from our local machine to the virtual machine, we don’t use that alternative.

Since all the server layer is into a virtual machine you need to connect to any service via TCP/IP. In this case I want to connect from my host machine to the MySQL server with a tool called SQL Power Architect that helps me to design the database.

So, with the machine available into the network is easy to connect to any service in the virtual machine. For MySQL however we need to setup the server to listen in any interfaces to allow external connections. Also we need to give to an user privileges to access from the network.

To listen in all interfaces, go to your my.cnf and set the bind-address directive to

bind-address =

Restart the mysql service

$sudo /etc/init.d/mysql restart

Then, access to MySQL with an administrative user and grant some privileges, this is like a free for all.

$mysql -u root -p
>GRANT ALL PRIVILEGES ON *.* TO [email protected]'%' IDENTIFIED BY 'yourpass';

The % means any ip, and *.* all databases all tables. To be more specific

>GRANT ALL PRIVILEGES ON 'drupal_db'.* TO 'drupal_user'@'' IDENTIFIED BY 'yourpass';

To give access from the ip to the user, drupal_user, using the pass, yourpass, and will access only to the drupal_db tables.

That’s it, you are now inside of your mysql server from your host machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

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