Configuring Sharding

Start the relevant processes.

  • Run mongod on the shard servers.  Use the --shardsvr command line parameter. For replica pairs, use the --pairwith command line option.  (To get started with a simple test we recommend just running a single mongod process per shard without failure.)
  • Run mongod on the config server(s) with the --configsvr command line parameter.  If the config servers are running on a shared machine other processes, assign it to separate a separate dbpath (--dbpath command line parameter).
  • Run mongos on the servers of your choice.  Specify --configdb parameter to indicate location of the config database(s).

Configure Sharding from the Shell

You may want to automate or record your steps below in a .js file for replay in the shell anytime needed.

To run these commands, connect to a mongos that was started above, and run all configuration commands through it.  Note: you must use the special admin database for these commands. Also, even though you're running through a mongos, data is stored in the config server, so these commands are only run once for the lifetime of the cluster.

./mongo <mongos-hostname>:<mongos-port>/admin
> db
admin
>
Adding a Shard

Each shard consist of either two servers (a replica pair) or a single mongod server instance.  (Alpha 2 supports single-server shards only.)  To add a shard:

> db.runCommand( { addshard : "<serverhostname>[:<port>]" } );
{"ok" : 1 , "added" : ...}

Add pairs by comma separating two server[:port] names in the addshard command (alpha 3 and beyond).

There is an optional "maxSize" parameter that lets you tell the system how much disk space to use on that shard. If you do not specify that, it will just use the entire disk.

To see existing shards:

> db.runCommand( { listshards : 1 } );
Enabling for a Database

We must enable sharding for a database -- otherwise data for the database is stored entirely on one shard.

> db.runCommand( { enablesharding : "<dbname>" } );

Once enabled, mongos will place different collections for the database on different shards.  However, unless the collection is sharded (see below), all data from one collection will be located on a single shard.

Sharding a Collection

Use the shardcollection command to shard a collection.  An index is automatically created for the specified key.

 > db.runCommand( { shardcollection : "<namespace>",
                    key : <shardkeypatternobject>
                  } )

For example, to shard the GridFS chunks collection (which is a great candidate as this can get very large) on the test database one would invoke:

 > db.runCommand( { shardcollection : "test.fs.chunks", key : { _id : 1 } } )
{"ok" : 1}

You can also make your shard key unique:

db.runCommand( { shardcollection : "test.users" , key : { email : 1 } , unique : true } );

Examples

See Also


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.

These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group.

blog comments powered by Disqus