MongoDB replication
Posted By : Ashish Sharma | 31-Jan-2022
In this blog we will discuss how to setup replica set for mongo database. As replica set are very important for data redundancy and availability. A replica set is a group of mongod instances that maintain the same data set.
But before we start replication process first we will learn how to install mongodb on ubuntu server.
MongoDb installation:
In order to start mongodb installation first we have to add key in our server
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
To check the list of keys which got added in our server use below command -
apt-key list
From the list one can chose which mongodb version he wants to install in their server, we are going to install version 4.4.
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
After running this command, update your server’s local package index so APT knows where to find the mongodb-org package:
sudo apt update
Following that, you can install MongoDB from apt package manager:
sudo apt install mongodb-org
Comman command to check MongoDB Service:
To start mongodb service
sudo systemctl start mongod
To check mongodb service status
sudo systemctl status mongod
To stop mongodb service
sudo systemctl stop mongod
To enable mongodb service so that it start automatiocally after system reboot
sudo systemctl enable mongod
Replication can be understood with below mentioned diagram, in this daigram it is depicted that how a primary node communicate with secondary node -
In case a primary server goes down due to any reason a new secondary server or replica set is elected as new primary. Below is the diagram to depict the same -
Now to start replication setup first we have to create another server and install mongodb on it. Also make sure the two server are in same network so that they can communicate with each other using private IP.
Once the secondary server is up with mongodb installed in it we have to edit the "/etc/hosts" file to add both server so that system can recognize them. We have to update there DNS hostname so that one node can reach the other node -
By using below command one can access the host file and edit it
sudo nano /etc/hosts
After this we have to update add private Ip of both our node and a domain name to easliy remember those, below is an example to do so -
172.0.0.1 mongo1
172.0.0.2 mongo2
Just add these line and in place of IP put your server IP and you can configure your servers to use whatever hostname you’d like. This has to be done on both the servers.
Enabling replication on both server:
To enable replication we have to update mongod.conf file which is located at /etc/mongod.conf, use below command to edit it -
sudo nano /etc/mongod.conf
We have used nano editor for this tutorial you can your prefered editor to edit the file. There are two things we have to edit in this configuration file
- Network Interface
- Replication
Below is the description how to do it -
- On master DB we have to update the network interface as mongo1 as it contains IP of our master node
net:
port: 27017
bindIp: 127.0.0.1,mongo1
. . .
replication:
replSetName: "rs0"
- On secondary DB we have to update the network interface as mongo2 as it contains IP of our secondary node node
net:
port: 27017
bindIp: 127.0.0.1,mongo2
. . .
replication:
replSetName: "rs0"
- But the repliac set name should be same for both the configuration
Now as we have updated both server configuration we should restart both server mongodb service and than we should open mongoshell on our master DB.
To initialize the replication we would use below command -
rs.initiate()
To add a node for this replication to proceed use below command -
rs.add("172.0.0.2:27017")
One can add multiple nodes in this configuration
After this your replication will start working and you can test replication on secondary Db, you will not be able to access the DB in secondary server directly, so to do that one have to use below command
rs.secondaryOk()
This will allow you to access the Database details.
So with this we conclude the replication process setup for MongoDB. Further more configuration can be done to secure the replica set and prmiary by enabling authorization in the configuration by adding a secret file. We will look into that in my next blog.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ashish Sharma
. Ashish is a DevOps Engineer. He holds expertise in Clouds like AWS, IONOS, Digital Ocean and also worked on tools like - Jenkins, Docker, Ansible, GIT, BItbucket, Vagrant, Nagios, Zabbix, His hobbies are playing sports and travelling.