Fork me on GitHub

Joslyn Esser

MongoDB Sharding Guide: Configuration

Now that we have laid out the groundwork for our cluster and set up our shard servers on EC2, it is time to configure the shards and shard our first database!

Configure Shards

From the routing server, go ahead and connect to the admin database:

mongo admin

Add the two shard servers, changing the hostnames to your EC2 private IP addresses:

db.runCommand( { addshard : "SHARD_A_IP:27018", name : "Shard A" } );
db.runCommand( { addshard : "SHARD_B_IP:27018", name : "Shard B" } );

Shard your first database:

db.runCommand( { enablesharding : "testing" } );

Now that your database is sharded, each collection will exist on one of the two shards. Let’s shard an individual collection, using the id as our shard key:

db.runCommand( { shardcollection : "testing.teststrings", key : { _id : 1 } } )

Your documents in the sharded collection will now be distributed in chunks across Shard A and Shard B. Congratulations! Easy, right?

See it in action

Let’s populate our collection with test data and see it in action.

First, switch to the sharded database and generate a large amount test data:

use testing
testString = "";
while ( testString.length < 200000 )
    testString += "how big can we get? ";
for ( var num = 0; num < 5000; num++ ){
    db.teststrings.save( { testString : testString } );
}

Take a peek at your different chunks of data:

use config
db.chunks.find();

If you want to get really down and dirty, you can check out the latest in the changelog and see what’s been happening with your shards:

db.changelog.find().sort({_id : -1})

Are we really done?

You might be thinking to yourself, “Is that it? We’re done?” It might be hard to believe, but you now have a fully sharded mongo cluster. Need another shard? Run the build script and add it! Mongo will take care of the rest. I was amazed at how simple it is to get up and running, and it really shows one of MongoDB’s strengths: simplicity. With that said, we should probably add Replica Sets to the mix for automated failover. We’ll do just that in a later article… (Coming Soon!)

8 Comments

  1. Luis Cosio — February 18, 2011

    Are you going to continue doing the next part of the articles?

    They are really good.

  2. Eddie — March 20, 2011

    Loving the series! Please write the next article.

  3. Jeff — March 30, 2011

    Awesome tutorial – please add the next article! Really want to get replica sets working :)

  4. Diego — June 12, 2011

    Thanks! Awesome work make.
    Please write the next article =)

  5. Master — January 16, 2013

    Coming Soon! when?

  6. Praveen — December 12, 2013

    Hi Joslyn,
    We nice tutorial. I have a problem here..

    I’m trying to learn sharding in my local environment(Windows 8). As you guided I have done upto part1. Problem started after taking deep breathe. I’m unable to add shard to admin. I have also created a question in forum(https://groups.google.com/forum/#!topic/mongodb-user/4xx5onpAGsE). If you find time please have a look at it.

    Again thanks for this nice tutorial :)

    Thank you,
    Praveen Jeganathan.

  7. Patrick — October 22, 2014

    FYI: sdf has changed to xvdf smooth sailing once i figured that out

  8. f — December 03, 2015

    f

Make a Comment