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:
- Primary:
mongo1.example.com
(IP: 192.168.1.1) - Secondary:
mongo2.example.com
(IP: 192.168.1.2) - Arbiter:
mongo3.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.
Edit the MongoDB Configuration File:
Open the MongoDB configuration file on each server:
bashsudo nano /etc/mongod.conf
Modify the following sections:
yamlnet: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0"
Ensure the
bindIp
is set to0.0.0.0
or to the specific IP addresses of your servers to allow external connections, and set thereplSetName
tors0
(or any name you prefer).Start MongoDB:
Restart MongoDB to apply the changes:
bashsudo systemctl restart mongodsudo systemctl enable mongod
Step 2: Initialize the Replica Set
Connect to the Primary MongoDB Instance:
Connect to the primary MongoDB instance using the mongo shell:
bashmongo --host mongo1.example.comInitiate 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.
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
Insert Data into the Primary:
Insert some data into a collection on the primary server:
use testDBdb.testCollection.insert({ name: "MongoDB Replication Test" })
Check Data on the Secondary:
Connect to the secondary MongoDB instance:
bashmongo --host mongo2.example.comEnsure the data is replicated:
use testDBdb.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:
bashsudo systemctl stop mongod
Check the replica set status on one of the secondaries to see the election process:
bashmongo --host mongo2.example.comrs.status()
Once the original primary is back online, it will rejoin the replica set as a secondary:
bashsudo systemctl start mongod
Comments
Post a Comment