sh.enableSharding()
On this page本页内容
Definition定义
sh.enableSharding(database, primaryShard)
- Note
Explicitly creates a database.显式创建数据库。Use the使用mongosh
methodsh.shardCollection()
to shard collections on the database.mongosh
方法sh.shardCollection()
对数据库上的集合进行分片。Themongosh
methodsh.enableSharding()
wraps theenableSharding
command.mongosh
方法sh.enableSharding()
封装enableSharding
命令。Importantmongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
enableSharding
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
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()
方法采用以下参数:
database | String | |
primaryShard | String | Warning Tip |
Behavior行为
Write Concern撰写关注事项
mongos
uses "majority"
for the enableSharding
command and its helper sh.enableSharding()
.mongos
使用"majority"
作为enableSharding
命令及其助手sh.enableSharding()
。
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 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
, explicitly creates the shardTest
database:mongos
运行,显式创建shardTest
数据库:
sh.enableSharding("shardTest")
To verify, run 要进行验证,请运行sh.status()
.sh.status()
。