MongoDB Sharded ReplicaSet with GridFS

Note if you are in a hurry please read the synopses below and refer to the source code on GitHub – (start with


This blog will describe the steps in setting up a MongoDB sharded replica set for GridFS. The steps to shard or create a replica set for GridFS is no different from a regular MongoDB Database or Collection. For this blog I have used a Ubuntu Desktop Virtual Machine, though one could setup this environment on other OSs like Windows, UNIX, etc.

I will start with setting up an environment with 2 shards (shard1 and shard2),

MongoDB Sharded Replica Set with GridFS - initial setup

Illustration 1: Two Shard Replica Set


Eventually I will add one additional shard to the setup,


MongoDB Sharded Replica Set with GridFS - Add an Additional Shard

Illustration 2: Add an additional shard


I wanted to demonstrate that multiple hosts could be used here to create in a sort a distributed File System using GridFS, hence every sharded replica set node is on a different host (actually on the same host – used host mapping entries to create multiple hosts). Add the following entries to the /etc/hosts file. Note all the IPs are pointing to local host since I didn’t want to setup those many VMs yet wanted to demonstrate a multi-host setup.




Table 1: /etc/hosts snippet

I had also added extra disks to my virtual machine so I could allocate one disk to each mapped host above (using one disk for all logs for simplicity; /media/app is where mongodb is installed). See output of “df -h” command below,

/dev/sdc 7.8G 247M 7.2G 4% /media/app/dev/sdf 2.9G 176M 2.6G 7% /media/config-1/dev/sdb 2.0G 175M 1.7G 10% /media/config-2/dev/sdd 2.0G 175M 1.7G 10% /media/config-3/dev/sdh1 4.8G 363M 4.2G 8% /media/shard1_repl1

/dev/sdg 4.8G 363M 4.2G 8% /media/shard1_repl2

/dev/sdj 4.8G 363M 4.2G 8% /media/shard1_repl3

/dev/sdk 4.8G 363M 4.2G 8% /media/shard2_repl1

/dev/sdl 4.8G 363M 4.2G 8% /media/shard2_repl2

/dev/sde 4.8G 363M 4.2G 8% /media/shard2_repl3

/dev/sdn 2.4G 356M 2.0G 16% /media/shard3_repl1

/dev/sdo 2.4G 356M 2.0G 16% /media/shard3_repl2

/dev/sdp 2.4G 356M 2.0G 16% /media/shard3_repl3

/dev/sdi 7.8G 23M 7.4G 1% /media/log

Table 2: df -h output snippet


Setting up sharded replica set

Now let’s get started with initial 2 shard setup see Illustration 1: Two Shard Replica Set.




#Create directories for DB and Log
mkdir -p $dbPathDir $logPathDir



mongod –replSet $shard –logpath $logPath –dbpath $dbPathDir –bind_ip $bind_ip –port $port –shardsvr –fork

Table 3:

Invoking the script create one replica node in a shard, example,

./ shard1 shard1_repl1 3001

Table 4: Invoke –

Repeat this step for all the nodes in the sharded replica set.

Setting up Config Server

Note that the current version of MongoDB sharding requires exactly 3 config instances setup. The script to create a config server is,




mkdir -p $dbPathDir $logPathDir



mongod –logpath ${logPath} –dbpath ${dbPathDir} –bind_ip ${bind_ip} –port ${port} –configsvr –fork

Table 5:


This script can be invoked as show in the below example,

./ config-1 8001

Table 6: Invoke

Setup mongos process





mkdir -p $logPathDir


mongos –logpath $logPath –configdb $configdb –fork

Table 7:

Invoke to start the mongos process.

./ 8001 8002 8003

Table 8: Invoke

(Note the above script can be improved by taking the host names as parameters).

Putting it altogether

View the start-up script – (

For the first time run (after hosts and mounts are setup),


Table 9: Invoking for the first time

Note the TRUE parameter is significant for initial setup – this TRUE parameter must be skipped for subsequent start-ups,


Table 10: Invoking subsequently

Adding an additional Shard

To add an additional shard, you can use the same script used in the “Setting up sharded replica set” section. The shards could be running while you do this. See for an example of adding a shard to an existing environment of shared replica sets.

Stop and cleanup

The stop script is available here – it stops all the nodes in the correct sequence (including the replica set nodes on the 3rd shard) –

Finally if you have messed something up – you can run cleanup (WARNING: Cleanup will delete all your data!) –


2 thoughts on “MongoDB Sharded ReplicaSet with GridFS

  1. Hi Yazard,

    Thanks for the article. I have a couple of questions if you don’t mind? What was your use case for this setup? For example what data was you using in GridFS?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s