Config servers store the metadata for a sharded cluster. The metadata reflects state and organization for all data and components within the sharded cluster. The metadata includes the list of chunks on every shard and the ranges that define the chunks.配置服务器存储分片集群的元数据。元数据反映了分片集群内所有数据和组件的状态和组织。元数据包括每个分片上的块列表以及定义块的范围。
The mongos instances cache this data and use it to route read and write operations to the correct shards. mongos实例缓存这些数据,并使用它将读写操作路由到正确的分片。当集群的元数据发生变化时,mongos updates the cache when there are metadata changes for the cluster, such as adding a shard. Shards also read chunk metadata from the config servers.mongos会更新缓存,例如添加分片。分片还从配置服务器读取块元数据。
The config servers also store Authentication on Self-Managed Deployments configuration information such as Role-Based Access Control or internal authentication settings for the cluster.配置服务器还存储自我管理部署的身份验证配置信息,如基于角色的访问控制或集群的内部身份验证设置。
MongoDB also uses the config servers to manage distributed locks.MongoDB还使用配置服务器来管理分布式锁。
Each sharded cluster must have its own config servers. Do not use the same config servers for different sharded clusters.每个分片集群都必须有自己的配置服务器。不要为不同的分片集群使用相同的配置服务器。
Warning
Administrative operations conducted on config servers may have significant impact on sharded cluster performance and availability. Depending on the number of config servers impacted, the cluster may be read-only or offline for a period of time.在配置服务器上执行的管理操作可能会对分片集群的性能和可用性产生重大影响。根据受影响的配置服务器的数量,集群可能在一段时间内是只读的或离线的。
Starting in MongoDB 8.0, you can configure a config server to store application data in addition to the usual sharded cluster metadata. 从MongoDB 8.0开始,您可以配置配置服务器来存储应用程序数据,以及通常的分片集群元数据。The config server is then known as a config shard. You'll learn more later on this page in the Config Shards section.配置服务器被称为配置分片。稍后,您将在配置分片部分了解更多信息。
Replica Set Config Servers副本集配置服务器
The following restrictions apply to a replica set configuration when used for config servers:当用于配置服务器时,以下限制适用于副本集配置:
Must have zero arbiters.必须没有仲裁器。Must have no delayed members.必须没有延迟成员。Must build indexes (i.e. no member should have必须构建索引(即,任何成员都不应将members[n].buildIndexessetting set to false).members[n].buildIndexes设置为false)。
Read and Write Operations on Config Servers配置服务器上的读写操作
The admin database and the config database exist on the config servers.admin数据库和config数据inventory在于配置服务器上。
Writes to Config Servers写入配置服务器
The admin database contains the collections related to the authentication and authorization as well as the other system.* collections for internal use.admin数据库包含与身份验证和授权相关的集合以及其他供内部使用的system.*集合。
The config database contains the collections that contain the sharded cluster metadata. config数据库包含包含分片集群元数据的集合。MongoDB writes data to the 当元数据发生变化时,例如在块迁移或块分割之后,MongoDB会将数据写入config database when the metadata changes, such as after a chunk migration or a chunk split.config数据库。
Users should avoid writing directly to the config database in the course of normal operation or maintenance.用户在正常操作或维护过程中应避免直接写入配置数据库。
When writing to the config servers, MongoDB uses a write concern of 在写入配置服务器时,MongoDB使用"majority"."majority"的写入关注。
Reads from Config Servers从配置服务器读取
MongoDB reads from the MongoDB从admin database for authentication and authorization data and other internal uses.admin数据库中读取身份验证和授权数据以及其他内部用途。
MongoDB reads from the MongoDB在config database when a mongos starts or after a change in the metadata, such as after a chunk migration. Shards also read chunk metadata from the config servers.mongos启动时或元数据更改后(如块迁移后)从config数据库中读取。Shards还从配置服务器读取块元数据。
When reading from the replica set config servers, MongoDB uses a Read Concern level of 从副本集配置服务器读取时,MongoDB使用"majority"."majority"的读取关注级别。
Metadata Views Must be Up-to-Date元数据视图必须是最新的
For an operation to succeed, the view of the metadata on the specific shard member must be up-to-date. The shard and the router issuing the request must have the same version of the chunks metadata.为了使操作成功,特定分片成员上的元数据视图必须是最新的。发出请求的分片和路由器必须具有相同版本的块元数据。
If the metadata is not up-to-date, the operation fails with the 如果元数据不是最新的,则操作失败,出现StaleConfig error and the metadata refresh process is triggered. Refreshing the metadata can introduce additional operational latency.StaleConfig错误,并触发元数据刷新过程。刷新元数据可能会引入额外的操作延迟。
On a secondary, a metadata refresh can take a long time if there is significant replication lag. 在辅助服务器上,如果存在明显的复制延迟,元数据刷新可能需要很长时间。For secondary reads, set 对于二次读取,设置maxStalenessSeconds to minimize the impact of replication lag.maxStalenessSeconds以最小化复制延迟的影响。
Config Server Availability配置服务器可用性
If the config server replica set loses its primary and cannot elect a primary, the cluster's metadata becomes read only. You can still read and write data from the shards, but no chunk migration or chunk splits will occur until the replica set can elect a primary.如果配置服务器副本集丢失了其主副本集并且无法选择主副本集,则集群的元数据将变为只读。您仍然可以从分片读取和写入数据,但在副本集可以选择主副本之前,不会发生块迁移或块拆分。
In a sharded cluster, 在分片集群中,mongod and mongos instances monitor the replica sets in the sharded cluster (e.g. shard replica sets, config server replica set).mongod和mongos实例监控分片集群内的副本集(例如,分片副本集、配置服务器副本集)。
If all config servers become unavailable, the cluster can become inoperable.如果所有配置服务器都不可用,集群可能无法运行。 To ensure that the config servers remain available and intact, backups of config servers are critical. 为了确保配置服务器保持可用和完整,配置服务器的备份至关重要。The data on the config server is small compared to the data stored in a cluster, and the config server has a relatively low activity load.与存储在集群中的数据相比,配置服务器上的数据很小,配置服务器的活动负载相对较低。
See A Config Server Replica Set Member Become Unavailable for more information.有关详细信息,请参阅配置服务器副本集成员变得不可用。
Sharded Cluster Metadata分片集群元数据
Config servers store metadata in the 配置服务器将元数据存储在config database.config数据库中。
Important
Always back up the 在对配置服务器进行任何维护之前,始终备份config database before doing any maintenance on the config server.config数据库。
To access the 要访问config database, issue the following command in mongosh:config数据库,请在mongosh中发出以下命令:
use config
In general, you should never edit the content of the 一般来说,您永远不应该直接编辑config database directly. The config database contains the following collections:config数据库的内容。config数据库包含以下集合:
For more information on these collections and their role in sharded clusters, see Config Database. 有关这些集合及其在分片集群中的作用的更多信息,请参阅配置数据库。See Read and Write Operations on Config Servers for more information about reads and updates to the metadata.有关元数据读取和更新的更多信息,请参阅配置服务器上的读取和写入操作。
Sharded Cluster Security分片群集安全
Use Self-Managed Internal/Membership Authentication to enforce intra-cluster security and prevent unauthorized cluster components from accessing the cluster. 使用自我管理的内部/成员身份验证来强制实施集群内安全,并防止未经授权的集群组件访问集群。You must start each 您必须使用适当的安全设置启动集群中的每个mongod in the cluster with the appropriate security settings in order to enforce internal authentication.mongod,以强制执行内部身份验证。
Starting in MongoDB 5.3, SCRAM-SHA-1 cannot be used for intra-cluster authentication. Only SCRAM-SHA-256 is supported.从MongoDB 5.3开始,SCRAM-SHA-1不能用于集群内身份验证。仅支持SCRAM-SHA-256。
In previous MongoDB versions, SCRAM-SHA-1 and SCRAM-SHA-256 can both be used for intra-cluster authentication, even if SCRAM is not explicitly enabled.在以前的MongoDB版本中,即使没有明确启用SCRAM,SCRAM-SHA-1和SCRAM-SHA-256都可以用于集群内身份验证。
See Deploy Self-Managed Sharded Cluster with Keyfile Authentication for a tutorial on deploying a secured sharded cluster.有关部署安全分片集群的教程,请参阅部署具有键文件身份验证的自我管理分片集群。
Config Shards配置分片
New in version 8.0.在版本8.0中新增。
Starting in MongoDB 8.0, you can:从MongoDB 8.0开始,您可以:
Configure a config server to store your application data in addition to the usual sharded cluster metadata. A config server that stores application data is called a config shard.配置配置服务器以存储应用程序数据以及通常的分片集群元数据。存储应用程序数据的配置服务器称为配置分片。Transition a config server between being a config shard and a dedicated config server.在配置分片和专用配置服务器之间转换配置服务器。
A cluster requires a config server, but it can be a config shard instead of a dedicated config server. Using a config shard reduces the number of nodes required and can simplify your deployment.集群需要一个配置服务器,但它可以是一个配置分片,而不是一个专用的配置服务器。使用配置分片可以减少所需的节点数量,并简化部署。
If your application has demanding availability and resiliency requirements, consider deploying a dedicated config server. A dedicated config server provides isolation, dedicated resources, and consistent performance for critical cluster operations.如果应用程序具有苛刻的可用性和弹性要求,请考虑部署专用配置服务器。专用配置服务器为关键集群操作提供隔离、专用资源和一致的性能。
You cannot use the same config shard for multiple sharded clusters.您不能对多个分片集群使用相同的配置分片。
Commands命令
To configure a dedicated config server to run as a config shard, run the 要将专用配置服务器配置为作为配置分片运行,请运行transitionFromDedicatedConfigServer command.transitionFromDedicatedConfigServer命令。
To configure a config shard to run as a dedicated config server, run the 要将配置分片配置为作为专用配置服务器运行,请运行transitionToDedicatedConfigServer command.transitionToDedicatedConfigServer命令。
Users用户
A user created on a config shard has the same behavior as a user created on a dedicated config server.在配置分片上创建的用户与在专用配置服务器上创建的用户名具有相同的行为。
Confirm use of 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')
}Downgrade Feature Compatibility Version降级功能兼容性版本
If your cluster has a config shard and you need to downgrade the feature compatibility version to earlier than 8.0, connect to 如果集群有一个配置分片,你需要将功能兼容性版本降级到8.0之前,请连接到mongos and perform this procedure:mongos并执行以下过程:
Configure a config shard to run as a dedicated config server.配置一个配置分片作为专用配置服务器运行。
To begin configuring a config shard to run as a dedicated config server, run 要开始配置配置分片作为专用配置服务器运行,请运行transitionToDedicatedConfigServer:transitionToDedicatedConfigServer:
db.adminCommand( {
transitionToDedicatedConfigServer: 1
} )List all of the databases and collections in the cluster.列出群集中的所有数据库和集合。
To list all the databases in the cluster, run要列出群集中的所有数据库,请运行listDatabases:listDatabases:db.adminCommand( { listDatabases: 1, nameOnly: true } )Exclude排除adminandconfigdatabases.admin和config数据库。For each database, list all of the collections in the database.对于每个数据库,列出数据库中的所有集合。To list all of the collections in the database, run要列出数据库中的所有集合,请运行listCollections.listCollections。db.adminCommand(
{
listCollections: 1,
nameOnly: true,
filter: { type: { $ne: "view" } }
}
)Exclude collections starting with排除从system.system开始的集合。
For each non-system collection, move the collection to a new shard.对于每个非系统集合,将该集合移动到一个新的分片。
To move the collection to a new shard, run 要将集合移动到新的分片,请运行moveCollection:moveCollection:
db.adminCommand(
{
moveCollection: "<database>.<collection>",
toShard: "<new shard>",
}
)Wait for balancer to move sharded collection data off config server.等待平衡器将分片集合数据移出配置服务器。
To verify the balancer has moved sharded collection data off the config server, run 要验证平衡器是否已将分片集合数据移出配置服务器,请再次运行transitionToDedicatedConfigServer again:transitionToDedicatedConfigServer:
db.adminCommand( {
transitionToDedicatedConfigServer: 1
} )
The response after a successful data move contains 数据移动成功后的响应包含state: "completed". state: "completed"。If the response contains 如果响应包含state: "pendingDataCleanup", wait a moment then continue calling transitionToDedicatedConfigServer until the command response contains state: "completed". state: "pendingDataCleanup",请稍候,然后继续调用transitionToDedicatedConfigServer,直到命令响应包含state: "completed"。For full response details, see 有关完整的响应详细信息,请参阅removeShard.removeShard。
Set the feature compatibility version.设置功能兼容性版本。
To set the feature compatibility version, run 要设置功能兼容性版本,请运行setFeatureCompatibilityVersion:setFeatureCompatibilityVersion:
db.adminCommand( { setFeatureCompatibilityVersion: "7.0" } )