Using Vagrant with Sitecore Part 2 – MongoDB on Linux build using Vagrant

This is part 2 of the series of post that I am doing about Vagrant to aid with Sitecore Development

Part 1 – Setup, Chocolatey, Vagrant, and VirtualBox
Part 2 – This post
Part 3 – Sitecore using Solr running on Ubuntu Linux


Normally I would have just installed MongoDB on my local machine, and be done with it. But as always looking for ways to improve, I wanted to run MongoDB on linux. Due to this knowledge base article: I am going to be using MongoDB 2.6. as I will be using a fresh copy of Sitecore 8.0 for final testing.

If you look at the documentation for installing MongoDB 2.6, the following steps are required:

  1. Import the public key used by the package management system.
  2. Create a list file for MongoDB.
  3. Reload local package database.
  4. Install the MongoDB packages
  5. Pin a specific version of MongoDB

I wanted to run all the command manually to make sure I get everything correct. And if not, then its easy enough to destroy the box and start again.

Now on to installing.

I am going to keep on using the vagrant linux instance that i built in Part 1

after ssh’ing into the VM, lets start the installation

Import the public key used by the package management system.

The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the MongoDB public GPG Key:

sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

This returns the following :

Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.ZkFf5tD5Bp --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp:// --recv 7F0CEB10
gpg: requesting key 7F0CEB10 from hkp server
gpg: key 7F0CEB10: public key "Richard Kreuter " imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)

Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb.list list file using the following command

echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

There is no output from running the command

Reload local package database

Issue the following command to reload the local package database

sudo apt-get -y update

This runs for a while, and you should get output similar to the following

