In this article, we are going to setup MongoDB replication step by step on Ubuntu Linux.
What is Replication?
Replication is the process of synchronizing data across multiple MongoDB servers. Replication provides redundancy and increases data availability with multiple copies of data on different MongoDB servers.
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
In this mongodb replication setup step by step on linux, following are my ip addresses and their host names respectively.
192.168.152.135 mongodb1 192.168.152.141 mongodb2 192.168.152.142 mongodb3
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. 192.168.152.135 mongodb1 192.168.152.141 mongodb2 192.168.152.142 mongodb3
Step3: Configure firewall in all nodes in replica set
Now install ufw(uncomplicated firewall), if not installed by using below command.
sudo apt install ufw sudo ufw enable
Now enable the port 27017 on all replica nodes.
sudo ufw allow 27017
Verify firewall opened for port 27017 or not
sudo ufw statsu verbose
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: 192.168.152.135 #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.
For the same I made below video, please watch it and subscribe my channel.