Setup Mongos Router Server (mongos-router):

After above-mentioned steps, our Shard Config server is good to go. We will now try to start our mongo router process with the Shard Config server.

# create required directories for mongos router
$ sudo mkdir -p /srv/mongos-router/logs

# start mongodb config server process
$ sudo mongos --configdb configrs/dh-mongo-shard-config:27019 --bind_ip 0.0.0.0 --logpath /srv/mongos-router/logs/mongos-router.log --fork

Setup MongoDB Shard Node Servers (mongo-shard-node-01 & mongo-shard-node-02):

We assume that the MongoDB has already been installed with the steps mentioned previously. So we need to make some minor adjustments in order to make it ready for the cluster.

We have to edit /etc/mongod.conf replace the following line:

bindIp: 127.0.0.1

# sharding:

Replace with following:

bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr

Then restart the MongoDB process as follows:

sudo systemctl restart mongod

We have to repeat this process all the shard nodes that we have in the network.

Attach Shard Nodes to the Cluster:

As we are ready with the setup all the required nodes now let’s try to add them into the cluster as mentioned below:

$ mongo localhost:27017
MongoDB shell version v4.0.5
connecting to: mongodb://localhost:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("40647d53-afdc-415b-8f8f-b5bc7e826ba8") }
MongoDB server version: 4.0.5
Server has startup warnings: 
2019-01-21T13:38:06.863+0530 I CONTROL [main] 
2019-01-21T13:38:06.863+0530 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2019-01-21T13:38:06.863+0530 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2019-01-21T13:38:06.863+0530 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-21T13:38:06.863+0530 I CONTROL [main] 
mongos> sh.addShard("172.16.1.5:27017")
{
    "shardAdded" : "shard0000",
    "ok" : 1,
    "operationTime" : Timestamp(1548058139, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1548058139, 3),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
mongos> sh.addShard("172.16.1.6:27017")
{
    "shardAdded" : "shard0001",
    "ok" : 1,
    "operationTime" : Timestamp(1548058143, 2),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1548058143, 2),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Part 4: Test your MongoDB Shard