How To Configure MongoDB Replication On Ubuntu 20.04 LTS


MongoDB Replication / Replica set – In MongoDB, a copy set is a gathering of mongod measures that keep up a similar informational index. Imitation sets are the reason for all creation arrangements as they give information excess and high accessibility.


instructions to design MongoDB Replica Set on Ubuntu 18.04/20.04 worker. MongoDB is a venture class NoSQL information base framework with auto-scaling, high accessibility and superior. In a NoSQL data set, information is put away in an archive structure utilizing MongoDB BSON design. SQL articulations can’t be utilized in MongoDB to embed or recover information. 


Here you can get the bulk data insert python3 script for load testing.


In this post, We will install and configure MongoDB Cluster on ubuntu 18.04/20.04 LTS


MongoDB replication required minimum 3 nodes in the cluster, In my case i have following nodes. mongo-node-2 mongo-node-3 mongo-node-1

Step 1:  Configure DNS with MongoDB Nodes


You need to map ip address with names to resolved it via DNS, In my case i am going to use hosts file to achieve local DNS pointing.

sudo vim /etc/hosts

# Add the following ip address and hostname according to your env.    mongo-node-1    mongo-node-2    mongo-node-3


Save and exit from the vim editor.


Step 2: Install MongoDB 


Click here find the installation of MongoDB on different version of ubuntu, The following command work on ubuntu 20.04 LTS only.

Import the public key

wget -qO - | sudo apt-key add -


Add MongoDB Repository

echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list


Install MongoDB 

sudo apt-get update
sudo apt-get install mongodb-org -y

Verify the MongoDB Service

sudo systemctl start mongod && sudo systemctl status mongod


Note : Make sure you have installed mongoDB on all the 3 nodes and all the mongoDB nodes should be able to ping with defined DNS names in hosts file.

Step 3: Bind IP Address with MongoDB


You need to bind the IP address with MongoDB by using the following command.

sudo vim /etc/mongod.conf 


Add your server IP address like this :-

  port: 27017
  bindIp: localhost,


Save and exit, You need to restart the mongod service by using following commands.

sudo systemctl restart mongod.service

Verify with netstat commands like this :-

netstat  -plntu | grep 27017

You need to bind the IP Address all the MongoDB nodes.

Step 4: Set Up MongoDB Authentication


To authentication with all the MongoDB nodes, We need to generate key file by using the given command, Execute the commands on all the MongoDB nodes.


To Generate the key

Execute the following command.

cd /opt/mongodb/ && openssl rand -base64 756 > mongo-keyfile

Update the file permission and configure with mongoDB config file.

chmod 400 /opt/mongodb/mongo-keyfile && chown mongodb:mongodb /opt/mongodb/mongo-keyfile


Key Configuration with MongoDB


After getting key, We need to configure with MongoDB for authentication, Use the following command for the same.

Open the mongoDB config file with your text editor.

sudo vim /etc/mongod.conf

Add the following keyFile like this.

  keyFile: /opt/mongodb/mongo-keyfile

  replSetName: rs0

In the end save and exit from the MongoDB config file and restart the MongoDB service by using the given command.

sudo systemctl restart mongod

Step 5: Create an Administrative User


You need to create root user to manage MongoDB database, To achieve this, You need to execute the following command one by one.


To log in Mongo Shell type Mongo on linux terminal.



After that you need to choose the admin database.

use admin


and then you need to execute the MongoDB query like this to create the root user with password.


    "createUser" : "root",
    "pwd" : "YOUR_PASSWORD",
    "customData" : {

    "roles" : [
            "role" : "root",
            "db" : "admin"

After that we need to apply authentication security by following to add lines in mongoDB config files.

sudo vim /etc/mongod.conf

and add or update the below parameter

  authorization: enabled
  keyFile: /opt/mongodb/mongo-keyfile

  enableLocalhostAuthBypass: false 

Save and exit from the MongoDB config file,  To get effect you need to restart your MongoDB service by using given commands.

sudo systemctl restart mongod.service && sudo systemctl status mongod.service


Step 6:Enable MongoDB Replication


We are ready to enable MongoDB replication by using MongoDB main config file, Use the following command for the same.

sudo vim /etc/mongod.conf

Add or update the following parameter.

  replSetName: replication


Save and exit and To get effect you need to restart your mongoDB service by using given commands.

sudo systemctl restart mongod.service && sudo systemctl status mongod.service

Once MongoDB is restart, We need to login in MongoDB Shell and execute the following MongoDB query to initialization and Add MongoDB nodes.

Login MongoDB Shell

mongo -u admin -p --authenticationDatabase admin


Enable MongoDB Replication



Adding MongoDB Nodes


Step 7:Testing MongoDB Replication


Now we need to test MongoDB replication, To do that, We need to Add some sample data in MongoDB Master node, To achieve this We are going to use the for loop, Execute the given command one by one to add data.


Execute on Primary MongoDB (Master Node)

rs0:PRIMARY> use exampleDB
rs0:PRIMARY> for (var i = 0; i <= 10; i++) db.exampleCollection.insert( { x : i } )

Execute on Slave Nodes

rs0:SECONDARY> rs.slaveOk()
rs0:SECONDARY> use exampleDB
rs0:SECONDARY> db.exampleCollection.find()


We have successfully Install and configure MongoDB replication on ubuntu machine, Still you have any issue leave a comment.





How To Configure MongoDB Replication On Ubuntu 20.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top