Create a MongoDB replica set in Windows

Adela Chao
4 min readOct 23, 2020

A replica set in MongoDB is a group of mongod instances that maintain the same data set. It contains several data bearing nodes and optionally one arbiter node.

Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

The primary node receives all write operations and records all changes to its data sets in its operation log, i.e. oplog.

The secondaries replicate the primary’s oplog and apply the operations to their data sets asynchronously.

By having the secondaries’ data sets reflect the primary’s data set, the replica set can continue to function despite the failure of one or more members.

If the primary is unavailable, an eligible secondary will hold an election to elect itself the new primary.

For more detail about MongoDB replication, please refer to MongoDB documentation.

In this article, we are going to create a test replica set in Windows machine.

This replica set will contain one primary node and two secondary nodes.

Add replication options into MongoDB config file

On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation.

Open mongod.cfg file, add replication options

replication:
oplogSizeMB: <int>
replSetName: <string>
enableMajorityReadConcern: <boolean>

Here we will only specify replSetName which is the replica set name, and leave oplogSizeMB and enableMajorityReadConcern as default value. You can refer to MongoDB configuration file manual to adjust them.

So our replication setting looks like this:

replication:
replSetName: rs0

Convert a standalone instance to a replica set

Now, if you already have a mongod instance running, please shut down it.

if you have installed MongoDB as a service, please restart it.

Open a new Command Prompt with Administrator mode, restart the instance as a member of the replica set :

mongod --port 27017 --replSet rs0 --dbpath="C:\data\db0"

You can see we use the replSet option to specify the replica set name which is the the name we define in mongod.cfg just now.

So this mongod instance is started. Open a new command prompt, connect to the instance with default port “27017” by simply using “mongo” command

Once connected, we are going to initiate the replica set by command

rs.initiate()

Add new members to the replica set

The replicate set is now operational, we can start to add new members.

Open a new Command Prompt, create a new instance listening to port 27027.

mongod --port 27027 --replSet rs0 --dbpath="C:\data\db1"

Repeat the step for 3rd instance listening to port 27037.

mongod --port 27037 --replSet rs0 --dbpath="C:\data\db2"

Add the 2 instances into our replica set using rs.add() command

rs.add( { host: "127.0.0.1:27027", priority: 0, votes: 0 } )
rs.add( { host: "127.0.0.1:27037", priority: 0, votes: 0 } )

Check replica set status

Now we have one primary node and two secondary nodes in the replica set.

Lets confirm it by rs.status() method

rs.status()

If you connect to this replica set by MongoDB Compass, you can see a list of our 3 nodes in the cluster :

Now feel free to play around with this replica set, in next article I will show you how to downgrade replica set back to a standalone node.

--

--