Setup MongoDB Replica

Jeril Kuriakose
Analytics Vidhya
Published in
3 min readMay 6, 2021

--

In this post we will setup a replica node for an already existing mongoDB or for a new mongoDB. We will also see how to access a mongoDB replica setup using Python programming language. A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.

To start we need two instances to host mongoDB. One instance will act as the primary, and the other will act as the secondary. More about primary and secondary can be found here. Once the instances are ready, we can download mongoDB from here. MongoDB should be installed in both the instances. If anyone has an existing mongoDB, they need not install it again. The existing mongoDB service must be stopped and the following steps must be follwed to setup mongoDB replica.

Step 1:

Start mongoDB in the primary instance, using the following command

mongod.exe --dbpath <"path/to/your/db"> --port <port number> --replSet "rs0" --bind_ip <localhost / 0.0.0.0>

Example:

mongod.exe --dbpath "D:\mongoData\db\data" --port 27017 --replSet "rs0" --bind_ip 0.0.0.0

the --dpath and --port is optional, if not provided the default dbpathand port will be taken. --replSet is the “name of the replica set that the mongod is part of, all hosts in the replica set must have the same set name”. In our case we have given as rs0 , this can be any name, but all the replica sets must use the same name. --bind_ip is the socket path on which mongod should listen for client connections, to expose mongod for remote access the bind_ip must be 0.0.0.0 .

Step 2:

Start mongoDB in the secondary instance, using the following command

mongod.exe --dbpath <"path/to/your/db"> --port <port number> --replSet "rs0" --bind_ip <localhost / 0.0.0.0>

Step 3:

Open a new command prompt or terminal in the primary instance, and start mongo shell

mongo.exe

Step 4:

Initiate the replica set inside mongo shell, using the following command

rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "<instance1_ipaddress>:<instance1_port>" },
{ _id: 1, host: "<instance2_ipaddress>:<instance2_port>" }
]
})

the above command will initiate the replica. The attribute _id: “rs0” is the replica set name that we provided while starting the mongod. The secondary instances can be further added by appending the instance info to the members attribute.

Step 5:

Inside mongo shell, check the status of the replica

rs.status()

This will show the information about the primary node and secondary node and its status. Some times in the secondary node the state might be "stateStr": STARTUP2 , this means that syncing is happening from the primary to secondary. This might take some time based on the size of the already existing data in the primary. Once the synching is over the state of the secondary node will become "stateStr": SECONDARY .

Now we will see how to connect to the replica setup using Python programming language.

Required Package:

pymongo

Syntax:

pymongo.MongoClient('mongodb://<username1>:<password1>@<ipaddress1>:<port1>,<username2>:<password2>@<ipaddress2>:<port2>/?replicaSet=<replicaSetName>')

Example 1 using username and password:

pymongo.MongoClient('mongodb://jeril:abc123@201.202.203.204:27017,jeril:abc123@201.202.203.212:27017/?replicaSet=rs0')

Example 2 without username and password:

pymongo.MongoClient('mongodb://201.202.203.204:27017,201.202.203.212:27017/?replicaSet=rs0')

Code:

import pymongo
client = pymongo.MongoClient('mongodb://jeril:abc123@201.202.203.204:27017,jeril:abc123@201.202.203.212:27017/?replicaSet=rs0')
print(client)
print()
print(client.nodes)

Output:

MongoClient(host=['201.202.203.204:27017', '201.202.203.212:27017'], document_class=dict, tz_aware=False, connect=True, replicaset='rs0')frozenset({('201.202.203.204', 27017), ('201.202.203.212', 27017)})

The addresses passed to MongoClient() are called the seeds. As long as at least one of the seeds is online, MongoClient discovers all the members in the replica set, and determines which is the current primary and which are secondaries or arbiters.

Happy Coding !!!

References:

  1. MongoDB Docs

2. Stackoverflow

3. PyMongo Docs

--

--