MongoDB Replica Set Windows Service

I have been doing some development using MongoDB lately, and I have been very happy with it. One of the things I had to do was set it up as a Windows service in the development servers where I work. The process was relatively simple, but seems like a great bit of information to document. I will be showing the settings I use, which include creating the mongo instances as part of a replica set and exposing a RESTful service for monitoring. The following can be done from either the standard Windows command line or PowerShell. I assume you are somewhat familiar with MongoDB and don’t go into much detail, if you are not familiar check out the links!

The command below is used to install MongoDB both as a service and a part of a replica set:

mongod.exe --replSet NAME_OF_REPLICA_SET
--port PORT_NUMBER
--logpath PATH_TO_LOG_FILE --logappend
--rest --dbpath PATH_TO_DATABASE_FOLDER
--serviceName NAME_OF_WINDOWS_SERVICE 
-serviceDisplayName NAME_TO_DISPLAY --install

The command may be run once on each machine, or serveral times on the same machine. Afterwards, remember to start the services up for the first time, either through the Windows Services GUI or the command line using:

net start SERVICE_NAME

Where SERVICE_NAME is the name given to the mongo instance.

Notes:

  • “–port” is not required if running on separate machines, but it is mandatory for instances running on the same machine.
  • “–serviceName” and “–serviceDisplayName” can be the same, but must not be repeated across installs on the same machine.
  • “–rest” is not required but allows for a simple web interface for monitoring a replica set or mongo instance.
  • If the port number for set up is 27017 then, the web interface would be: http://localhost:28017/_replSet (i.e. port + 1000)

Once all the desired instances of mongo are up, connect to the MongoDB shell through one of the instances as follows:

mongo.exe localhost:27017

The above connection assumes there is an instance running on port 27017 of the local machine.

Once connected create the config object. The config object has the following structure:

{
  _id : setname, // Same as the value of --replSet when creating MongoDB instance
  members: [
    {
      _id : ordinal,
      host : hostname:port
      [, arbiterOnly : bool]
      [, buildIndexes : bool]
      [, hidden : bool]
      [, priority: priority]
      [, tags: {loc1 : desc1, loc2 : desc2, ..., locN : descN}]
      [, slaveDelay : ]
      [, votes : ]
    }
    , ...
  ],
  [settings: {
    [getLastErrorDefaults: lasterrdefaults]
    [, getLastErrorModes : modes]
  }]
}

Of immediate relevance to us are the two mandatory options “_id” and “host”. Other important options (likely to be used) are:

  • “arbiterOnly”: Used in an instance of MongoDB that will not be used to store any data and will only take part in voting.
  • “priority”: Values range from 0 – 99 and is used during voting to increase the chances of becoming a primary. (Higher is better)

Create the config object in the shell like so:

config = {_id: name, members: [{...settings...}]

After the config object has been created a replica set may be initiated:

rs.initiate(config)

At this point you have a replica set running as a Windows service. This has the benefit of auto starting the database instances after any server restarts, which should free up some time from the IT folk or yourself if its your responsibility.

Leave a Reply

Your email address will not be published. Required fields are marked *

*