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

MongoDB

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: https://kb.sitecore.net/articles/920798 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://keyserver.ubuntu.com:80 --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://keyserver.ubuntu.com:80 --recv 7F0CEB10
gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com
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 http://downloads-distro.mongodb.org/repo/ubuntu-upstart 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 http://security.ubuntu.com precise-security InRelease [55.7 kB]
Get:2 http://security.ubuntu.com precise-security/main Sources [142 kB]
Get:3 http://security.ubuntu.com precise-security/restricted Sources [4,476 B]
Get:4 http://security.ubuntu.com precise-security/universe Sources [48.9 kB]
Get:5 http://security.ubuntu.com precise-security/multiverse Sources [2,720 B]
Get:6 http://security.ubuntu.com precise-security/main amd64 Packages [616 kB]
Ign http://downloads-distro.mongodb.org dist InRelease
Ign http://us.archive.ubuntu.com precise InRelease
Get:7 http://us.archive.ubuntu.com precise-updates InRelease [55.7 kB]
Get:8 http://security.ubuntu.com precise-security/restricted amd64 Packages [11.6 kB]
Get:9 http://security.ubuntu.com precise-security/universe amd64 Packages [133 kB]
Get:10 http://security.ubuntu.com precise-security/multiverse amd64 Packages [3,160 B]
Get:11 http://security.ubuntu.com precise-security/main i386 Packages [677 kB]
Get:12 http://downloads-distro.mongodb.org dist Release.gpg [490 B]
Get:13 http://security.ubuntu.com precise-security/restricted i386 Packages [11.6 kB]
Get:14 http://security.ubuntu.com precise-security/universe i386 Packages [141 kB]
Get:15 http://us.archive.ubuntu.com precise-backports InRelease [55.7 kB]
Get:16 http://security.ubuntu.com precise-security/multiverse i386 Packages [3,340 B]
Get:17 http://security.ubuntu.com precise-security/main TranslationIndex [208 B]
Get:18 http://security.ubuntu.com precise-security/multiverse TranslationIndex [199 B]
Get:19 http://security.ubuntu.com precise-security/restricted TranslationIndex [202 B]
Get:20 http://security.ubuntu.com precise-security/universe TranslationIndex [205 B]
Get:21 http://security.ubuntu.com precise-security/main Translation-en [256 kB]
Get:22 http://security.ubuntu.com precise-security/multiverse Translation-en [1,698 B]
Get:23 http://security.ubuntu.com precise-security/restricted Translation-en [2,976 B]
Get:24 http://security.ubuntu.com precise-security/universe Translation-en [85.0 kB]
Hit http://us.archive.ubuntu.com precise Release.gpg
Get:25 http://downloads-distro.mongodb.org dist Release [2,040 B]
Get:26 http://us.archive.ubuntu.com precise-updates/main Sources [497 kB]
Get:27 http://downloads-distro.mongodb.org dist/10gen amd64 Packages [29.8 kB]
Get:28 http://downloads-distro.mongodb.org dist/10gen i386 Packages [29.6 kB]
Get:29 http://us.archive.ubuntu.com precise-updates/restricted Sources [8,708 B]
Get:30 http://us.archive.ubuntu.com precise-updates/universe Sources [128 kB]
Ign http://downloads-distro.mongodb.org dist/10gen TranslationIndex
Get:31 http://us.archive.ubuntu.com precise-updates/multiverse Sources [10.2 kB]
Get:32 http://us.archive.ubuntu.com precise-updates/main amd64 Packages [995 kB]
Ign http://downloads-distro.mongodb.org dist/10gen Translation-en_US
Get:33 http://us.archive.ubuntu.com precise-updates/restricted amd64 Packages [16.2 kB]
Get:34 http://us.archive.ubuntu.com precise-updates/universe amd64 Packages [277 kB]
Ign http://downloads-distro.mongodb.org dist/10gen Translation-en
Get:35 http://us.archive.ubuntu.com precise-updates/multiverse amd64 Packages [16.9 kB]
Get:36 http://us.archive.ubuntu.com precise-updates/main i386 Packages [1,063 kB]
Get:37 http://us.archive.ubuntu.com precise-updates/restricted i386 Packages [16.1 kB]
Get:38 http://us.archive.ubuntu.com precise-updates/universe i386 Packages [287 kB]
Get:39 http://us.archive.ubuntu.com precise-updates/multiverse i386 Packages [17.1 kB]
Get:40 http://us.archive.ubuntu.com precise-updates/main TranslationIndex [208 B]
Get:41 http://us.archive.ubuntu.com precise-updates/multiverse TranslationIndex [202 B]
Get:42 http://us.archive.ubuntu.com precise-updates/restricted TranslationIndex [202 B]
Get:43 http://us.archive.ubuntu.com precise-updates/universe TranslationIndex [205 B]
Hit http://us.archive.ubuntu.com precise Release
Get:44 http://us.archive.ubuntu.com precise-backports/main Sources [5,922 B]
Get:45 http://us.archive.ubuntu.com precise-backports/restricted Sources [28 B]
Get:46 http://us.archive.ubuntu.com precise-backports/universe Sources [44.2 kB]
Get:47 http://us.archive.ubuntu.com precise-backports/multiverse Sources [5,750 B]
Get:48 http://us.archive.ubuntu.com precise-backports/main amd64 Packages [6,477 B]
Get:49 http://us.archive.ubuntu.com precise-backports/restricted amd64 Packages [28 B]
Get:50 http://us.archive.ubuntu.com precise-backports/universe amd64 Packages [46.2 kB]
Get:51 http://us.archive.ubuntu.com precise-backports/multiverse amd64 Packages [5,419 B]
Get:52 http://us.archive.ubuntu.com precise-backports/main i386 Packages [6,478 B]
Get:53 http://us.archive.ubuntu.com precise-backports/restricted i386 Packages [28 B]
Get:54 http://us.archive.ubuntu.com precise-backports/universe i386 Packages [46.1 kB]
Get:55 http://us.archive.ubuntu.com precise-backports/multiverse i386 Packages [5,413 B]
Get:56 http://us.archive.ubuntu.com precise-backports/main TranslationIndex [202 B]
Get:57 http://us.archive.ubuntu.com precise-backports/multiverse TranslationIndex [202 B]
Get:58 http://us.archive.ubuntu.com precise-backports/restricted TranslationIndex [193 B]
Get:59 http://us.archive.ubuntu.com precise-backports/universe TranslationIndex [205 B]
Get:60 http://us.archive.ubuntu.com precise-updates/main Translation-en [418 kB]
Get:61 http://us.archive.ubuntu.com precise-updates/multiverse Translation-en [9,806 B]
Get:62 http://us.archive.ubuntu.com precise-updates/restricted Translation-en [3,869 B]
Get:63 http://us.archive.ubuntu.com precise-updates/universe Translation-en [166 kB]
Hit http://us.archive.ubuntu.com precise/main Sources
Hit http://us.archive.ubuntu.com precise/restricted Sources
Hit http://us.archive.ubuntu.com precise/universe Sources
Hit http://us.archive.ubuntu.com precise/multiverse Sources
Hit http://us.archive.ubuntu.com precise/main amd64 Packages
Hit http://us.archive.ubuntu.com precise/restricted amd64 Packages
Hit http://us.archive.ubuntu.com precise/universe amd64 Packages
Hit http://us.archive.ubuntu.com precise/multiverse amd64 Packages
Hit http://us.archive.ubuntu.com precise/main i386 Packages
Hit http://us.archive.ubuntu.com precise/restricted i386 Packages
Hit http://us.archive.ubuntu.com precise/universe i386 Packages
Hit http://us.archive.ubuntu.com precise/multiverse i386 Packages
Hit http://us.archive.ubuntu.com precise/main TranslationIndex
Hit http://us.archive.ubuntu.com precise/multiverse TranslationIndex
Hit http://us.archive.ubuntu.com precise/restricted TranslationIndex
Hit http://us.archive.ubuntu.com precise/universe TranslationIndex
Get:64 http://us.archive.ubuntu.com precise-backports/main Translation-en [5,737 B]
Get:65 http://us.archive.ubuntu.com precise-backports/multiverse Translation-en [4,852 B]
Get:66 http://us.archive.ubuntu.com precise-backports/restricted Translation-en [28 B]
Get:67 http://us.archive.ubuntu.com precise-backports/universe Translation-en [35.9 kB]
Hit http://us.archive.ubuntu.com precise/main Translation-en
Hit http://us.archive.ubuntu.com precise/multiverse Translation-en
Hit http://us.archive.ubuntu.com precise/restricted Translation-en
Hit http://us.archive.ubuntu.com 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 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-shell amd64 2.6.9 [4,471 kB]
Get:2 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-server amd64 2.6.9 [9,521 kB]
Get:3 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-mongos amd64 2.6.9 [7,199 kB]
Get:4 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-org-tools amd64 2.6.9 [94.4 MB]
Get:5 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ 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) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
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


mongo

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 build20.nj1.10gen.cc 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.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, 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

do’h

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 127.0.0.1. 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 = 127.0.0.1”
  • 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


  config.vm.network "forwarded_port", guest: 3000, host: 3000 
  config.vm.network "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

Provisioning

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
  # https://docs.vagrantup.com.

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

It is this line, “config.vm.provision “shell”, path: “provision.sh”” 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 provision.sh file


apt-get -y update

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart 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 provision.sh 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

Sitecore

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 12 other followers

%d bloggers like this: