MongoDB replica set

Introduction

MongoDB is a popular NoSQL database. It’s a document database meaning it stores data in JSON-like format. What if our database is down for some reason? How will we serve the users in the meantime? We need a system that is scalable, highly available, has better performance, ensures data durability. Replica set comes to the rescue here.

In this article, we will discuss what replica set is and how to create a replica set in MongoDB.

Replica set

Replica set in MongoDB are instances of mongod processes that have the same data. Whenever data is written in any of the databases that are immediately replicated in the other databases in the replica sets. Usually, there is one primary node, and the rest of the nodes act as secondary nodes or as arbiters. Unlike the primary and secondary nodes, arbiters don’t store data. They participate in the election for primary only. There may be up to 50 nodes in a replica set.

Now we will see how we can set up a replica set of three nodes.

Installation

We will be using CentOs and MongoDB-4.4 version for this tutorial.

Configure the package management system (yum)

Create a repo file so that you can install MongoDB directly using yum:

  • First go to /etc/yum.repos.d directory:
cd /etc/yum.repos.d/
  • Create a new file (In this case mongodb-org-4.4.repo). Now add the following lines to the file.
[mongodb-org-4.4]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Install the MongoDB packages

To install the latest stable version of MongoDB, issue the following command:

sudo yum install -y mongodb-org

Run MongoDB as a service

  • To start mongo as daemon(background process):
sudo systemctl start mongod
  • To check status:
sudo systemctl status mongod
  • To stop:
sudo systemctl stop mongod
  • Go to mongo console:
mongo

Setting up replica set

For all the members in the replica set open /etc/mongod.conf file & edit the followings:

replication:
replSetName: “REPLICA_NAME”
net:
bindIp: localhost,OTHER_IP_ADDRESS/HOST_NAME (0.0.0.0 for allowing all ips)

Reload the systemd daemon:

sudo systemctl daemon-reload

Now check the status of mongod:

sudo systemctl status mongod

If everything is ok then proceed to initiate the replica set.

Initiating the replica set

First, we need to initiate the replica set. From any of the servers, you need to initiate it. To initiate, go to mongo console and run the following commands():

rs.initiate( {
_id : "REPLICA_NAME",
members: [
{ _id: 0, host: "mongodb0.example.net:27017" },
{ _id: 1, host: "192.168.40.5:27017" },
{ _id: 2, host: "mongodb2.example.net:27017" }
]
})

You can use both IP and hostnames to initiate. Now the replica set is established and running.

To check the config of the replica set:

rs.conf()

Try some CRUD operations to verify data is consistent on all the nodes.

To shutdown any server of the replica set. Go to mongo console of that server and run:

use admin
db.shutdownServer()

If any of the nodes are down by chance one of the secondary nodes is elected as primary. We can also change the primary node:

rs.stepDown();

Notes

  1. In /etc/mongod.conf file never use 0.0.0.0 for ip_bind option it will allow all ips to interact with the db.
  2. The article assumes all default options for initial configuration. You can change the port number, add authorization from the /etc/mongod.conf file. Always verify mongod is running fine after any change in this file.
  3. With version change, commands and configurations for mongo change a lot. To stop updating the version after installation, add the line in the /etc/yum.conf file:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store