How to Setup a MongoDB Replica Set

Samuel Addico
CodeOps
Published in
3 min readJan 18, 2019

Introduction

In this tutorial, we are going to be looking at how setup a MongoDB replicat set including 3 nodes: One primary node and 2 secondary nodes.This tutorial also assumes you have a fair knowledge of MongoDB and Clustering.

MongoDB Installation

Well, so as the title suggests, we will have to install MongoDB on all 3 nodes.For the sake of this session, we will be using version 4.0 and on Ubuntu 16. You could follow installation guide on the official website.You should see below once MongoDB is up and running:

Configuring The Replica Set

Hosts File

To ensure that there is communication amongst the 3 nodes, we need to configure the hostname and their corresponding private IPs in the /etc/hosts as shown below :

For this update to be effected you need to reboot the nodes

Mongo Configuration File

We will update the mongo configuration on each node by ensuring they listen on the private IPs configured above.See below :

net:
port: 27017
bindIp: 172.31.24.121
replication:
replSetName: replicaSet

Also note to restart service mongod after this updates

Initialize the Replica Set

This is where we do the actual replica configurations.Connect a mongo shell to one of the running mongod instances and configure as show below.

ubuntu@mongodb-node-1:~$mongo
> rs.initiate()

You should see an output as shown below.

Next, we will add the remaining nodes to the replica configurations by running below :

> rs.add('mongodb-node-2:27017')
> rs.add('mongodb-node-3:27017')

If the replica set has been configured properly, you’ll see output similar to the following:

Congratulations !!, We have successfully configure a MongoDB replica set.

Advantages of Replication

  • High Availability : when one node or the primary node goes down, another node in the cluster can be switch to and depended on for serving of workload
  • Increased read performance : Multiple machines provide greater processing power and data can be spread across multiples machines on a network.This allows to vastly improve your application’s read performance.
  • Greater scalability: As your user base grows and report complexity increases, your resources can grow.
  • Improve data durability guarantee: Replication allows you to increase this durability guarantee by ensuring that data modifications are written to multiple machines. This means that multiple disks, disk controllers, power supplies, and CPUs are used to ensure that your data modification makes it to stable storage.

Considerations

This replica set consists of 3 nodes as you can see.However, if you decide to go with an even number say 4 members, deploy an arbiter so that the set has an odd number of voting members.Voting is usually conducted by cluster members to elect a primary node and so adding an arbiter ensures no ties are risen during voting.Hope this helps. Read more here

In my next story, we will deal with how to add authentication to this :-),Thank you for reading. Don’t forget to hit the clap icon.

--

--

Samuel Addico
CodeOps

DevOps | SRE | Java | Kubernetes | AWS | Ansible | Terraform | CI/CD