Get:1 precise-security InRelease [55.7 kB]
Get:2 precise-security/main Sources [142 kB]
Get:3 precise-security/restricted Sources [4,476 B]
Get:4 precise-security/universe Sources [48.9 kB]
Get:5 precise-security/multiverse Sources [2,720 B]
Get:6 precise-security/main amd64 Packages [616 kB]
Ign dist InRelease
Ign precise InRelease
Get:7 precise-updates InRelease [55.7 kB]
Get:8 precise-security/restricted amd64 Packages [11.6 kB]
Get:9 precise-security/universe amd64 Packages [133 kB]
Get:10 precise-security/multiverse amd64 Packages [3,160 B]
Get:11 precise-security/main i386 Packages [677 kB]
Get:12 dist Release.gpg [490 B]
Get:13 precise-security/restricted i386 Packages [11.6 kB]
Get:14 precise-security/universe i386 Packages [141 kB]
Get:15 precise-backports InRelease [55.7 kB]
Get:16 precise-security/multiverse i386 Packages [3,340 B]
Get:17 precise-security/main TranslationIndex [208 B]
Get:18 precise-security/multiverse TranslationIndex [199 B]
Get:19 precise-security/restricted TranslationIndex [202 B]
Get:20 precise-security/universe TranslationIndex [205 B]
Get:21 precise-security/main Translation-en [256 kB]
Get:22 precise-security/multiverse Translation-en [1,698 B]
Get:23 precise-security/restricted Translation-en [2,976 B]
Get:24 precise-security/universe Translation-en [85.0 kB]
Hit precise Release.gpg
Get:25 dist Release [2,040 B]
Get:26 precise-updates/main Sources [497 kB]
Get:27 dist/10gen amd64 Packages [29.8 kB]
Get:28 dist/10gen i386 Packages [29.6 kB]
Get:29 precise-updates/restricted Sources [8,708 B]
Get:30 precise-updates/universe Sources [128 kB]
Ign dist/10gen TranslationIndex
Get:31 precise-updates/multiverse Sources [10.2 kB]
Get:32 precise-updates/main amd64 Packages [995 kB]
Ign dist/10gen Translation-en_US
Get:33 precise-updates/restricted amd64 Packages [16.2 kB]
Get:34 precise-updates/universe amd64 Packages [277 kB]
Ign dist/10gen Translation-en
Get:35 precise-updates/multiverse amd64 Packages [16.9 kB]
Get:36 precise-updates/main i386 Packages [1,063 kB]
Get:37 precise-updates/restricted i386 Packages [16.1 kB]
Get:38 precise-updates/universe i386 Packages [287 kB]
Get:39 precise-updates/multiverse i386 Packages [17.1 kB]
Get:40 precise-updates/main TranslationIndex [208 B]
Get:41 precise-updates/multiverse TranslationIndex [202 B]
Get:42 precise-updates/restricted TranslationIndex [202 B]
Get:43 precise-updates/universe TranslationIndex [205 B]
Hit precise Release
Get:44 precise-backports/main Sources [5,922 B]
Get:45 precise-backports/restricted Sources [28 B]
Get:46 precise-backports/universe Sources [44.2 kB]
Get:47 precise-backports/multiverse Sources [5,750 B]
Get:48 precise-backports/main amd64 Packages [6,477 B]
Get:49 precise-backports/restricted amd64 Packages [28 B]
Get:50 precise-backports/universe amd64 Packages [46.2 kB]
Get:51 precise-backports/multiverse amd64 Packages [5,419 B]
Get:52 precise-backports/main i386 Packages [6,478 B]
Get:53 precise-backports/restricted i386 Packages [28 B]
Get:54 precise-backports/universe i386 Packages [46.1 kB]
Get:55 precise-backports/multiverse i386 Packages [5,413 B]
Get:56 precise-backports/main TranslationIndex [202 B]
Get:57 precise-backports/multiverse TranslationIndex [202 B]
Get:58 precise-backports/restricted TranslationIndex [193 B]
Get:59 precise-backports/universe TranslationIndex [205 B]
Get:60 precise-updates/main Translation-en [418 kB]
Get:61 precise-updates/multiverse Translation-en [9,806 B]
Get:62 precise-updates/restricted Translation-en [3,869 B]
Get:63 precise-updates/universe Translation-en [166 kB]
Hit precise/main Sources
Hit precise/restricted Sources
Hit precise/universe Sources
Hit precise/multiverse Sources
Hit precise/main amd64 Packages
Hit precise/restricted amd64 Packages
Hit precise/universe amd64 Packages
Hit precise/multiverse amd64 Packages
Hit precise/main i386 Packages
Hit precise/restricted i386 Packages
Hit precise/universe i386 Packages
Hit precise/multiverse i386 Packages
Hit precise/main TranslationIndex
Hit precise/multiverse TranslationIndex
Hit precise/restricted TranslationIndex
Hit precise/universe TranslationIndex
Get:64 precise-backports/main Translation-en [5,737 B]
Get:65 precise-backports/multiverse Translation-en [4,852 B]
Get:66 precise-backports/restricted Translation-en [28 B]
Get:67 precise-backports/universe Translation-en [35.9 kB]
Hit precise/main Translation-en
Hit precise/multiverse Translation-en
Hit precise/restricted Translation-en
Hit precise/universe Translation-en
Fetched 6,523 kB in 9s (679 kB/s)
Reading package lists... Done

Install the MongoDB packages

Specify that want to install 2.6.x of MongoDB.

sudo apt-get install -y mongodb-org=2.6.9 mongodb-org-server=2.6.9 mongodb-org-shell=2.6.9 mongodb-org-mongos=2.6.9 mongodb-org-tools=2.6.9

On my machine this took approximately 15 minutes to download and install.

