In our previous guide, we have gone through how to install and configure MongoDB in CentOS / RHEL 7 servers. By following for redundancy and high data availability at all time, MongoDB provides a functionality known as a replica set.

Replica set are basically multiple mongod processes running on multiple servers that maintain the same data set. Replica set can have a minimum of three nodes. These multiple nodes in a replica set play different roles and all together forming one replica set.

Read more about MongoDB:

Roles in replica set:

  • Primary
  • Secondary
  • Arbiter

Primary node: It is the master server of replica set and all the read and write operations will be performed on it.

Secondary node: It will in sync with the primary node and thus maintains the copy of the same data set of primary. There can be multiple secondary nodes in one replica set. When the primary node is unavailable, there will be an election between the available secondary nodes and out of these nodes one will become the next primary node of this replica set and continues the normal operations, thus providing high data availability to the database clients.

Arbiter: Do not hold any copies of data, only votes in elections to decide which node will be the next primary. Commonly used in infrastructures where multiple secondary nodes are contesting to become the next primary, this will help in reducing the election time.

In our Setup:

We will configure at least three servers on which mongodb instances will be running.

Server1: 	        192.168.10.7			mongodb-server-1a 
Server2:		192.168.10.8			mongodb-server-1b
Server3:		192.168.10.9			mongodb-server-1c

Out of these three one of them will become the primary node and remaining two will act as secondary nodes.

Verify IP Address across all the servers using “ifconfig” and make sure to add local host entry in “/etc/hosts“.

Installation of MongoDB instance:

Follow below guide to perform the installation.

Install MongoDB by following the above method on all three servers. After a successful installation, we will now configure it on all three servers. MongoDB default config file location will be reside under ‘/etc/mongod.conf’

Configuration:

#  vi /etc/mongod.conf

We will configure it as follows for all three nodes:

  • Log File Path: /var/log/mongodb/mongod.log
  • DB Path: /var/lib/mongo
  • Port: Default is 27017
  • Bind IP: By default listens on 127.0.0.1 But we will set it to 0.0.0.0 to be able to listen on different ipv4 addresses
  • Replication: Uncomment it to enable replication and set it as:
  • replSetName: “mongodb-rs”

Note: You can name the replica set of your choice but it must be same across all members.

MongoDB Replication Configuration
MongoDB Replication Configuration

Restarting MongoDB service:

After properly configuring it on all three nodes, restart “mongodb” service on all three nodes.

# systemctl restart mongod
# systemctl status mongod

Mongo Shell, Initiate the Replica

We can now access the MongoDB via mongo shell to initiate the instance and add replica set members. You can access mongo shell by simply typing the below command on any one of the above servers.

# mongo

In mongo shell, we can initiate the replica set by the following command where “_id” contains the replica set name

   rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "mongodb-server-1a:27017" },
{ _id: 1, host: "mongodb-server-1b:27017" },
{ _id: 2, host: "mongodb-server-1c:27017" }
]
})
Initiate the Replica

We can see the confirmation that the operation is successful as:

successful Replication


Check replica set status:

We can check the current replica set status in mongo shell by:

 > rs.status() 

To check the status of any node and it’s primary or more information, simply type the below command:

 >  rs.isMaster() 

From this, we can see that our replica set has been initiated successfully. And below roles have been assigned to current replica set members:

Primary Node: mongodb-server-1a
Secondary Nodes: mongodb-server-1b and mongodb-server-1c

In mongo shell of mongodb-server-1a:

In mongo shell of mongodb-server-1b:

In mongo shell of mongodb-server-1c:


Testing High Availability of Replica Set:

To test the high availability feature of the replica set, if the primary server goes down or becomes unavailable for whatever reason, there must be an election and as a result one of the secondary servers should become the new primary.

In my case I intentionally stopped the mongodb-server-1a, thus making the replica set to hold an election and as result of this mongodb-sever-1c becomes the new primary.

Note:  While in an election, the process by which one of the secondary nodes is chosen to become the next primary node is based entirely upon the Mongodb algorithm and it chooses the best option out of all nodes at that point in time.

As you can see that after the election mongodb-server-1c becomes the new primary node of the current replica set.

Node 3 elected as Primary
Verify from Primary node3

That’s it we have successfully tested and confirmed everything in our replica setup working as expected.

Conclusion:

We have successfully deployed a highly available MongoDB cluster which has three nodes, one is primary node and the others are secondary nodes which are in sync with the primary node and maintain the same data set to provide reliability to our database infrastructure. Subscribe to our newsletter to read more about MongoDB articles. Your comments are most welcome.

Ahmad Wamiq
Working as a DevOps - Application Support Engineer with Linux & Windows System Administration, Eager to learn about new technologies.

How to Install MongoDB 4.0.5 on CentOS7 and RedHat7

Previous article

30 Yum command for Package Management in Linux

Next article

You may also like

Comments

Leave a reply

Your email address will not be published.

More in Database