sh.enableSharding()

On this page本页内容

Definition定义

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

Syntax语法

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

Parameter参数

The sh.enableSharding() method takes the following parameter:sh.enableSharding()方法采用以下参数:

Parameter参数Type类型Description描述
databaseString

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)开始提供

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

Example示例

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

←  sh.enableAutoSplitsh.getBalancerHost() →