How to set up a MongoDB Sharded Cluster

MongoDB Sharded ClusterDefinition

A sharded cluster is a set of Replica Sets (shards) whose mission is to distribute the load uniformly, so that we can scale horizontally our applications in order to work with huge amounts of data.

Advantages of a sharded cluster

The main features of a sharded cluster are:

  • Scalability: From a standalone server to distributed architectures of huge clusters (This allows us to shard our database transparently across all our shards. This increases the performance of our data processing).
  • Load balancing: Automatic data movement across different shards for load balancing. The balancer decides when to migrate the data and the destination Shard, so they are evenly distributed among all servers in the cluster. Each shard stores the data for a selected range of our collection according to a partition key.

Our sharded cluster configuration

This is the configuration we will use in our sharded cluster:

  • Three config servers (the minimum in a production environment): the mongos will query them to know the shard to which enroute the clients requires. We will allocate them in the juan-mongodbspain machine.
  • Two Replica Sets (a and b), each one with three nodes (enough to show how to set up the cluster). Each Replica Set in a separate machine, the first in juan-mongodbspain and the second one in juan-mongodbspain2.
  • Two mongos (they request the information needed by the clients to the shard that contains it). Both of them will be allocated in the juan-mongodbspain machine.

Creating directories

We can not start the services if we have not created the directories in which we will store our data. We will use these ones:

  • cfg0, cfg1 and cfg2 for the three config server
  • a0, a1 and a2 for the nodes of the Replica Set called ‘a’
  • b0, b1 and b2 for the nodes of the Replica Set called ‘b’


We will follow this order to start the instances of our sharded cluster:

  1. Config servers
  2. mongod’s
  3. mongo’s

Config servers

These are the commands we will use for the config servers:

With the ‘configsvr’ option we indicate that the service will be a config service.

As we can see, we allocate the three config servers in the juan-mongodbspain machine, for that reason we use different ports. With different machines we should not specify any ports.


In the last post we talked about how to set up a Replica Set, you can read it here:

Now it is the turn of the six nodes of the two Replica Sets. The first three nodes belong to the first Replica Set (shard) and they will be in one machine. The other three belong to the second shard and they will be in the other machine.

In the command we must specify that our mongod will belong to a sharded cluster. For this purpose we use the ‘–shardsvr’ option. 27018 is the default port for these instances.

Now the second shard, remember that it is in the other machine:


Except for administrative tasks, we will never connect ourselves to a mongod, neither to a config server. The apps will connect to the mongos which mission is to send the queries to the appropiate shards. For this reason we specify ports different to the standard in all of our instances. Therefore, our apps will only be able to connect to the database through a mongos.

This is the command:

As we can see, the first command does not specify any port. Hence, this mongos will listen at the port by default (27017).

Checking our services

Now we will check that all of our services are running:

In the other machine we have the mongod’s corresponding to the second shard:

Replica Set configurations

Now we must configure our Replica Sets (we have explained it in an earlier post). I show it only for the first Replica Set because it is identical in both of them.

Cluster configuration

The last thing we must do is to add our shards to the cluster. We will do it from the first mongos, but before doing it we can check that our cluster has not any shard yet.

Ok, let’s add our two shards to the cluster:

Now, our cluster is conformed by the two shards:

Cluster Balancer

So we can know if the balancer is active:

We have finished the setup of the sharded cluster. In a future post we will explain how to shard a collection.

In the next post we will talk about how to upgrade a stand-alone node, a Replica Set and a Sharded Cluster.

Leave a comment

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

four × one =

10 thoughts on “How to set up a MongoDB Sharded Cluster”