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) = 192.168.101.151

Secondary MongoDB IP(Mongo2) = 192.168.101.152

Third MongoDB IP(Mongo3) = 192.168.101.153

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 - https://www.mongodb.org/static/pgp/server-4.2.asc | 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 ] https://repo.mongodb.org/apt/ubuntu 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:

replication:
 replSetName: rs0


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

bind_ip = 192.168.101.151


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.initiate()
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)

rs.status()

It should show something like

rs.status()
{
        "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: ha-db1.codelathe.com:27017",
                        "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: ha-db1.codelathe.com:27017",
                        "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:
#   http://docs.mongodb.org/manual/reference/configuration-options/

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

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

# network interfaces
net:
  port: 27017
  bindIp: 192.168.101.151


#processManagement:

security:
  keyFile: /etc/mongodb-keyfile
  authorization: enabled

#operationProfiling:

replication:
 replSetName: rs0

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp: