Installation and Configuration of 3 server MongoDB cluster

Installation of MongoDB in 3 servers

The below steps will help you to install mongodb on an Ubuntu server, you will need to follow the same procedure in 3 servers.

Primary MongoDB IP(Mongo1) =

Secondary MongoDB IP(Mongo2) =

Third MongoDB IP(Mongo3) =

Importing the public key

The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the MongoDB public GPG Key:

Create a list file for MongoDB.

wget -qO - | sudo apt-key add - 

Create the /etc/apt/sources.list.d/mongodb-org-4.2.list list file using the command appropriate for your version of Ubuntu:

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

Reload local package database.

Issue the following command to reload the local package database:

sudo apt-get update

Install the MongoDB packages.

Install the latest stable version of MongoDB.

Issue the following command:

sudo apt-get install -y mongodb-org

Setting up Replication

You will need to edit mongo.conf at /etc/mongodb.conf in each DB node and enable DB replication. 

In case of mongodb on Linux, uncomment the line containing replication and add the replica set name as follows:

 replSetName: rs0

Important: Comment out (or remove) bind directive and add the server IP in it

bind_ip =

After making the above changes, please restart your MongoDB service

Open the mongo shell by running the mongosh command /usr/bin/mongosh 

Important Step : This applies to ONLY one node. Select a node (say Mongo1) and issue the following command. If you issue this in more than one system, the configuration will become invalid!

Initialize the replica set with the following command 

rs.add(“ip address of 2nd MongoDB server”)
rs.add(“ip address of 3rd MongoDB server”) 


In each of the three database server nodes, connect to mongo shell and enter rs.status() to see the actual value (One of the nodes should show as Primary and other two nodes should show as Secondary)


It should show something like

        "set" : "rs0",
        "date" : ISODate("2014-09-03T20:52:14Z"),
        "myState" : 2,
        "members" : [
                        "_id" : 0,
                        "name" : "<ip of other DB>:27017",                       
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "PRIMARY",
                        "uptime" : 749,
                        "optime" : Timestamp(1409777412, 1),
                        "optimeDate" : ISODate("2014-09-03T20:50:12Z"),
                        "errmsg" : "syncing to:",
                        "self" : true
                        "_id" : 1,
                        "name" : "<Ip of other DB>:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 749,
                        "optime" : Timestamp(1409777412, 1),
                        "optimeDate" : ISODate("2014-09-03T20:50:12Z"),
                        "errmsg" : "syncing to:",
                        "self" : true

Enable MongoDB Cluster Authentication

You can find more details on setting up MongoDB authentication here

You can find the sample of MongoDB configuration file below

# mongod.conf

# for documentation of all options, see:

# Where and how to store data.
  dbPath: /var/lib/mongodb
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
  port: 27017


  keyFile: /etc/mongodb-keyfile
  authorization: enabled


 replSetName: rs0


## Enterprise-Only Options: