On this page本页内容
sh.enableSharding(database, primaryShard)
Enables sharding on the specified database. 在指定的数据库上启用分片。Once you enabled sharding for a database, you can use 一旦为数据库启用了分片,就可以使用sh.shardCollection()
to shard collections in that database. sh.shardCollection()
分片该数据库中的集合。The mongosh
method sh.enableSharding()
wraps the enableSharding
command.mongosh
方法mongosh
method sh.enableSharding()
包装了enableSharding
命令。
The sh.enableSharding()
has the following form:sh.enableSharding()
具有以下形式:
sh.enableSharding( <database>, <primary shard> // Optional. Available starting in MongoDB 4.2.2 (and 4.0.14) )
The sh.enableSharding()
method takes the following parameter:sh.enableSharding()
方法采用以下参数:
database | String |
|
primaryShard | String |
Tip
|
mongos
uses "majority"
for the enableSharding
command and its helper sh.enableSharding()
.mongos
对enableSharding
命令及其助手sh.enableSharding()
使用"majority"
。
In general, you should not need to specify the primaryShard in the command. 通常,您不需要在命令中指定primaryShard
。Instead, allow the balancer to select the primary shard.相反,允许平衡器选择主分片。
However, if you do specify the primary shard for a database and the database is already enabled for sharding with a different primary shard, the operation returns an error and the primary shard for the database remains as before. 但是,如果确实为数据库指定了主分片,并且该数据库已经启用了使用不同的主分片进行分片分割,则操作将返回错误,数据库的主分片仍将保持不变。To change the primary shard for a database, use the 要更改数据库的主分片,请改用movePrimary
command instead.movePrimary
命令。
The following example, run from a 以下示例从mongos
, enables sharding for the test
database:mongos
运行,为test
数据库启用分片:
sh.enableSharding("test")
To verify, run 要进行验证,请运行sh.status()
.sh.status()
。