Uncover the infinite in IT

Table of Contents
< All Topics

MongoDB Replica Set

Set up a MongoDB Replica Set for automatic failover.

Prerequisites

  • Two MongoDB servers with static IPs.

Step-by-Step Guide

Step 1: Install MongoDB on Both Servers

1. Install MongoDB:

sudo apt install mongodb -y
Step 2: Configure MongoDB for Replica Set

1. Edit mongod.conf on each server:

sudo nano /etc/mongod.conf

Add the following under replication:

replication:
  replSetName: "rs0"

2. Restart MongoDB:

sudo systemctl restart mongod
Step 3: Initialize the Replica Set

1. Open MongoDB Shell on the primary server:

mongo

2. Initiate the Replica Set:

rs.initiate()

3. Add the Secondary Server:

rs.add("<SECONDARY_IP>:27017")