On this page本页内容
enableSharding
The enableSharding
command enables sharding on the specified database. enableSharding
命令在指定的数据库上启用分片。Once you enabled sharding for a database, you can use the 为数据库启用分片后,可以使用shardCollection
command to shard collections in that database.shardCollection
命令对该数据库中的集合进行分片。
The mongosh
method sh.enableSharding()
wraps the enableSharding
command.mongosh
方法sh.enableSharding()
包装了enableSharding
命令。
The enableSharding
command has the following syntax:enableSharding
命令具有以下语法:
Recommended. 推荐。 Specify just the database for which to enable sharding.仅指定要启用分片的数据库。
db.adminCommand( { enableSharding: "<database name>" } )
Optionally, starting in MongoDB 4.2.2 (and 4.0.14), you can include the primary shard specification in the command, although this is not recommended:可选地,从MongoDB 4.2.2(和4.0.14)开始,您可以在命令中包含主分片规范,但不建议这样做:
db.adminCommand( { enableSharding: "<database name>", primaryShard: "<shard name>" // Available starting in MongoDB 4.2.2 (and 4.0.14) } )
You can only run 您只能从enableSharding
in the admin
database from a mongos
instance.mongos
实例在admin
数据库中运行enableSharding
。
The command takes the following fields:该命令接受以下字段:
enableSharding | String |
|
primaryShard | String |
Tip
|
The command returns a document that contains status of the operation.该命令返回包含操作状态的文档。
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 primaryShard in the command for a database and the database is already sharding enabled with a different primary shard, the operation returns an error and the primary shard for the database remains as before. 但是,如果您在命令中为数据库指定了primaryShard,并且该数据库已经使用不同的主分片启用了分片,则该操作将返回一个错误,数据库的主分片将保持原样。To change the primary shard for a database, use 要更改数据库的主分片,请改用movePrimary
instead.movePrimary
。
The following command, run from a 从mongos
, enables sharding for the test
database:mongos
运行的以下命令启用测试数据库的分片:
db.adminCommand( { enableSharding: "test" } )
To verify, run 要进行验证,请运行sh.status()
.sh.status()
。