Database Manual / Reference / Database Commands / Sharding

enableSharding (database command数据库命令)

Definition定义

enableSharding

Note

Changed in version 6.0.在版本6.0中的更改。

Starting in MongoDB 6.0, this command is not required to shard a collection.从MongoDB 6.0开始,分片集合不需要此命令。

The enableSharding command explicitly creates a database.enableSharding命令显式创建数据库。

Tip

In mongosh, this command can also be run through the sh.enableSharding() helper method.mongosh中,此命令也可以通过sh.enableSharding()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用数据库命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

  • Recommended. Specify the database to create:推荐。指定要创建的数据库:

    db.adminCommand(
    {
    enableSharding: "<database name>"
    }
    )
  • Optionally, you can include the primary shard specification in the command, although this is not recommended:您可以选择在命令中包含主分片规范,但不建议这样做:

    db.adminCommand(
    {
    enableSharding: "<database name>",
    primaryShard: "<shard name>"
    }
    )

You can only run the enableSharding command on the admin database from a mongos instance.您只能从mongos实例在admin数据库上运行enableSharding命令。

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
enableShardingString字符串

The database you want to use. The database is created if it does not exist.您要使用的数据库。如果数据库不存在,则创建该数据库。

primaryShardString字符串

Optional. 可选。The primary shard for the database. It's the default shard for all unsharded collections in the database.数据库的主分片。它是数据库中所有未分片集合的默认分片。

WARNING:警告: In general, you should not specify the primary shard. Allow the cluster to select the primary shard instead.一般来说,您不应该指定主分片。允许集群选择主分片。

Returns

The command returns a document that contains status of the operation.该命令返回一个包含操作状态的文档。

Behavior行为

Write Concern写关注

mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().mongosenableSharding命令及其辅助函数shenableSharding()中使用"majority"

Primary Shard主分片

In general, you should not need to specify the primaryShard in the command. Instead, allow the balancer to select the primary shard.一般来说,您不需要在命令中指定primaryShard。相反,允许平衡器选择主分片。

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

Example示例

The following command, run from a mongos, creates the the shardTest database:以下命令从mongos运行,创建shardTest数据库:

db.adminCommand( { enableSharding: "shardTest" } )