Database Manual / Sharding / Administration / Config Shard

Convert a Replica Set to a Sharded Cluster with an Embedded Config Server使用嵌入式配置服务器将副本集转换为分片集群

Starting in MongoDB 8.0, you can configure a config server to store your application data in addition to the usual sharded cluster metadata. 从MongoDB 8.0开始,您可以配置配置服务器来存储应用程序数据以及通常的分片集群元数据。A config server that stores application data is called a config shard or embedded config server.存储应用程序数据的配置服务器称为配置分片或嵌入式配置服务器。

Converting your replica set into a sharded cluster with an embedded config server can reduce:将副本集转换为具有嵌入式配置服务器的分片集群可以减少:

About this Task关于此任务

You can't directly convert a replica set to a config shard. To convert a replica set to an embedded config server, you must:您不能直接将副本集转换为配置分片。要将副本集转换为嵌入式配置服务器,您必须:

Access Control访问控制

If access control is enabled, the transitionFromDedicatedConfigServer command requires the transitionFromDedicatedConfigServer authorization action for the cluster.如果启用了访问控制,则transitionFromDedicatedConfigServer命令要求对群集执行transitionFromDedicatedConfigServer授权操作。

The clusterManager role has the transitionFromDedicatedConfigServer authorization action and can be assigned to a user.clusterManager角色具有从clusterManager授权操作转换,可以分配给用户。

Steps步骤

The following example converts a self-managed replica set to a config shard that contain pre-existing user data from the replica set.以下示例将自我管理的副本集转换为配置分片,其中包含副本集中预先存在的用户数据。

1

Convert your replica set to a sharded cluster with a dedicated config server使用专用配置服务器将副本集转换为分片集群

This tutorial assumes that you know how to convert your self-managed replica set to a sharded cluster. 本教程假设您知道如何将自我管理的副本集转换为分片集群。For full instructions, see Convert a Self-Managed Replica Set to a Sharded Cluster.有关完整说明,请参阅将自管理副本集转换为分片群集

2

Connect to mongos连接到mongos

For example, to connect to your mongos instance running on host mongodb6.example.net as an admin user named admin01:例如,要以名为admin01的管理员用户连接到在主机mongodb6.example.net上运行的mongos实例:

mongosh "mongodb://admin01@mongodb6.example.net:27017"
3

Run the transitionFromDedicatedConfigServer command运行transitionFromDedicatedConfigServer命令

To configure your dedicated config server to run as a config shard, run the transitionFromDedicatedConfigServer command from the admin database:要将专用配置服务器配置为作为配置分片运行,请从管理数据库运行transitionFromDedicatedConfigServer命令:

db.adminCommand( {
transitionFromDedicatedConfigServer: 1
} )
4

Verify that the config server is now a config shard验证配置服务器现在是一个配置分片

You can confirm that a sharded cluster uses a config shard by using one of the following methods:您可以使用以下方法之一确认分片集群使用配置分片:

  • Run the sh.isConfigShardEnabled() method in mongosh. mongosh中运行sh.isConfigShardEnabled()方法。If the sh.isConfigShardEnabled() output contains enabled: true, the cluster uses a config shard. If the output contains enabled: false, the cluster does not use a config shard.如果sh.isConfigShardEnabled()输出包含enabled: true,则集群使用配置分片。如果输出包含enabled:false,则集群不使用配置分片。
  • Run the listShards command against the admin database while connected to a mongos and inspect the output for a document where _id is set to "config". 在连接到mongos时,对admin数据库运行listShards命令,并检查_id设置为"config"的文档的输出。If the listShards output does not contain a document where _id is set to "config", the cluster does not use a config shard.如果listShards输出不包含_id设置为"config"的文档,则集群不使用配置分片。

The following example runs the listShards command and tries to find a document where _id is set to "config".以下示例运行listShards命令,并尝试查找_id设置为"config"的文档。

db.adminCommand({ listShards: 1 })["shards"].find(element => element._id === "config")

In this example, the returned document has _id set to "config" which confirms that this cluster uses a config shard.在这个例子中,返回的文档将_id设置为"config",这确认了该集群使用了配置分片。

{
_id: "config",
host: "configRepl/localhost:27018",
state: 1,
topologyTime: Timestamp({ t: 1732218671, i: 13 }),
replSetConfigVersion: Long('-1')
}

Note

If the balancer is running, it automatically migrates data across the shards. Otherwise, use the moveCollection or moveChunk commands to manually distribute your data.如果平衡器正在运行,它会自动跨分片迁移数据。否则,请使用moveCollectionmoveChunk命令手动分发数据。

5

Reduce the number of shards in your cluster to one将集群中的分片数量减少到一个

To reduce the cluster to a single shard after adding the config shard, move all unsharded collections to the config shard using the moveCollection command and remove the first shard in the cluster with the removeShard command. This step reduces your cluster to a single config shard.要在添加配置分片后将集群缩减为单个分片,请使用moveCollection命令将所有未分片的集合移动到配置分片,并使用removeShard命令删除集群中的第一个分片。此步骤将集群缩减为单个配置分片。

For full instructions on removing shards in your cluster, see Remove Shards from a Sharded Cluster.有关删除集群中分片的完整说明,请参阅从分片集群中删除分片

Learn More了解更多