Set up MongoDB replica set on Ubuntu machines.

saurav omar
2 min readDec 28, 2018

--

MongoDB Replica Set:

Introduction:

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. MongoDB handles replication through an implementation called “replication sets”. Replication sets in their basic form are somewhat similar to nodes in a master-slave configuration. The primary member of the replica set can be used for read and write operations whereas the secondary members are available for read-only operations.

More information: https://docs.mongodb.com/manual/replication/

Requirements

  • Each server instance must allow TCP traffic from each replica set member over port 27017
  • Each server instance hostname must be resolvable from each replica set member

Setup Replica Sets:

Setup DNS Resolution:

MongoDB instances to communicate with each other effectively, we will need to configure our machines to resolve the proper hostname for each member.

Add all MongoDb Servers in /etc/hosts:

192.168.0.1    mongodb01.yourdomain.com
192.168.0.2 mongodb02.yourdomain.com
192.168.0.3 mongodb03.yourdomain.com

Verify that all the nodes are able to connect each other using command:

mongo -host $hostname

Example :

mongo -host mongodb01.yourdomain.com
mongo -host mongodb02.yourdomain.com
mongo -host mongodb03.yourdomain.com

run above commands from all the servers to verify all servers are able to connect to each other.

Create Replica Set:

Update /etc/mongod.conf in all servers

replication:
replSetName: ${replica set name}

Restart MongoDB in all servers:

systemctl restart mongod

Use the mongo shell to connect once the service is running again.

mongo

Configure Replica Set:

Initialize Replica Set: This will initalize the replica set where this commands get executed becomes primary

rs.initiate( {    _id : “${replica set name}”,     members: [       { _id: 0, host: “mongodb01.yourdomain.com:27017” },       { _id: 1, host: “mongodb02.yourdomain.com:27017” },       { _id: 2, host: “mongodb03.yourdomain.com:27017” }      ]});

Verify all nodes are active and working fine:

rs.status()

O/P of the above command will be

It will display heartbeat , last sync time etc. and confirm all the nodes are connected.

{
"set" : "set1",
"date" : ISODate("2018-11-12T16:09:58Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "mongodb01.yourdomain.com:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"self" : true
},
{
"_id" : 1,
"name" : "mongodb02.yourdomain.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 770622,
"optime" : {
"t" : 1294848597000,
"i" : 13
},
"optimeDate" : ISODate("2018-11-12T16:09:57Z"),
"lastHeartbeat" : ISODate("2018-11-12T16:09:57Z")
},
{
"_id" : 3,
"name" : "mongodb03.yourdomain.com:27017",
"health" : 1,
"state" : 7,
"stateStr" : "SECONDARY",
"uptime" : 770622,
"optime" : {
"t" : 0,
"i" : 0
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-11-12T16:09:58Z")
}

],
"ok" : 1
}

--

--

saurav omar
saurav omar

Written by saurav omar

Geek and Always ready to give more than 100%

Responses (1)