Enabling replica-set in MongoDB with just one node

9472


A minimum of three no replica set is recommended for production loads.
But it is possible to create a single node replica set in MongoDB. You may want this in times like development where you want to test out some of the replica set features.

Following are the steps to create a single node replica set in MongoDB.

1. Locate the mongod.conf file and add the replica set details

Add the following replica set details to mongod.conf file

replication:
  replSetName: "<replica-set name>"

Example:

replication:
  replSetName: "rs0"

Note: Location in brew installed MongoDB /usr/local/etc/mongod.conf
In Ubuntu you can find at /etc/mongod.conf

2. Initiate the replica set using rs.initiate()

Login to MongoDB shell and run the command rs.initiate() this will start your replica set. Logs looks like following on successful start

> rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "127.0.0.1:27017",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1577545731, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1577545731, 1)
}

That's all with these two simple steps you are running a MongoDB replica set with one node only.