Setting Up MongoDB Replication on CentOS: A Step-by-Step Guide By Rama Srinivas Kasilanka

MongoDB replication is a powerful feature that enhances data availability and reliability. By creating multiple copies of your data, you can ensure that your database remains operational even if one or more servers fail. In this guide, we’ll walk you through the process of setting up MongoDB replication on CentOS.

Prerequisites

Before we begin, make sure you have the following:

  • Three CentOS servers with root or sudo privileges
  • MongoDB installed on each server
  • Network connectivity between the servers

For the purpose of this guide, we'll refer to the servers as:

  • Primarymongo1.example.com (IP: 192.168.1.1)
  • Secondarymongo2.example.com (IP: 192.168.1.2)
  • Arbitermongo3.example.com (IP: 192.168.1.3)

Step 1: Configure MongoDB on Each Server

First, we need to configure each MongoDB instance to participate in a replica set.

  1. Edit the MongoDB Configuration File:

    Open the MongoDB configuration file on each server:

    bash

    sudo nano /etc/mongod.conf

    Modify the following sections:

    yaml
    net: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0"

    Ensure the bindIp is set to 0.0.0.0 or to the specific IP addresses of your servers to allow external connections, and set the replSetName to rs0 (or any name you prefer).

  2. Start MongoDB:

    Restart MongoDB to apply the changes:

    bash
    sudo systemctl restart mongod
    sudo systemctl enable mongod

Step 2: Initialize the Replica Set

  1. Connect to the Primary MongoDB Instance:

    Connect to the primary MongoDB instance using the mongo shell:

    bash
    mongo --host mongo1.example.com
  2. Initiate the Replica Set:

    In the mongo shell, initiate the replica set with the following command:


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

    This command sets up the replica set with three members: two data-bearing nodes and one arbiter.

  3. Check the Replica Set Status:

    Check the status of the replica set to ensure it was configured correctly:


    rs.status()

    You should see output indicating that the replica set is initialized and the members are recognized.

Step 3: Verify Replication

  1. Insert Data into the Primary:

    Insert some data into a collection on the primary server:


    use testDB
    db.testCollection.insert({ name: "MongoDB Replication Test" })
  2. Check Data on the Secondary:

    Connect to the secondary MongoDB instance:

    bash
    mongo --host mongo2.example.com

    Ensure the data is replicated:


    use testDB
    db.testCollection.find()

    You should see the document you inserted on the primary.

Step 4: Handling Failover

MongoDB replica sets automatically handle failover. If the primary server goes down, one of the secondaries will be elected as the new primary. You can test this by shutting down the primary server:

bash
sudo systemctl stop mongod

Check the replica set status on one of the secondaries to see the election process:

bash
mongo --host mongo2.example.com
rs.status()

Once the original primary is back online, it will rejoin the replica set as a secondary:

bash
sudo systemctl start mongod

Comments

Popular posts from this blog

Installing MongoDB on CentOS