How to create a MongoDB Replica Set on CentOS 8


A replica set is a cluster of MongoDB database servers that implements master-slave (primary-secondary) replication. Replica sets also fail over automatically, so if one of the members becomes unavailable, a new primary host is elected and your data is still accessible. When combined with sharded database clusters, replica sets allow you to create scalable, highly available database systems for use with growing datasets.
To create a replica set, you’ll need at least two nodes with MongoDB installed (Recommended three nodes).
In this tutorial, we use two nodes: 11.11.11.11 (Primary) and 22.22.22.22 (Secondary).

Configure Networking
To allow for consistent replication, each node will need to communicate with all the others in the cluster. For example, in a two node set, data transfer will look like this:

two nodes mongodb replica set

Firewall
On each of your nodes, open port 27017 for each others.
On Primary node 11.11.11.11:

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="22.22.22.22" port protocol="tcp" port="27017" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="22.22.22.22" port protocol="udp" port="27017" accept'
sudo firewall-cmd --reload

On Secondary node 22.22.22.22:

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="11.11.11.11" port protocol="tcp" port="27017" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="11.11.11.11" port protocol="udp" port="27017" accept'
sudo firewall-cmd --reload

Configure Hosts Files

Each member of your replica set should have a hostname that identifies it as a member of the set. This way, you’ll be able to keep your infrastructure organized at scale (for example, if you add more replica sets). In order to simplify the configuration of your replica set, add the following lines to the /etc/hosts file on each member of the replica set:

11.11.11.11    mongodb-repl-1
22.22.22.22    mongodb-repl-2

Configure MongoDB

On each of your nodes, make the following changes to your /etc/mongod.conf file:
On Primary node 11.11.11.11:

net:
  port: 27017
  bindIp: 127.0.0.1,11.11.11.11
 
replication:
  replSetName: rs0

On Secondary node 22.22.22.22:

net:
  port: 27017
  bindIp: 127.0.0.1,22.22.22.22
 
replication:
  replSetName: rs0

On each of your nodes, restart service mongod:
service mongod restart

Start Replication and Add Members
Do on Primary node 11.11.11.11.

1: Connect to the MongoDB shell:
mongo

2: From the mongo shell, initiate the replica set:
rs.initiate()

Result if success:

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "11.11.11.11:27017",
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1604206625, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1604206625, 1)
}

3: Add the other hosts to the replica set:
rs.add("mongodb-repl-2")

Result if success:

rs0:SECONDARY> rs.add("mongodb-repl-2")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1604206705, 2),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1604206705, 2)
}

4: Check the configuration of your replica set:
rs.conf()

Example result:

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 2,
        "term" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "11.11.11.11:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "mongodb-repl-2:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5f9e4021cc0ed40c03526938")
        }
}

5: Check the status of your replica set:
rs.status()

Example result:

rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-11-01T05:01:34.493Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 2,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1604206888, 2),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-01T05:01:28.217Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1604206888, 2),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-01T05:01:28.217Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1604206888, 2),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1604206888, 2),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-01T05:01:28.217Z"),
                "lastDurableWallTime" : ISODate("2020-11-01T05:01:28.217Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1604206855, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-01T04:57:05.048Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1604206625, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-01T04:57:05.066Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-01T04:57:05.083Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "11.11.11.11:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 307,
                        "optime" : {
                                "ts" : Timestamp(1604206888, 2),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-01T05:01:28Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1604206625, 2),
                        "electionDate" : ISODate("2020-11-01T04:57:05Z"),
                        "configVersion" : 2,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "mongodb-repl-2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 188,
                        "optime" : {
                                "ts" : Timestamp(1604206888, 2),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1604206888, 2),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-01T05:01:28Z"),
                        "optimeDurableDate" : ISODate("2020-11-01T05:01:28Z"),
                        "lastHeartbeat" : ISODate("2020-11-01T05:01:33.578Z"),
                        "lastHeartbeatRecv" : ISODate("2020-11-01T05:01:32.947Z"),
                        "pingMs" : NumberLong(260),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "139.180.196.39:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 2,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1604206888, 2),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1604206888, 2)
}

Leave a Reply