In our previous tutorials, we have explained how important is NoSQL database and MongoDB in modern-day projects and also we have covered the step by step installation guide for MongoDB 4.0 on Ubuntu 18.04. But the standalone installation is not always sufficient for large scale application having extremely high user and data volume. In order to support high volume use cases MongoDB has a concept called SHARD. It’s nothing but few MongoDB system clustered together to provide increased storage and computing power, similar to concepts of distributed computing.

Today we will learn how to setup MongoDB 4.0 Sharded cluster. Before going to the setup let’s look into the hardware system requirements to setup a shard.

A MongoDB Shard consists of 3 unique components as follows:

  • mongos Router: It distributes the traffic between the machines based on the provided configuration.
  • mongo Shard Config: It provides information to the router about the available shard nodes.
  • mongo Shard node:  These are the systems where the actual data is stored.

Installation & Basic Setup:

We have created 4 virtual machines to run our tests for the Sharded Cluster setup as mentioned below:

  • mongos Router:
    • Hostname: mongos-router
    • Local IP: 172.16.1.3
  • mongo Shard Config:
    • Hostname: mongo-shard-config
    • Local IP: 172.16.1.4
  • mongo Shard Node 01:
    • Hostname: mongo-shard-node-01
    • Local IP: 172.16.1.5
  • mongo Shard Node 02:
    • Hostname: mongo-shard-node-02
    • Local IP: 172.16.1.6

First, we will install MongoDB latest version to all the server as mentioned in the previous tutorial. But we don’t have to start MongoDB process on the “mongos Router” and “mongo Shard Config” machines.

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list
$ sudo apt update
$ sudo apt install mongodb-org

Part 2: Setup Shard Config Server (mongo-shard-config)