Start the relevant processes.
Configure Sharding from the ShellYou 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 ShardEach 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 DatabaseWe 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 CollectionUse 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 |

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