Blog

Lately we required to deploy cluster (sharding) environment with 2 shards.

When you're moving feature from development environment to production environment, we must to test in sharded environment, because some features failed in one locale mongos after moving from single mongod instance, specially when you're make database updates, because on some scenarios shard key usage required.

So here is short command list  to install MongoDB cluster, and show description on each step.

config servers

We need 3 config servers for redundancy. Pay attention that we can managed with one only.

# mongod --configsvr --dbpath /data/configdb1 --fork --port 27021 --pidfilepath /data/configdb1/mongod-con1.lock --logpath=/var/log/mongodb/con01.log
# mongod --configsvr --dbpath /data/configdb2 --fork --port 27022 --pidfilepath /data/configdb2/mongod-con2.lock --logpath=/var/log/mongodb/con02.log
# mongod --configsvr --dbpath /data/configdb3 --fork --port 27023 --pidfilepath /data/configdb3/mongod-con3.lock --logpath=/var/log/mongodb/con03.log

mongod

mongod are the shard nodes where the data store.

# mongod --shardsvr --dbpath /mongo1/data1 --port 27018 --logpath=/var/log/mongodb/mongod1.log --fork
# mongod --shardsvr --dbpath /mongo1/data2 --port 27019 --logpath=/var/log/mongodb/mongod2.log --fork

mongos

mongos is the routing server which the app connected to and the mongos know how to "speak" with the shards. Let's declare it.

# mongos --configdb localhost:27021,localhost:27022,localhost:27023 --port 27017 --logpath=/var/log/mongodb/mongos.log --fork

shards configuration on mongos

let's add the shards to the mongos

# mongo databaseName
> sh.addShard( "localhost:27018" )
> sh.addShard( "localhost:27019" )

sharding database and collections

The last step is to declare database and collection sharding. Field1 is the shard key.

> sh.enableSharding("databaseName")
> sh.shardCollection("databaseName.collectionName", { "field1" : 1 } );

Disable script

mongod --configsvr --dbpath /data/configdb1 --fork --port 27021 --pidfilepath /data/configdb1/mongod-con1.lock --logpath=/var/log/mongodb/con01.log --shutdown
mongod --configsvr --dbpath /data/configdb2 --fork --port 27022 --pidfilepath /data/configdb2/mongod-con2.lock --logpath=/var/log/mongodb/con02.log --shutdown
mongod --configsvr --dbpath /data/configdb3 --fork --port 27023 --pidfilepath /data/configdb3/mongod-con3.lock --logpath=/var/log/mongodb/con03.log --shutdown
# mongod:
mongod --shardsvr --dbpath /mongo1/ --port 27018 --logpath=/var/log/mongodb/mongod1.log --fork --shutdown
mongod --shardsvr --dbpath /mongo3/ --port 27019 --logpath=/var/log/mongodb/mongod2.log --fork --shutdown
# mongos:
killall mongos

Notes

  • You can use all commands in one script to make it easy to turn it on and off.
  • All commands work on 2.4 and 2.6.
    • You can downgrade and upgrade to test features on both environments.

That's all...

If you've more tips, we would like you to add in post comments.

Brands

SIGN UP FOR OUR NEWSLETTER

Start receiving monthly updates on new features and the latest industry trends.

Invalid Name
Invalid Email
Invalid Input