Installation of a MongoDb Replica Set

The MongoDb replica set, allows the creation of a resilient database backed by multiple servers.  According to the Mongo description a replica set is "A Synchronised data set across multiple instances on preferably geographically different servers."

Setup Required

Before describing the down loading and installation instructions for Mongo, we need to describe the setup that can be used.

The minimum number of servers required for a replica set is 3.  To minimise the amount of hardware required, we will make one of the servers an Arbiter.  This is an instance of Mongo, but it is only used for voting.

The following diagram shows an example setup that could be used.



On this page:

Default Port

The default port used by Witness is 27010. For all server instances the port should be kept as 27010.



Download and installation of MongoDb

Download the MongoDB ZIP installer here: https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl?_ga=1.147098928.662975023.1471964900

Make sure you choose the ZIP and not the MSI version as we need to manually install the servers.

To enable resilience, the two Mongo servers must be installed on their own hardware.  You should not use the same hardware platform that Witness is running on.  The final server which is the Arbiter can be run on the same hardware installation as Witness.

Default Setup

The mongo zip file will contain only one directory which is bin.  On each server create the directory C:\mongodb and copy the bin folder to this directory.

Add to environment variables

To make the usage of Mongo easier, we recommend that you append the bin folder to the Windows Path. Please append the C:\mongodb\bin folder to the PATH environment variable.



In the mongodb directory create data\replicaset-27010 folder and also the log folder.

If done correctly, you will have the following under C:\monogdb

  • C:\monogdb\bin



  • C:\mongodb\data\replicaset-27010

  • C:\mongodb\log

MongoDb Configuration File

To allow more control over configuration all instances of the MongoDb server will use a config file.

In each C:\mongodb folder create the file replicaset-27010.cfg with the following content.

  • dbpath=C:\mongodb\data\replicaset-27010

  • logpath=C:\mongodb\log\replicaset-27010.log

  • logappend=true

  • port=27010

  • replSet=navtechreplicaset

  • bind_ip = 127.0.0.1,<<ip address of server/DNS name>>



  • dbath: this is the directory where MongoDB will store data.  Each member of the replica set will have its own folder.  If the folder does not exist, MongoDB will not start.

  • logpath: this is the file where MongoDB will send all diagnostic information.  If the file is not present, MongoDB will create it.  By default MongoDB will overwrite the log file each time the process starts.  To append to the log instead, add "logappend=true" to the config file.

  • port: this is the port that the MongoDB server will listen on for client connections.  By default if not set, MongoDB will resort to 27017

  • replSet: the name of  the replica set this server belongs to.  All members of the replica set must use this name.  if not set in the config, this server is not part of a replica set and any commands issued to it to make it part of a replica set will fail.

  • bind_ip: MongoDB will bind to these ip addresses.  It is possible to bind to more than one, simply add each address as a comma separated list.

Install MongoDB as a service

To make maintenance of the servers as easy as possible it is recommended that you install the MongoDB instances as services.  On each machine that Mongo will be running, issue the following command through the command line.

mongod --config "C:\mongodb\replicaset-27010.cfg" --install --serviceName "NavtechRadar-MongoDb-replicaset-27010" --serviceDisplayName "NavtechRadar-MongoDb-replicaset-27010"



Configure MongoDB Replica Set

To configure the replica set, all the MongoDB servers need to be running.

You must be logged into the server that will become the PRIMARY node in the Replica Set for the following to function correctly.



mongo --port 27010



rs.initiate()



Add Secondary Replica Set member

Depending on the version of MonogDB that you running with, you may need to perform the following command on the PRIMARY or the SECONDARY.



After running the rs.add command, execute the command

On the primary server.  You should now see the two servers with one marked as PRIMARY and the other as SECONDARY.

Add the Arbiter

Finally execute the following command on the primary.

Where HOST is the IP Address of where the arbiter resides.

Execute the command rs.status() again, and you should see something like the following

You have now successfully installed and configured the MonogDB servers as a replica set.