How to Create MongoDB ReplicaSet

In this article i will explain how we can configure mongodb cluster using two nodes in simple language because if anyone is trying to setup 1st time it can be confusing.

MongoDB ReplicaSet Configuration

Please 1st install mongodb in all servers im providing only two server configuration but it can also work in any number of servers.

1 add hosts in all servers and provide them hostnames like.

172.16.10.24 mongodb-1172.16.10.25 mongodb-2

Now open configs of all server located /etc/mongod.conf

And add a config block.

replication:
  replSetName: replicaSet

Note: replicaSet is just a name of a replication cluster you can make it anything like rs-1 or rs1 or replicaset it can be anything you need.

Now restart all servers after setting hostnames and and updating configs of all servers.

After restart all servers initiate a replica set by logging in only in the primary server mongo-shell and entering a query.

ubuntu@mongodb-node-1:~$mongo
> rs.initiate()

And now add server to replica set by running query only in primary server.

> rs.add('mongodb-2:27017')
> rs.add('mongodb-3:27017')
> rs.add('mongodb-4:27017')

And this will initiate replicaSet and data will start syncing in all servers connected in replicaSet.

And lastly, to check replication status, use a query.

ubuntu@mongodb-node-1:~$mongo
> rs.status()

It will show the status of the connected servers in replicaSet status output like below.

replicaSet:SECONDARY> rs.status()
{
        "set" : "replicaSet",
        "date" : ISODate("2023-09-11T12:20:19.723Z"),
        "myState" : 2,
        "term" : NumberLong(2),
        "syncingTo" : "172.16.10.24:27017",
        "syncSourceHost" : "172.16.10.24:27017",
        "syncSourceId" : 0,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1694434819, 316),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1694434819, 316),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1694434819, 318),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1694434819, 316),
                        "t" : NumberLong(2)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1694434788, 357),
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(2),
                "lastVoteDate" : ISODate("2023-09-11T09:35:02.711Z"),
                "electionCandidateMemberId" : 0,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1694421434, 224),
                        "t" : NumberLong(1)
                },
                "priorityAtElection" : 1
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.16.10.24:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 9919,
                        "optime" : {
                                "ts" : Timestamp(1694434818, 53),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1694434818, 53),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2023-09-11T12:20:18Z"),
                        "optimeDurableDate" : ISODate("2023-09-11T12:20:18Z"),
                        "lastHeartbeat" : ISODate("2023-09-11T12:20:18.204Z"),
                        "lastHeartbeatRecv" : ISODate("2023-09-11T12:20:19.100Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1694424902, 1),
                        "electionDate" : ISODate("2023-09-11T09:35:02Z"),
                        "configVersion" : 2
                },
                {
                        "_id" : 1,
                        "name" : "mongodb-2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 9920,
                        "optime" : {
                                "ts" : Timestamp(1694434819, 318),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2023-09-11T12:20:19Z"),
                        "syncingTo" : "172.16.10.24:27017",
                        "syncSourceHost" : "172.16.10.24:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1694434819, 318),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1694434819, 321),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
replicaSet:SECONDARY>

If output is not like above which means slave state is not  (  “state” : 2,) then it means it should be (  “state” : 5,) which means slaves are still syncing data from primary and after initial replication complete state will change to 2.

All servers should be able to connect using 27017 port. If not please open the port from the firewall and allow connection between all servers in replicaSet.

If your data is huge please tail logs of slave servers to see progress of data sync. Primary server will not show the replication progress if you want to see sync progress; it can only be shown from the slave server which is trying to replicate data from primary.

Leave a Reply

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

© 2024 Doc.thewion - WordPress Theme by WPEnjoy