Quickly deploy a mongoDB 3-members replica set with vagrant

If you quickly need to test a 3-member mongoDB replica set, this post may be for you.

Using Vagrant and VirtualBox, you can quickly deploy 3 linux servers running mongoDB, automatically configured as a replica set with 1 primary and 2 secondaries. The configuration below is to be used for quick test/dev purposes, not production.


Download and install VirtualBox and Vagrant on your computer.

Setup vagrant with initial box image

(Optional if you’re using the Vagrantfile in below tarball)
Open up a terminal and type:
[sourcecode language=’bash’]vagrant init precise64 http://files.vagrantup.com/precise64.box[/sourcecode]

Vagrant config files

Virtualbox (default)

Download this tarball in a place of your liking. Untar it. You can edit them as you see fit, but they should get you started.

Mac OSX / Parallels

Thanks to the vagrant-parallels project, you can install the plugin to run vagrant with parallels.

Run the following commands, and download this Vagrantfile:

vagrant plugin install vagrant-parallels
vagrant box add --provider=parallels precise64 http://download.parallels.com/desktop/vagrant/precise64.box

You can then up your VMs by appending to the vagrant up commands:


For more info, see the parallels-plugin website.


Go to the newly created directory, and power everything up from the directory where your Vagrantfile resides.

vagrant up

It is important that “mongo1” gets deployed last, as the provisioning for the replica is done on that node, which will become the master. This is automatic based on the file you just downloaded.

It possible that you get this warning message when the VMs come up:

 The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
prevent things such as shared folders from working properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.3

This should not matter. It doesn’t for these specific versions, and for the purpose of this article.

In a matter of minutes, your mongoDB replica set will be ready.


The 3 servers are bound to the following IP addresses (VirtualBox / Parallels)

  • mongo1 : /
  • mongo2 : /
  • mongo3 : /
  • You should end up with the following mongoDB configuration, out of mongo1:

    set0:PRIMARY> rs.conf()
    	"_id" : "set0",
    	"version" : 3,
    	"members" : [
    			"_id" : 0,
    			"host" : ""
    			"_id" : 1,
    			"host" : "mongo3:27017"
    			"_id" : 2,
    			"host" : "mongo2:27017"

    Testing with data

    Insert a line for testing, on the primary node:

    set0:PRIMARY> db.something.insert( {test : true} )
    set0:PRIMARY> db.something.find();
    { "_id" : ObjectId("53133ee70a67e2fcfad30e41"), "test" : true }

    Now, logon to one of the secondary, and query for that data:

    set0:SECONDARY> db.something.find();
    error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

    This is normal. You have to tell Mongo to allow reads on the secondaries.

    set0:SECONDARY> rs.slaveOk()
    set0:SECONDARY> db.something.find();
    { "_id" : ObjectId("53133ee70a67e2fcfad30e41"), "test" : true }