Monday , May 20 2024

How To Install MongoDB Sharded Cluster

In my previous articles, I talked about MongoDB Community Edition installation and MongoDB Sharded Cluster components in general. You can access these posts below:

How To Install MongoDB Community Edition“,

MongoDB Sharded Cluster Components

If you want to enable MongoDB Sharded Cluster Authorization you should read the below article.

How To Enable MongoDB Sharded Cluster Authorization

Also you may want to install MongoDB Sharded Cluster with Authorization, In this case you should read the below article. You should definetely read the below article. Its very detailed article, and tested step by step.

Deploy Sharded Cluster with Keyfile Access Control on Red Hat Enterprise Linux or CentOS Linux

In this section, we will install Sharded Cluster on three separate machines (mongodb1, mongodb2, mongodb3) that have the CentOS Linux operating system.

Install MongoDB Sharded Cluster

Step 1: Install Community Edition

First, create users on all three machines and install MongoDB Community Edition as I have mentioned in my previous article named “How To Install MongoDB Community Edition“.

Step 2: Specify Path Structure

You need to create a proper folder structure to store database files. MongoDB is very flexible in this regard, you can design the folder structure as you want. I decided to create /mongodb directory on each server and create shard, configuration and log folders. The path structure will be as follows:




We can summarize the structure as follows:

  • The shA, shB, and shC folders are three separate shards. The suffixes 0,1 and 2 means replica sets. As you can see, the replica set of each shard’s data resides on another server, which minimizes data loss. Even if one server becomes unavailable, the other two servers will not lose data.
  • The cfg directory will contain data of the configuration server and is available on three separate servers.
  • Log files related to processes and servers will be stored in the logs directories.
  • All three servers have shards, one config server and one router. Since we have more than one router, applications will be able to use other routers without any interruption of service in case of a problem about the router.

Step 3: Create Config Servers

Create configuration servers by specifying file paths and cfg names with the following command on all three machines.

Script for mongodb1

Run the same script on 3 servers by changing names and paths.

Step 4: Create Shard Servers

Create Shard servers with replica sets. The paths mentioned above should be used.

Script for mongodb1

Run the same script on 3 servers by changing names and paths.

Step 5: Start Routers

Routers (mongos) are started on the default port (27017) on all three machines.

Script for mongodb1

Run the same script on 3 servers..

Step 6: Check Routers Status

Use the below command  to check that the process starts correctly.

Step 7: Create Replica Set

At this stage we will first add replica set members.

Script for mongodb1 shA set

This should also be done for shB and shC (if it is to be performed on only one server; use port 27500 for shA, port 27600 for shB, port 27700 for shC).
As can be seen, one of the three members is PRIMARY and the others are SECONDARY. Read and write requests are provided on the PRIMARY member and SECONDARY members are synchronized.

Create Shards

On the servers where the primary replica member is present, shards are added with the following commands.


About Emre Gazioğlu

Leave a Reply

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