Multiple MongoDB Instances with Ubuntu’s Upstart

Recently a client asked me to setup multiple instances of MongoDB on a Linux Ubuntu server. Ubuntu does not use standard /etc/init.d/ scripts, instead it uses upstart, an event-based replacement for the /sbin/init daemon, that handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running. Upstart uses it’s own limited syntax to describe a service or task. I tried launching several processes from a single upstart config, but upstart could not track the service properly. Instead, I broke-up the upstart script into two — one master to define the instances, and another to start each one independently.

The first part of this setup involved creating a mongodb configuration file for each instance. I decided to put them in /etc/mongodb.d/ and name them rs0-0.conf, rs0-1.conf, and rs0-3.conf (rs for replica set). The configuration file for /etc/mongodb.d/rs0-0.conf contains the following options:

These new mongodb upstart scripts will use the /etc/mongodb.conf file (if present), and any other file ending in .conf under /etc/mongodb.d/. This means the scripts will work with the default single-instance mongodb configuration as well. It’s important to avoid using the ‘fork’ option in the mongodb configuration files — this will break upstart’s ability to track the process ID.

All instances of mongodb can be started / stopped using the standard upstart syntax (“start mongodb” and “stop mongodb”), and when running, the upstart processes list looks like this:

A single instance can also be started / stopped using this syntax:

The following upstart scripts should be saved under /etc/init/mongodb.conf and /etc/init/mongodb.conf. To start mongodb automatically when the system boots, a symbolic link should be created (if it doesn’t already exist) under /etc/init.d/mongodb.

The following mongodb.conf file should be placed in /etc/init/.

The following mongodb-instance.conf file should be placed in /etc/init/.

Find this content useful? Share it with your friends!