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:将副本集转换为具有嵌入式配置服务器的分片集群可以减少:
The number of nodes required in your deployment.部署中所需的节点数。Complexity for maintaining one-shard clusters.维护一个分片集群的复杂性。
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:您不能直接将副本集转换为配置分片。要将副本集转换为嵌入式配置服务器,您必须:
Convert your replica set to a sharded cluster with a dedicated config server.将副本集转换为具有专用配置服务器的分片集群。Configure the dedicated config server to run as a config shard using the使用transitionFromDedicatedConfigServercommand.transitionFromDedicatedConfigServer命令将专用配置服务器配置为作为配置分片运行。ThetransitionFromDedicatedConfigServercommand adds the config server as a shard in the cluster.transitionFromDedicatedConfigServer命令将配置服务器作为分片添加到集群中。
Reduce the number of shards in your cluster after adding the config shard.添加配置分片后,减少集群中的分片数量。
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.以下示例将自我管理的副本集转换为配置分片,其中包含副本集中预先存在的用户数据。
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.有关完整说明,请参阅将自管理副本集转换为分片群集。
Connect to mongos连接到mongos
mongosFor 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"Run the transitionFromDedicatedConfigServer command运行transitionFromDedicatedConfigServer命令
transitionFromDedicatedConfigServer commandTo configure your dedicated config server to run as a config shard, run the 要将专用配置服务器配置为作为配置分片运行,请从管理数据库运行transitionFromDedicatedConfigServer command from the admin database:transitionFromDedicatedConfigServer命令:
db.adminCommand( {
transitionFromDedicatedConfigServer: 1
} )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 inmongosh.mongosh中运行sh.isConfigShardEnabled()方法。If the如果sh.isConfigShardEnabled()output containsenabled: true, the cluster uses a config shard. If the output containsenabled: false, the cluster does not use a config shard.sh.isConfigShardEnabled()输出包含enabled: true,则集群使用配置分片。如果输出包含enabled:false,则集群不使用配置分片。Run the在连接到listShardscommand against theadmindatabase while connected to amongosand inspect the output for a document where_idis set to"config".mongos时,对admin数据库运行listShards命令,并检查_id设置为"config"的文档的输出。If the如果listShardsoutput does not contain a document where_idis 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.moveCollection或moveChunk命令手动分发数据。
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.有关删除集群中分片的完整说明,请参阅从分片集群中删除分片。