“How to configure replica set in MongoDB | RHEL | 3 nodes”

It Skills INC:

In this tutorial, we’ll learn how to configure a replica set with 3 nodes. 

Mongodb replication setup step by step on linux

 

MongoDB replication setup is divided into 6 steps:

1. Keep data backup /etc/hosts and /etc/mongod.conf
2. Configure hosts/
3. Configure firewall
4. Configure MongoDB Replica Set
5. Initiate Replication
6. Test the replication

 

Step1: Keep data backup /etc/hosts and /etc/mongod.conf


cp /etc/hosts hosts_before_mod


cp /etc/mongod.conf mongod_before_mod

 

Step2: Configure hosts


Edit the /etc/hosts file in all replica servers and add below lines.

vi /etc/hosts


Then add below lines and save hosts file.
<ipaddress-node1> mongodb1 #PRIMARY
<ipaddress-node2> mongodb2 #Secondary
<ipaddress-node3> mongodb3 #Secondary

 

Step3: Configure firewall in all nodes in replica set
iptables -A INPUT -s <ip-address> -p tcp –destination-port 27017 -m state –state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d <ip-address> -p tcp –source-port 27017 -m state –state ESTABLISHED -j ACCEPT

 

Step4: Configure MongoDB Replica Set


This can be done by modifying the /etc/mongod.conf configuration file. In this step, we add bindIp and replica set name.

vi /etc/mongod.conf

Then add the ip address of your host to the field bindIp

Before modification of bindIp

# network interfaces
net:
port: 27017
bindIp: 127.0.0.1

After modification of bindIp

# network interfaces
net:
port: 27017
bindIp: <ipaddress-node1> #127.0.0.1

Now enable replication and add replica set name:

Before replica set name added:

#replication:

After replica set name added: Remove the hash mark and field replSetName. replSetName is case sensitive.

replication:
replSetName: “r2schools”

Important note: There should be single space after colon(:) and two spaces before the replSetName

repeat the above steps of 4th step in all replica nodes.

Step5: Initiate Replication on three nodes
We have to restart MongoDB servers on all three nodes by using below command:

systemctl restart mongod.service

Connect to the mongodb servers.

Then execute the command:

rs.initiate()

Press Enter twice.

After this add another nodes to replica set.

rs.add(“192.168.152.141”)
rs.add(“192.168.152.142”)

Now verify the replication status:

rs.status()

In the secondary servers execute below command:

rs.salveOk()

Test the replication
Now create a database on primary server. Then create a collection in that database and test this changes have been updated in the secondary servers or not.

Goto Primary node.

a) Create a database

r2schools:PRIMARY> use test
switched to db test
r2schools:PRIMARY> db
test
r2schools:PRIMARY>

b) Create a collection in the above database:

db.eclhur.insert({“name”:”Elchuru”})

Then run the command

show dbs

c) Now switch to Secondary nodes and type show dbs. If the new database reflected in the seconday nodes then replication setup is successfull otherwise something went wrong and need to troubleshooted.

r2schools:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB


New database replicated in the secondary note. So replication test successful.

So in this article, we have covered mongodb replication setup step by step on linux successfully.