buy
Configure Standalone MongoDB Instance to a Replica Set

Overview

This guide will show you how to convert MongoDB to to a Replica Set.

Step 1 : Stop the MongoDB Instance.

systemctl stop mongod

Step 2 : Modify the MongoDB Configuration File and add the below content to config file located at /etc/mongod.conf.

replication:
  replSetName: "rs0"
security:
  keyFile: /usr/local/apps/mongodb/etc/mongodb-keyfile

Execute the below command to generate keyFile:

openssl rand -base64 756 > /usr/local/apps/mongodb/etc/mongodb-keyfile
chown mongod:mongod /usr/local/apps/mongodb/etc/mongodb-keyfile
chmod 600 /usr/local/apps/mongodb/etc/mongodb-keyfile
rm -rf /tmp/mongodb-27017.sock

Step 3 : Start the MongoDB Instance.

systemctl start mongod

Step 4 : Execute the following command to login as admin and authenticate in mongodb shell.

mongosh
use admin
db.auth('root', 'password')

Note :  Get MongoDB password from /var/webuzo/mongodb.conf

Step 5 : Initiate the Replica Set, execute the following command.

rs.initiate()

Step 6 : Verify the Replica Set Status

rs.status()

Expected output will be the following.

rs0 [direct: other] admin> rs.status()
{
  set: 'rs0',
  date: ISODate('2024-05-27T12:13:15.252Z'),
  myState: 1,
  term: Long('1'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1716811990, i: 1 }), t: Long('1') },
    lastCommittedWallTime: ISODate('2024-05-27T12:13:10.299Z'),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1716811990, i: 1 }), t: Long('1') },
    appliedOpTime: { ts: Timestamp({ t: 1716811990, i: 1 }), t: Long('1') },
    durableOpTime: { ts: Timestamp({ t: 1716811990, i: 1 }), t: Long('1') },
    lastAppliedWallTime: ISODate('2024-05-27T12:13:10.299Z'),
    lastDurableWallTime: ISODate('2024-05-27T12:13:10.299Z')
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1716811940, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate('2024-05-27T12:11:28.340Z'),
    electionTerm: Long('1'),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1716811885, i: 1 }), t: Long('-1') },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1716811885, i: 1 }), t: Long('-1') },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long('10000'),
    newTermStartDate: ISODate('2024-05-27T12:11:29.061Z'),
    wMajorityWriteAvailabilityDate: ISODate('2024-05-27T12:11:30.326Z')
  },
  members: [
    {
      _id: 0,
      name: '127.0.0.1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 550,
      optime: { ts: Timestamp({ t: 1716811990, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-05-27T12:13:10.000Z'),
      lastAppliedWallTime: ISODate('2024-05-27T12:13:10.299Z'),
      lastDurableWallTime: ISODate('2024-05-27T12:13:10.299Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: 'Could not find member to sync from',
      electionTime: Timestamp({ t: 1716811888, i: 1 }),
      electionDate: ISODate('2024-05-27T12:11:28.000Z'),
      configVersion: 1,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1716811990, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('qAbaBIhv7Uu5hTsqz280+jjxnl8=', 0),
      keyId: Long('7373650916638982145')
    }
  },
  operationTime: Timestamp({ t: 1716811990, i: 1 })
}
rs0 [direct: primary] admin>

That's it now your MongoDB is configures as replica Set

    Was this page helpful?
    Newsletter Subscription
    Subscribing you to the mailing list