MongoDB is an open-source document-oriented non-relational database management system used for high volume data storage, it uses collections and documents unlike tables and rows in relational database, documents consist of key-value pairs which are mongo dB’s basic unit of data, fields in a MongoDB document can be indexed with primary and secondary index’s.
Replica Set
we can achieve high availability for data stored in MongoDB using replica sets, a replica set is a group of mongod instances that host the same data set each replica set will have one primary node , replication happens from primary node to secondary nodes and any node from replica set can act as primary, if a primary node fails all the available nodes will vote to select a primary node and once the failed node(previous primary) recovers it will join replica set as secondary node, a replica set can have maximum number of 50 nodes with only 7 voting members in it.
Arbiter Node
Arbiter Node is a MongoDB instance which you can add to a replica set, it participates in selecting primary node in case of a failover but will not hold any data, let’s consider a scenario to understand the need of a arbiter node in a replica set. consider you have a replica set with two nodes one acting as a primary and the other is secondary if the primary goes down mongodb will not be able to choose new primary as it need majority of members (at least two active nodes) to choose primary node.
Note: only one arbiter node should be added to a replica set.
Consider below given table to understand the majority required for electing a primary node in case of a failure, fault tolerance is number of nodes that can go unavailable.
Steps to create a Replica Set
In this blog we will see how to create a replica set with 3 nodes in which two will be functional and the other will be a arbiter node.
Step-1:
before going ahead with creating replication allow traffic on port 27017 on your machine using firewall command, also if you are using any cloud instances add a firewall rule/ security group allowing traffic to port 27017, MongoDB runs on port 27017 by default, allow traffic on the port number that you are using if you’re not using the default one.
Ensure your system has the firewalld installed, execute below command on your terminal.
sudo yum install firewalld -y
use below given command if firewalld has been installed now.
sudo systemctl enable --now firewalld
use below given command to open port 27017 for traffic/connections, if you are using any other port to launch your mongod instance use it in the command instead of 27017.
firewall-cmd --zone=public --add-port=27017/tcp --permanent && firewall-cmd --reload
Step-2:Start each member of the replica set with –replSet().
Stop already running mongodb server instances using db.shutdownSrver(); against admin database on your mongo shells
use admin; db.shutdownServer();
now start the mongodb servers on all the machines using below given command
mongod –bind_ip localhost,<hostname(s)|ip address(es)> –replSet “rs0”
mongod --bind_ip 127.0.0.1,172.31.42.102 --replSet rs0 --dbpath /var/lib/mongo
–bind_ip by default mongodb listens to localhost only, but to achieve accessibility to from network / networks use your ip-addresses along with local host.
–replSet give a name for your replica set here, if your application runs on more than one replica set each set should have a different name.
Step-3: Initiate and add mongodb instances to the replica set
from only one of your machines where mongod instances are running open a new terminal and enter “mongo” to connect to mongo shell(this will be your primary node).
Now to initiate replica set use below given below command, I am going to initiate a replica set with only two mongodb server node and leaving third one to add it a as a arbiter node in the next step.
rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "ip-172-31-41-107.ap-south-1.compute.internal:27017" }, { _id: 1, host: "ip-172-31-41-117.ap-south-1.compute.internal:27017" }, ] })
here id:”rs0″ is the name of the replica set I gave while starting mongodb servers id:0,id:1 are id’s that I am giving to the mongod servers in replica set, give a unique id to each of your servers, and for hosts I am using hostname of my machines along with port number that my mongod servers are running on (27017).
now use below given commands to check the configuration and status of your replication.
rs.config(); rs.status() ;
then on your secondary node connect to your mongo shell and run below command to allow read operations.
rs.secondaryOk();
Let’s check our configuration now, in the below snapshot left one is my primary and on the right is secondary I created a data base named “my_db” and checked for it on the secondary server ,mongodb does not show databases until you insert some data into it.
Step-4: Add Arbiter node to your replica set.
On your primary node set write concern using below operation, write concern request acknowledgement for write operations form specified number of mongod instances, as I will have two functional nodes and one arbiter in my replica set, I am going to set my write concern to two using below operation.
use below operation under admin database to set write concern.
db.adminCommand({ "setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : 2 } })
To add arbiter node to your replica set use below command.
rs.addArb("ip-172-31-36-253.ap-south-1.compute.internal:27017")
rs.addArb() is a function to add arbiter node to your replica set, here”ip-172-31-33-133.ap-south-1.compute.internal” is host name of my third machine and 27017 is port number on which mongodb server is running on.
Please Contact us for Virtual CTO Services to learn more about IT Staffing Services in India, Hire Software Developers in India, product development or cloud Infrastructure engineering services. We can also help you hire most experienced software developers on part-time/full-time/project basis.