Happy Coding, Happy Life

How to Setup Mongo Shard

| Comments

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).

Mongo Shard Setup

a) Create mongo box

vagrant box add precise64 http://files.vagrantup.com/precise64.box  
setup mongo as [Mongo GUide](http://example.net/) mentioned
vagrant package --base default --output mongo_vm.box

b) Create several VMs

Please check the vagrant config file from here 3 config servers 2 query servers 2 replicaSet and each replica has 2 instances.

c) Setup the shard

1) start config servers
    (ssh to config1): mongod --configsvr --dbpath /home/vagrant/mongo-config --port 27020
    (ssh to config2): mongod --configsvr --dbpath /home/vagrant/mongo-config --port 27020
    (ssh to config3): mongod --configsvr --dbpath /home/vagrant/mongo-config --port 27020

2) start query servers
    (ssh to router1): mongos --configdb,, --port 27017 --chunkSize 2
    (ssh to router2): mongos --configdb,, --port 27017 --chunkSize 2

3) start replicaSet1
    (ssh to rep1a): mongod --dbpath /home/vagrant/mongo-data --port 27040 --replSet rep1 --oplogSize 200 --rest
    (ssh to rep1b): mongod --dbpath /home/vagrant/mongo-data --port 27040 --replSet rep1 --oplogSize 200 --rest

4) start replicaSet2
    (ssh to rep2a): mongod --dbpath /home/vagrant/mongo-data --port 27040 --replSet rep2 --oplogSize 200 --rest
    (ssh to rep2b): mongod --dbpath /home/vagrant/mongo-data --port 27040 --replSet rep2 --oplogSize 200 --rest

Config the shard

use mongo clien access any one of query server, and then execute 


    You will see the shard server has been configured.