Provisioning MySQL server in a Vagrant environment with Chef

Reading time ~3 minutes


in this article I will show you how can you provision a MySQL server with Chef in a Vagrant environment. The Vagrant environment will be an Ubuntu virtual machine. I will do this tutorial in a OSX machine. If you want to make it in a Windows environment, the commands could be slightly different.

First of all you will need to install Vagrant, Chef and VirtualBox. Please, refer to their web sites to know how to do it.

The first step is create a directory for our environment. Let's call it UbuntuMySQL. We will make two directories inside it: environment and share.

mkdir -p UbuntuMySQL/{environment/,share/}

Initialize a git repository inside your new folder.

cd UbuntuMySQL
git init

Go to your environment folder and initialize Vagrant

cd environment
vagrant init

Now you have a file called Vagrantfile whit sample data. Take a look at it to imagine how many things you can configure. Erase all the content and copy these lines.

[caption id="attachment_170" align="alignnone" width="1024"]First Vagrantfile First Vagrantfile[/caption]

(The url of config.vm.box_url is

In this lines we are telling Vagrant that we want him to download this virtual maxine, create a private network and that we want to sync our ../share folder with the /share folder in the virtual machine.

Now we are ready to start our virtual machine. Just write vagrant up in your shell. If it's the first time you make this, have a book near you because vagrant will download the virtual machine and, depending on your broadband connection, could be time consuming.

The result of the operation is something similar to this.

[caption id="attachment_171" align="alignnone" width="1024"]virtual machine up virtual machine up[/caption]

Now, you could write vagrant ssh and access your virtual machine. As it's configured in the Vagrantfile file, the user used to connect will be "vagrant".

[caption id="attachment_172" align="alignnone" width="1024"]vagrant ssh vagrant ssh[/caption]

Ok, we have a brand new Ubuntu virtual machine but we want it to install a MySQL server. Do I have to do it manually? The answer is NO, you could use chef to do the job for you.

Chef uses cookbooks to know what to do to install applications (or whatever you want to do with Chef). So, seems logic that we need to have a MySQL cookbook to be able to install MySQL. Do I have to write it? Maybe, but there are loads of cookbooks already written. You could find them at Let's download the MySQL cookbook to our repository. To do that write this command in your shell in the virtual machine root folder:

git submodule add environment/cookbooks/mysql

Doing this, we will clone the repository in environment/cookbooks/mysql folder.

If you take a look at recipes folder inside that folder, you could see more than one recipe:

[caption id="attachment_173" align="alignnone" width="1024"]MySQL recipes MySQL recipes[/caption]

The one we want to install is server.rb.

Let's edit our Vagrantfile to tell Vagrant to install MySQL using Chef.


Stop the virtual machine using "vagrant halt" and run one more time "vagrant up".

Ops! We have an error!

[caption id="attachment_175" align="alignnone" width="1024"]Error no Chef Error no Chef[/caption]

It seems that Chef isn't installed in the virtual machine. To do that we can use Vagrant Omnibus to help us. Install it and write in your shell

vagrant plugin install vagrant-omnibus

Now, we can change our Vagrantfile to install chef in the virtual machine.

[caption id="attachment_176" align="alignnone" width="780"]Install chef Install chef[/caption]

Let's try to provision our virtual machine. Write this in your shell:

vagrant provision

Ops! Another error!

[caption id="attachment_177" align="alignnone" width="1024"]No yum-mysql-cummunity No yum-mysql-cummunity[/caption]

It seems that we need another cookbook, in this case yum-mysql-cummunity. Let's download it:

git submodule add environment/cookbooks/yum-mysql-community

Try to provision one more time and... oh no, another error!

[caption id="attachment_178" align="alignnone" width="1024"]No yum No yum[/caption]

We need to download another cookbook. Let's do it:

git submodule add environment/cookbooks/yum

And try to provision one more time. Oh, no errors! Looks promising!

To ensure MySQL is installed, enter in the virtual machine

vagrant ssh

and try to access to the mysql server:

[caption id="attachment_179" align="alignnone" width="1024"]MySQL installed MySQL installed[/caption]

Great! And now, let's try to connect to that server from our computer. Open MySQLWorkbench and connect to the server:

[caption id="attachment_180" align="alignnone" width="1024"]Connecting from host Connecting from host[/caption]

[caption id="attachment_181" align="alignnone" width="728"]Connected Connected[/caption]

That's great! We've installed a virtual machine (with no gui) in our machine, installed a MySQL server and connected to it. The greatest thing is that this steps are repeatable. We could upload this files to our Git server, and another workmate could pull them, execute vagrant up and have exactly the same environment we have.

Thanks for reading, see you soon!

Using different configuration per stage

In the [previous article](./deploy-serverless-app/) we saw how to create a basic deployment pipeline for a serverless application. In thi...… Continue reading

Deploying a serverless application

Published on April 19, 2018

Dynamic secrets with Vault

Published on December 16, 2017