enableSharding

On this page本页内容

Definition定义

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命令。

Syntax语法

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

Fields字段

The command takes the following fields:该命令接受以下字段:

Field字段Type类型Description描述
enableShardingString

The database for which you wish to enable sharding.要为其启用分片的数据库。

primaryShardString

Optional. 可选。The primary shard for the database; the primary shard contains the unsharded collection for this database. 数据库的主分片;主分片包含此数据库的未分级集合。In general, rather than explicitly specifying the primary shard, it is recommended to let the balancer select the primary shard instead.通常,建议让平衡器选择主分片,而不是显式指定主分片。

Warning警告
Tip

In general, you should not need to specify the primary shard. 通常,您不需要指定主分片。Allow the balancer to select the primary shard instead.允许平衡器选择主分片。

Available starting in MongoDB 4.2.2 (and 4.0.14)从MongoDB 4.2.2(和4.0.14)开始提供

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命令及其助手sh.enableSharding()使用"majority"

Primary Shard主分片

In general, you should not need to specify the primaryShard in the command. 通常,您不需要在命令中指定primaryShardInstead, 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

Example示例

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()

←  commitReshardCollectionflushRouterConfig →