Introduction

MongoDB is an open-source NoSQL database that stores data in flexible, document-based collections instead of traditional tables. It is designed for high performance, scalability, and high availability. A MongoDB replica set (cluster) consists of multiple MongoDB instances that work together to provide data redundancy and automatic failover.

Prerequisites

  • Root or sudo access to all MongoDB servers
  • CentOS/RHEL server with yum package manager
  • Network connectivity between all cluster nodes on port 27017
  • SSH access to each server

Implementation

Part 1: Install MongoDB

Step 1

Create the MongoDB repository file:

/etc/yum.repos.d/mongodb-org-3.2.repo

Add the following repository configuration:

[mongodb-org-3.2]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

Step 2

Install MongoDB.

yum install -y mongodb-org

Step 3

(Optional) Modify the database storage path in:

/etc/mongodb.conf

Example:

dbPath: /opt/mongodata

Step 4

Start the MongoDB service.

service mongod start

Step 5

Enable MongoDB to start automatically after reboot.

chkconfig mongod on

Step 6 (Optional)

If MongoDB needs to accept remote connections, edit:

/etc/mongodb.conf

Comment out the bindIp configuration or configure it with the required network interface.

Part 2: Configure the MongoDB Replica Set

Step 1

Verify that all cluster nodes can communicate with each other on port 27017.

Step 2

Edit the MongoDB configuration file on each server:

/etc/mongodb.conf

Add the replication configuration:

replication:

    replSetName: <ReplicationName>

Step 3

Restart the MongoDB service on every node.

service mongod restart

Step 4

Log in to the MongoDB shell on the primary server and create the replica set configuration.

conf = {

    _id: “”,

    members: [

        { _id: 0, host: “Host1IPAddress:27017” },

        { _id: 1, host: “Host2IPAddress:27017” },

        { _id: 2, host: “Host3IPAddress:27017” }

    ]

}

Step 5

Initialize the replica set.

rs.initiate(conf)

Step 6

Verify the replication status.

rs.status()

Part 3: Configure an Arbiter (Optional)

Step 1

If the server already exists in the replica set, remove it.

rs.remove(“HOSTIPADDRESS:27017”)

Step 2

Add the server as an arbiter.

rs.addArb(“HOSTIPADDRESS:27017”)

Step 3

Verify the replica set configuration.

rs.conf()

The arbiter node should display:

arbiterOnly: true

Additional Commands

View current running operations

db.currentOp()

Force the primary node to step down

rs.stepDown(300)

Check replica set status

rs.status()

Verify whether the current node is primary

db.isMaster()

Conclusion

A MongoDB replica set provides high availability by maintaining multiple copies of data across different servers. By configuring replication, initializing the replica set, and optionally adding an arbiter, administrators can improve fault tolerance and ensure automatic failover in the event of a node failure. Regularly monitoring the replica set status helps maintain a healthy and reliable MongoDB deployment.

Leave a Reply