How to Setup Mongo Shard

To simulate the process of production deployment of MongoDB, I used Vagrant to create a couples of VMs, and exprienced the journey of deployment mongo step by step as follow mentioned.

Mongo Shard Concepts

Before dived into the code, we can review the concepts related to Mongo Shard, it needs 3 components logically at least.

a) Config Server

The config server processes are mongod instances that store the cluster’s metadata. You designate a mongod as a config server using the –configsvr option. Each config server stores a complete copy of the cluster’s metadata.

b) Query Server

The query server are lightweight mongos instances and do not require data directories. You can run a mongos instance on a system that runs other cluster components, such as on an application server or a server running a mongod process. By default, a mongos instance runs on port 27017.

c) Shard Server

The shard server is a component which can consist of a repicaSet(multiple machines).

Config the shard

use mongo clien access any one of query server, and then execute
db.runCommand({addshard:'rep1/192.168.2.41:27040,192.168.2.42:27040'})
db.runCommand({addshard:'rep2/192.168.2.51:27040,192.168.2.52:27040'})
db.runCommand({listshards:1});
You will see the shard server has been configured.