MongoDB Replication Configuration
*** Steps to configure replication on a single Server (Windows) ***
Create the required directories before startup the mongod services
Step-1: Start the mongod services using a different port and data directory
open three different "cmd" terminals and runs the below commands on each terminal
mongod.exe --port 27018 --dbpath C:\mongodb\mongodb_P --replSet rs0 --oplogSize 128
mongod.exe --port 27019 --dbpath C:\mongodb\mongodb_S1 --replSet rs0 --oplogSize 128
mongod.exe --port 27020 --dbpath C:\mongodb\mongodb_S2 --replSet rs0 --oplogSize 128
Step-2: Connect to mongo shell using port numbers
mongo --port 27019
mongo --port 27018
mongo --port 27020
Step-3: configure the replica set using the below command "rsconf"
rsconf={
_id:"rs0",
members:[
{
_id:0,
host:"localhost:27018"
},
{
_id:1,
host:"localhost:27019"
},
{
_id:2,
host:"localhost:27020"
}
]
}
Output:
MongoDB > rsconf={
_id:"rs0",
members:[
{
_id:0,
host:"localhost:27018"
},
{
_id:1,
host:"localhost:27019"
},
{
_id:2,
host:"localhost:27020"
}
]
}
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "localhost:27018"
},
{
"_id" : 1,
"host" : "localhost:27019"
},
{
"_id" : 2,
"host" : "localhost:27020"
}
]
}
Step-4: Initiate the replication from any host.
rs.initiate(rsconf);
Output:
MongoDB > rs.initiate(rsconf);
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1594292126, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1594292126, 1)
}
Step-5: view the replication configuration
rs.conf();
Output:
MongoDB Enterprise rs0:PRIMARY> rs.conf();
{
"_id" : "rs0", --------------------------------------- replica set name "rs0"
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27020",
"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("5f06f799088a2096663c245b")
}
}
Replication Health check
rs.printReplicationInfo()
MongoDB rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size: 128MB
log length start to end: 5212secs (1.45hrs)
oplog first event time: Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time)
oplog last event time: Thu Jul 09 2020 17:52:18 GMT+0530 (India Standard Time)
now: Thu Jul 09 2020 17:52:20 GMT+0530 (India Standard Time)
db.getReplicationInfo()
MongoDB rs0:PRIMARY> db.getReplicationInfo()
{
"logSizeMB" : 128,
"usedMB" : 0.07,
"timeDiff" : 5232,
"timeDiffHours" : 1.45,
"tFirst" : "Thu Jul 09 2020 16:25:26 GMT+0530 (India Standard Time)",
"tLast" : "Thu Jul 09 2020 17:52:38 GMT+0530 (India Standard Time)",
"now" : "Thu Jul 09 2020 17:52:45 GMT+0530 (India Standard Time)"
}
rs.conf()
MongoDB rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27020",
"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("5f06f799088a2096663c245b")
}
}
rs.status()
MongoDB rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2020-07-09T12:23:42.107Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
"appliedOpTime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-07-09T12:23:38.436Z"),
"lastDurableWallTime" : ISODate("2020-07-09T12:23:38.436Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1594297418, 1),
"lastStableCheckpointTimestamp" : Timestamp(1594297418, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-07-09T10:55:37.887Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1594292126, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-07-09T10:55:38.027Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-07-09T10:55:38.771Z")
},
"members" : [
{
"_id" : 0,
"name" : "localhost:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 6159,
"optime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-09T12:23:38Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1594292137, 1),
"electionDate" : ISODate("2020-07-09T10:55:37Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "localhost:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5295,
"optime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-09T12:23:38Z"),
"optimeDurableDate" : ISODate("2020-07-09T12:23:38Z"),
"lastHeartbeat" : ISODate("2020-07-09T12:23:41.221Z"),
"lastHeartbeatRecv" : ISODate("2020-07-09T12:23:40.209Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27018",
"syncSourceHost" : "localhost:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "localhost:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5295,
"optime" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1594297418, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-07-09T12:23:38Z"),
"optimeDurableDate" : ISODate("2020-07-09T12:23:38Z"),
"lastHeartbeat" : ISODate("2020-07-09T12:23:41.224Z"),
"lastHeartbeatRecv" : ISODate("2020-07-09T12:23:40.200Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27018",
"syncSourceHost" : "localhost:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1594297418, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1594297418, 1)
}
rs.isMaster()
MongoDB Enterprise rs0:PRIMARY> rs.isMaster()
{
"hosts" : [
"localhost:27018",
"localhost:27019",
"localhost:27020"
],
"setName" : "rs0",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost:27018",
"me" : "localhost:27018",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1594297498, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2020-07-09T12:24:58Z"),
"majorityOpTime" : {
"ts" : Timestamp(1594297498, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2020-07-09T12:24:58Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-07-09T12:24:58.854Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 1,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1594297498, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1594297498, 1)
}
rs.slaveok()
Comments
Post a Comment