Lesson 7: Configuring a Replica Set in a MongoDB Deployment / Learn

Code Summary: Configuring a Replica Set in a MongoDB Deployment

Review the following code, which demonstrates how to:

  • Retrieve the status of a mongod instance
  • Reconfigure a running replica set
  • Set the priority of a replica set
  • Add and remove members of a replica set
  • Retrieve the status of a replica set

Retrieve the Status of a mongod Instance

Use the db.hello() command to retrieve information about a replica set, including:

  • The host of each member
  • The name of the replica set
  • The name of the primary
  • The election id
  • The timestamps for when the last operation occurred and when it became durable

Here’s an example of the output:

{
topologyVersion: {
    processId: ObjectId("63fd0cd9a301ea9f0b1049d2"),
    counter: Long("6")
  },
hosts: [
    'mongod0.replset.com:27017',
    'mongod1.replset.com:27017',
    'mongod2.replset.com:27017'
  ],
  setName: 'mongodb-repl-example',
  setVersion: 1,
  isWritablePrimary: true,
  secondary: false,
  primary: 'mongod0.replset.com:27017',
  me: 'mongod0.replset.com:27017',
  electionId: ObjectId("7fffffff0000000000000003"),
  lastWrite: {
    opTime: { ts: Timestamp({ t: 1675469681, i: 1 }), t: Long("3") },
    lastWriteDate: ISODate("2023-02-04T00:14:41.000Z"),
    majorityOpTime: { ts: Timestamp({ t: 1675469681, i: 1 }), t: Long("3") },
    majorityWriteDate: ISODate("2023-02-04T00:14:41.000Z")
  },
  maxBsonObjectSize: 16777216,
  maxMessageSizeBytes: 48000000,
  maxWriteBatchSize: 100000,
  localTime: ISODate("2023-02-04T00:14:44.847Z"),
  logicalSessionTimeoutMinutes: 30,
  connectionId: 130,
  minWireVersion: 0,
  maxWireVersion: 17,
  readOnly: false,
  ok: 1,
    '$clusterTime': {
    clusterTime: Timestamp({ t: 1677542950, i: 6 }),
    signature: {
      hash: Binary(Buffer.from("fac842d36ee350c41d1aa908ba6d306f53afc514", "hex"), 0),
      keyId: Long("7148911878087901206")
    }
  },
  operationTime: Timestamp({ t: 1677542950, i: 6 })
}

Reconfigure a Running Replica Set

To reconfigure a running replica set, use rs.conf(). Assign the rs.conf() command to a variable to retrieve the replica set configuration object:

config = rs.conf()

Set the Priority of a Replica Set

Set the priority of a replica set by assigning a priority value for each member. For example:

config.members[2].priority = 10

Add a Member to a Replica Set

To add a new member to a replica set, define the new member’s _id and host name in an object. Then, push this new member to the members array in the configuration object. Here’s an example:

member = {"_id": 3, "host": "mongod3.replset.com:27017"}

config.members.push(member)

Alternatively, you can use the rs.add() wrapper followed by the host to add a member to the replica set. For example:

rs.add("mongod3.replset.com:27017")

Remove a Member from a Replica Set

To remove a member from a replica set, use the JavaScript splice() method. Pass in as arguments the index of the starting member and the number of elements being removed:

config.members.splice(1, 1)

Alternatively, you can use the rs.remove() wrapper followed by the host to remove a member from the replica set:

rs.remove("mongod1.replset.com:27017")

To apply the changes, use the rs.reconfig() command, passing in the config variable as an argument. For example:

rs.reconfig(config)

Retrieve the Status of a Replica Set

Use the db.status() command to retrieve the current status of a replica set, such as:

  • The health of each member
  • Whether a member is the primary or a secondary
  • Information about operations

Note that this is not an exhaustive list. Here’s an example of output from the db.status() command:

{
  set: 'mongodb-repl-example',
  date: ISODate("2023-02-28T00:41:35.813Z"),
  myState: 1,
  term: Long("2"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
    lastCommittedWallTime: ISODate("2023-02-28T00:41:31.541Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
    appliedOpTime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
    durableOpTime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
    lastAppliedWallTime: ISODate("2023-02-28T00:41:31.541Z"),
    lastDurableWallTime: ISODate("2023-02-28T00:41:31.541Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1677544861, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'priorityTakeover',
    lastElectionDate: ISODate("2023-02-24T00:51:51.911Z"),
    electionTerm: Long("2"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1677199900, i: 1 }), t: Long("1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1677199900, i: 1 }), t: Long("1") },
    numVotesNeeded: 2,
    priorityAtElection: 10,
    electionTimeoutMillis: Long("10000"),
    priorPrimaryMemberId: 0,
    numCatchUpOps: Long("0"),
    newTermStartDate: ISODate("2023-02-24T00:51:52.276Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-02-24T00:51:53.386Z")
  },
  electionParticipantMetrics: {
    votedForCandidate: true,
    electionTerm: Long("1"),
    lastVoteDate: ISODate("2023-02-23T22:55:27.165Z"),
    electionCandidateMemberId: 0,
    voteReason: '',
    lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1677192915, i: 1 }), t: Long("-1") },
    maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1677192915, i: 1 }), t: Long("-1") },
    priorityAtElection: 1
  },
  members: [
    {
      _id: 0,
      name: 'mongodb.repl.member.one:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 351979,
      optime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
      optimeDurable: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-02-28T00:41:31.000Z"),
      optimeDurableDate: ISODate("2023-02-28T00:41:31.000Z"),
      lastAppliedWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      lastDurableWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      lastHeartbeat: ISODate("2023-02-28T00:41:34.484Z"),
      lastHeartbeatRecv: ISODate("2023-02-28T00:41:35.787Z"),
      pingMs: Long("37"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongodb.repl.member.three:27017',
      syncSourceId: 2,
      infoMessage: '',
      configVersion: 5,
      configTerm: 2
    },
    {
      _id: 2,
      name: 'mongodb.repl.member.three:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 352657,
      optime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-02-28T00:41:31.000Z"),
      lastAppliedWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      lastDurableWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1677199911, i: 1 }),
      electionDate: ISODate("2023-02-24T00:51:51.000Z"),
      configVersion: 5,
      configTerm: 2,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 3,
      name: 'mongodb.repl.member.two:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 343537,
      optime: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
      optimeDurable: { ts: Timestamp({ t: 1677544891, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-02-28T00:41:31.000Z"),
      optimeDurableDate: ISODate("2023-02-28T00:41:31.000Z"),
      lastAppliedWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      lastDurableWallTime: ISODate("2023-02-28T00:41:31.541Z"),
      lastHeartbeat: ISODate("2023-02-28T00:41:34.720Z"),
      lastHeartbeatRecv: ISODate("2023-02-28T00:41:34.206Z"),
      pingMs: Long("62"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongodb.repl.member.one:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 5,
      configTerm: 2
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1677544891, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("65e1e3e9636a54a0af40f152c7f2f061466096a3", "hex"), 0),
      keyId: Long("7203488770547515397")
    }
  },
  operationTime: Timestamp({ t: 1677544891, i: 1 })
}