Deploying a Sharded Cluster in MongoDB

We have implemented MongoDB on a number of past projects and continue to, as we are a MongoDB partner. We wanted to share some of the knowledge we’ve gained and pass along tutorials on how you can get the most out of MongoDB with your current or future projects. Check out this easy to follow tutorial.

Use the following sequence of tasks to deploy a sharded cluster:

A quick thing to remember for Sharding and “localhost” Addresses, if you’re using either localhost or 127.0.0.1 for the hostname of any host identifier, you need to use localost or 127.0.0.1 for every one of your host settings for all of the MongoDB instances in your cluster. This is because if you use a localhost address and a remote host address, MongoDB will work incorrectly.

Create a Config Server Database Instance

These config server processes you’ll be using are mongod instances that work to store the each cluster’s metadata. In order to do this, you’ll need to pick mongod as the config server using the –configsvr option. Each server will store a complete copy of the cluster’s metadata. When you do a production deployment, you need to deploy three config server instances on different servers, assuring that each has adequate data safety and uptime.

 

Remember that each memer of the sharded cluster has to be able to connect to every other member in the sharded cluster, each shard and config servers.

 

Make different data directories for the three config server instances. Data is stored in /data/configdb directory by default. To choose a different location create a command similar to the following:

mkdir /data/configdb

 

Begin the three config server instances:

mongod --configsvr --dbpath <path> --port <port>

By default, the port for config servers is 27019

mongod --configsvr --dbpath /data/configdb --port 27019

Remember that all config servers need to be running and available when you first create a sharded cluster.

Initiate the mongos Instances

Mongos instances are lightweight and do not need data directories. By default, a mongos instance runs on port 27017. You are able to run a mongos instance on any system that can run cluster componants like an application server or any server runing mongod processes. When you start, remember to mention the hostnames of the three config servers from earlier in either the configuration file or command line.

Issue a command to start the mongos instance by using the following:

mongos –configdb <config server hostnames>

All of the mongos in the sharded cluster will need to utilize the same configDB string with an identical host names listed in order – if you forget this step it will not function correctly.

Adding Shards to your Cluster

 

Using a mongo shell, connect to your mongos instance and issue the following command:

mongo --host <hostname of machine running mongos> --port <port mongos listens on>

As an example, if your mongos is mongos0.example.net on port 27017, you would use this command:

mongo --host mongos0.example.net --port 27017

 

You can now add each shard to the cluster by implementing the sh.addShard() method (see below). You’ll be issuing each one separately per shard, and if it is a replica set, specify the name of that replica set and member.

Check out these sxamples of adding a shard using sh.addShard():

 

Adding a shard for a replica set rs1 running on port 27017 on mongodb0.example.net:

sh.addShard( "rs1/mongodb0.example.net:27017" )

Version 2.0.3. and greater:

If you’re using any MongoDB versions prior to 2.0.3, you’ll need to specify each member of the replica set:

sh.addShard( "rs1/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )

 

When adding a shard for a standalone mongod on port 27017 of mongodb0.example.net, use this command:

sh.addShard( "mongodb0.example.net:27017" )

Begin Sharding for a Database

You’ll need to enable sharding for your collection’s database before you are able to shard a collection, which does not redistribute data. Once you’ve enabled this, MongoDB will assign a primary shard for the specific database that stores this data before the sharding commences.

Using a mongo shell, connect your mongo’s instance by issuing the following command:

mongo --host <hostname of machine running mongos> --port <port mongos listens on>

 

Next, issue the sh.enableSharding() , and specify the name of the database by implementing the following:

sh.enableSharding("<database>")

You can also enable sharding for a database using this handy command:

db.runCommand( { enableSharding: <database> } )

Sharding for a Collection

You can enable sharding on a per-collection basis.

Figure out what you’ll be using for the shard key, as it affects your efficiency for sharding. If your collection already has data, you’ll need to create an index on the shard key, by using createIndex(). If there is nothing in your collection, MongoDB will create an index as part of the sh.shardCollection().

To enable sharding for your collection, use the following command in a mongo shell:

 

sh.shardCollection("<database>.<collection>", shard-key-pattern)

Remember to replace the <database>.<collection> string with your full namespace of your database, and the full name of the collection. The shard-key-pattern is your shard key, which you specified earlier.