发布于 2015-09-14 14:49:30 | 192 次阅读 | 评论: 0 | 来源: 网络整理
You add shards to a sharded cluster after you create the cluster or anytime that you need to add capacity to the cluster. If you have not created a sharded cluster, see 片式集群部署.
When adding a shard to a cluster, you should always ensure that the cluster has enough capacity to support the migration without affecting legitimate production traffic.
In production environments, all shards should be replica sets.
You interact with a sharded cluster by connecting to a mongos instance.
From a mongo shell, connect to the mongos instance. Issue a command using the following syntax:
mongo --host <hostname of machine running mongos> --port <port mongos listens on>
For example, if a mongos is accessible at mongos0.example.net on port 27017, issue the following command:
mongo --host mongos0.example.net --port 27017
Add each shard to the cluster using the sh.addShard() method, as shown in the examples below. Issue sh.addShard() separately for each shard. If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.
Optional
You can instead use the addShard database command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, see addShard.
The following are examples of adding a shard with sh.addShard():
To add a shard for a replica set named rs1 with a member running on port 27017 on mongodb0.example.net, issue the following command:
sh.addShard( "rs1/mongodb0.example.net:27017" )
在 2.0.3 版更改.
For MongoDB versions prior to 2.0.3, you must specify all members of the replica set. For example:
sh.addShard( "rs1/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )
To add a shard for a standalone mongod on port 27017 of mongodb0.example.net, issue the following command:
sh.addShard( "mongodb0.example.net:27017" )
注解
It might take some time for chunks to migrate to the new shard.