Setting up replication in MongoDB

Steps to configure replication in MongoDB

In this replication setup, we will have the following configuration

Server Name Role Port
mongoserver1 PRIMARY 20050
mongoserver2 SECONDARY 20051
mongoserver3 SECONDARY 20052
mongoserver4 ARBITER 20053








Start the ‘mongod’ server process on all the servers

1st Server 1

$ mkdir -p /mongo/server1
$ mongod --bind_ip localhost,mongoserver1 --dbpath /mongo/server1 --port 20050 --logpath /mongo/server1/mongo.log --replSet testset --fork

2nd Server

$ mkdir -p /mongo/server2
$ mongod --bind_ip localhost,mongoserver2 --dbpath /mongo/server2 --port 20051 --logpath /mongo/server2/mongo.log --replSet testset --fork

3rd Server

$ mkdir -p /mongo/server3
$ mongod --bind_ip localhost,mongoserver3 --dbpath /momgo/server3 --port 20052 --logpath /mongo/server3/mongo.log --replSet testset --fork


$ mkdir -p /mongo/server4
$ mongod --bind_ip localhost,mongoserver4 --dbpath /mongo/server4 --port 20053 --logpath /mongo/server4/mongo.log --replSet testset --fork

Initiate replica set

$ mongo --port 20050
mongo > rs.initiate()

Check the status of replica set

mongo > rs.status()

Add members to the replica set

mongo> rs.add("mongoserver2:20051")
mongo> rs.add("mongoserver3:20052")

Adding Arbiter to the replica set

mongo> rs.addArb("mongoserver4:20053")

Find the details of the replication Set

mongo> rs.status()

Watch the video

[vdo id=cf94f356a259479fbd033e9d0048c6bc]

Leave a comment

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

We assure our best to meet up to your expectations

Your Feedback will help us

Glad That You Liked It

Your Feedback will help us to improve