For More posts related to MongoDB, click on the LINK

https://www.youtube.com/watch?v=t17gydO99EY

Overview

This document provides step-by-step instructions to configure a MongoDB Replica Set using YUM on Linux systems (RHEL/CentOS/Rocky/Alma). The setup includes one Primary node, one Secondary node, and one Arbiter node.

Replica Set Architecture

Replica Set Name: rs0

Primary   : mongo1 (192.168.190.10)
Secondary : mongo2 (192.169.190.20)
Arbiter   : mongo3 (192.169.190.30)

Step 1: Configure MongoDB YUM Repository (All Nodes)

Create the MongoDB repository file at /etc/yum.repos.d/mongodb-org-5.0.repo with the appropriate MongoDB version.


[mongodb-org-5.0]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://pgp.mongodb.com/server-5.0.asc

 

Step 2: Install MongoDB (All Nodes)

Run the following command to install MongoDB packages using YUM.

yum install -y mongodb-org

sudo yum install -y   mongodb-org-5.0.23

 

2a. Prevent automatic upgrades

MongoDB strongly recommends pinning versions.

Create:

sudo yum versionlock add mongodb-org*

Or install the plugin first if needed:

sudo yum install yum-plugin-versionlock

 

Step 3: Configure mongod.conf (All Nodes)

Edit /etc/mongod.conf and ensure the network and replication sections are configured correctly.


net:
  port: 27017
  bindIp: 0.0.0.0

replication:
  replSetName: rs0

 

Step 4: Start and Enable MongoDB Service

Start MongoDB and configure it to start automatically on boot.

systemctl start mongod
systemctl enable mongod

Step 5: Configure Firewall (All Nodes)

Ensure port 27017 is open between all MongoDB nodes.

firewall-cmd –add-port=27017/tcp –permanent
firewall-cmd –reload

Step 6: Initialize Replica Set (Primary Only)

Connect to the primary node and initiate the replica set configuration.


rs.initiate({
  _id: “rs0”,
  members: [
    { _id: 0, host: “mongo1:27017” },
    { _id: 1, host: “mongo2:27017” },
    { _id: 2, host: “mongo3:27017”, arbiterOnly: true }
  ]
})

 

Step 7: Verify Replica Set

Use the following commands to verify replica set health and member roles.

rs.status()
rs.isMaster()

Step 8: Test Failover

Stop MongoDB on the primary node and confirm that the secondary node is promoted to primary automatically.

Best Practices

  • Enable authentication and keyFile
    • Use TLS/SSL encryption
    • Use odd number of voting members
    • Monitor replica set health regularly