This returns the following :

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 192 not upgraded.
Need to get 116 MB of archives.
After this operation, 292 MB of additional disk space will be used.
Get:1 dist/10gen mongodb-org-shell amd64 2.6.9 [4,471 kB]
Get:2 dist/10gen mongodb-org-server amd64 2.6.9 [9,521 kB]
Get:3 dist/10gen mongodb-org-mongos amd64 2.6.9 [7,199 kB]
Get:4 dist/10gen mongodb-org-tools amd64 2.6.9 [94.4 MB]
Get:5 dist/10gen mongodb-org amd64 2.6.9 [3,650 B]
Fetched 116 MB in 12min 24s (155 kB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 73195 files and directories currently installed.)
Unpacking mongodb-org-shell (from .../mongodb-org-shell_2.6.9_amd64.deb) ...
Selecting previously unselected package mongodb-org-server.
Unpacking mongodb-org-server (from .../mongodb-org-server_2.6.9_amd64.deb) ...
Selecting previously unselected package mongodb-org-mongos.
Unpacking mongodb-org-mongos (from .../mongodb-org-mongos_2.6.9_amd64.deb) ...
Selecting previously unselected package mongodb-org-tools.
Unpacking mongodb-org-tools (from .../mongodb-org-tools_2.6.9_amd64.deb) ...
Selecting previously unselected package mongodb-org.
Unpacking mongodb-org (from .../mongodb-org_2.6.9_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Setting up mongodb-org-shell (2.6.9) ...
Setting up mongodb-org-server (2.6.9) ...
Adding system user `mongodb' (UID 106) ...
Adding new user `mongodb' (UID 106) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 111) ...
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
mongod start/running, process 1609
Setting up mongodb-org-mongos (2.6.9) ...
Setting up mongodb-org-tools (2.6.9) ...
Setting up mongodb-org (2.6.9) ...

Because I don’t want MongoDB to be upgraded, run the following command to pin the package

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

There is no output from running these commands

Start MongoDB

Issue the following command to start mongod if it has not already started. My experience is that after the MongoDB is installed, it is automatically started

sudo service mongod start

This informs you that the service is already running

start: Job is already running: mongod

Now its time to connect to MongoDB, Run


However if you try to access mongo by running mongod, I received the following error:

mongod --help for help and startup options
2016-07-12T10:00:19.579+0000 [initandlisten] MongoDB starting : pid=1647 port=27017 dbpath=/data/db 64-bit host=mongodb-dev
2016-07-12T10:00:19.579+0000 [initandlisten] db version v2.6.9
2016-07-12T10:00:19.579+0000 [initandlisten] git version: df313bc75aa94d192330cb92756fc486ea604e64
2016-07-12T10:00:19.580+0000 [initandlisten] build info: Linux 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2016-07-12T10:00:19.580+0000 [initandlisten] allocator: tcmalloc
2016-07-12T10:00:19.580+0000 [initandlisten] options: {}
2016-07-12T10:00:19.602+0000 [initandlisten] exception in initAndListen: 10296
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
, terminating
2016-07-12T10:00:19.603+0000 [initandlisten] dbexit:
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: going to close listening sockets...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: going to flush diaglog...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: going to close sockets...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: waiting for fs preallocator...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: lock for final commit...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: final commit...
2016-07-12T10:00:19.603+0000 [initandlisten] shutdown: closing all files...
2016-07-12T10:00:19.603+0000 [initandlisten] closeAllFiles() finished
2016-07-12T10:00:19.604+0000 [initandlisten] dbexit: really exiting now


To quickly check that everything is working, from the mongo shell, execute

show dbs

On a blank, freshly installed database, you should see something similar to

admin  (empty)
local 0.078GB

Excellent now we have a running copy of mongo.

Ctrl+C to exit the mongo shell

and exit to terminate the SSH session.

Accessing MongoDB from the Host

By default, MongoDB is configured to only accept connections from This needs to be updated, otherwise MongoDB will not accept connections from the host.

From within ssh, run

sudo vi /etc/mongod.conf

Must use sudo, to run vi with elevated privilages, otherwise you will not be able to save the configuration file. Now vi is not like notepad++. So you don’t have to google how to edit a file using vi, do the following:

  • scroll down until you get to the line “bind_ip =”
  • press i to put the document into edit mode. vi starts in command mode
  • comment out the line by entering “# “
  • press Esc to take the document out of insert mode and return to command mode
  • enter :wq to save the document and exit vi

The next step is to get Vagrant to port forward all request to port 27017.

You need to add the following lines to the vagrantfile "forwarded_port", guest: 3000, host: 3000 "forwarded_port", guest: 27017, host: 27017, id: "mongodb"

this will ensure that all requests to port 27017 are sent to the VM.

Make sure that you are no longer within the ssh session, and run

vagrant reload

This will reload the OS, and will include the port forwarding

This returns the following :

==> default: Forwarding ports...
default: 3000 (guest) => 3000 (host) (adapter 1)
default: 27017 (guest) => 27017 (host) (adapter 1)
default: 80 (guest) => 8080 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...

To test from the host, i have used Robomongo. Once the application has installed, run Robomongo and create a new connection.

Just give the connection a name, leave the address and port to the default values “localhost” and “27017” respectively.

Robomongo Connection Settings Dialog

Robomongo Connection Settings Dialog

Click on the test button, and you should be successful.

Robomongo Connection Test Success Dialog

Robomongo Connection Test Success Dialog

Save the connection

Robomongo Connection Settings Dialog

Robomongo Connections Dialog

And then connect. And you can issue a quick query and see the minimal content within the database

Robomongo connecting to VM database

Robomongo connecting to VM database


I don’t want to have to repeat the above steps every time I want to build a machine, so its time to put all of the commands into a provisioning file, and have vagrant run the scripts when the box is created.

I dont want to have to modify /etc/mongod.conf, so copy this to the vagrant folder. The vagrant folder is created by default.

cp /etc/mongod.conf /vagrant

This will copy the modified configuration file to the host. This will become clearer when you look at the provisioning file to be created.

Next, modify the vagrant file so that Vagrant knows it needs to run the provisions file

This is my vagrant file as it currently stands

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at

  # Every Vagrant development environment requires a box. You can search for
  # boxes at = "hashicorp/precise64"
  config.vm.hostname = "mongodb-dev" "forwarded_port", guest: 3000, host: 3000 "forwarded_port", guest: 27017, host: 27017, id: "mongodb"
  config.vm.provision "shell", path: ""

It is this line, “config.vm.provision “shell”, path: “”” that tells vagrant to run the provision file. You can include the scripts inline, but once you start getting a lot of commands, I find the file get unwieldy.

And this is my file

apt-get -y update

apt-key adv --keyserver hkp:// --recv 7F0CEB10

echo 'deb dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list

apt-get update -y

apt-get install -y mongodb-org=2.6.9 mongodb-org-server=2.6.9 mongodb-org-shell=2.6.9 mongodb-org-mongos=2.6.9 mongodb-org-tools=2.6.9

echo "mongodb-org hold" | dpkg --set-selections
echo "mongodb-org-server hold" | dpkg --set-selections
echo "mongodb-org-shell hold" | dpkg --set-selections
echo "mongodb-org-mongos hold" | dpkg --set-selections
echo "mongodb-org-tools hold" | dpkg --set-selections

service mongod stop

cp /vagrant/mongod.conf /etc

service mongod start

Its all the commands I ran individually. You will notice that I have not included the sudo command, as the provisioning file always runs as elevated you don’t need to use sudo.

This command, “cp /vagrant/mongod.conf /etc” will ensure that mongo will use our altered configuration so that we can reconnect again from the host.

You can make changes to a provision file, but Vagrant will not run the provision file when you run Vagrant up. Vagrant will only run the provision file the first time you run vagrant up. If you want vagrant to run a modified provision file you have to run vagrant provision, then vagrant up

To test this, you will first need to destroy the VM, by running the following command:

vagrant destroy -f 

the -f parameter will destroy the VM without confirmation

Now all you need to do, after saving the vagrantfile and is run:

vagrant up 

and it will run through all the commands that where performed previously. It will still take about 15 minutes. The only difference I noted is that when you run the commands, especially “apt-get update” you will no longer be presented with a percentage complete progress dialog. So just wait, it has not hung.

To test once the process has finished, you can ssh into the box and verify that mongo is running, and connect via Robomongo


After I was satisfied with all of this I then installed a brand new copy of Sitecore 8. I didn’t even need to change the connection settings, everything worked first time.

Next Steps

Whenever I start a new Sitecore project that requires MongoDB, with two files I have spin up a new instance specifically for that project.

I want to look into where MongoDB is installed, as I have seen a recommendation that you do not install MongoDB into the default folder, but rather into a folder that is version specific. This way when you upgrade, and there is a problem, its easier to just switch back to the previous version.

Instead of having only a single instance, I want to set up a standard 3 server configuration of Primary, Secondary, and Arbitar, so that I can have a development environment that is similar to production.

Instead of using localhost, I want to use the VM name, just in case I have multiple MongoDB VM’s running for different projects. Vagrant is clever enough to resolve port conflicts but I just don’t want to get into this situation to begin with


My musing about anything and everything

Tagged with: ,
Posted in MongoDB, Sitecore, Vagrant, VirtualBox

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 10 other followers

%d bloggers like this: