|
MongoDB Database Management
Implementing Sharding
How do you start the config server(s)?
.
- In production have 3 separate config servers
- Add a DNS or "hosts" file host name for each config server
- Make a data folder (i.e. /data/configdb)
- mongod --configsvr --dbpath path-to-data-folder --port 27019
How do you start the mongos instance(s)?
.
- Assign a DNS or "hosts" file name to the mongos instance
- mongos --port 27018 --configdb config-server-host-1, config-server-host-2, etc.
How do you add shards to the cluster?
.
- Connect to the mongos instance:
- mongo --host mongos-hostname --port 27018
- Add shard to stand-alone MongoDB instance:
- sh.addShard("hostname:port")
- Add shard to a MongoDB replica set:
- sh.addShard("rs/hostname:port")
- Where "rs" is the replica set identifier
How do you enable sharding for a database?
.
- Connect to the mongos instance:
- mongo --host mongos-hostname --port 27018
- Enable sharding for database "db-name":
- sh.enableSharding("db-name")
How do you enable sharding for a collection
.
- Identify one or more fields to be used as the shard key
- If the collection contains data:
- Create an index on the shard key field(s): ensureIndex()
- Enable sharding for the collection:
- sh.shardCollection("db-name.collection",{shard_key_field_1: 1 [,shard_key_field_2: 1]})
How can you confirm sharding is successful?
.
- Connect to the mongos instance:
- mongo --host mongos-hostname --port 27018
- sh.status()
|
|