定义
enableShardingThe
enableShardingcommand explicitly creates a database.提示
In
mongosh, this command can also be run through thesh.enableSharding()helper method.Helper methods are convenient for
mongoshusers, 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.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
推荐。 指定要创建的数据库:
db.adminCommand( { enableSharding: "<database name>" } ) (可选)您可以在命令中包含主分片规范,但不建议这样做:
db.adminCommand( { enableSharding: "<database name>", primaryShard: "<shard name>" } )
You can only run the enableSharding command on the admin database from a mongos instance.
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
字符串 | 要使用的数据库。 如果数据库不存在,则会创建该数据库。 | |
字符串 | 可选。 数据库的主分片分片。 它是数据库中所有未分片集合的默认分片片。 警告:一般来说,您不应指定主分片。 允许集群选择主分片分片。 |
返回:
该命令返回包含操作状态的文档。
行为
写关注
mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().
主分片
In general, you should not need to specify the primaryShard in the command. 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. To change the primary shard for a database, use movePrimary instead.
例子
从mongos运行以下命令,创建shardTest数据库:
db.adminCommand( { enableSharding: "shardTest" } )