mongos

On this page本页内容

MongoDB mongos instances route queries and write operations to shards in a sharded cluster. MongoDB mongos实例将查询和写入操作路由到分片集群中的分片mongos provide the only interface to a sharded cluster from the perspective of applications. 从应用程序的角度来看,mongos提供了到分片集群的唯一接口。Applications never connect or communicate directly with the shards.应用程序从不直接与分片连接或通信。

The mongos tracks what data is on which shard by caching the metadata from the config servers. mongos通过缓存配置服务器中的元数据来跟踪哪个分片上有哪些数据。The mongos uses the metadata to route operations from applications and clients to the mongod instances. mongos使用元数据将操作从应用程序和客户端路由到mongod实例。A mongos has no persistentstate and consumes minimal system resources.mongos没有persistentstate,消耗的系统资源最少。

The most common practice is to run mongos instances on the same systems as your application servers, but you can maintain mongos instances on the shards or on other dedicated resources. 最常见的做法是在与应用服务器相同的系统上运行mongos实例,但可以在分片或其他专用资源上维护mongos实例。 See also Number of mongos and Distribution.另请参见mongos的数量和发行版

Routing And Results Process路由和结果处理

A mongos instance routes a query to a cluster by:mongos实例通过以下方式将查询路由到集群

  1. Determining the list of shards that must receive the query.确定必须接收查询的分片列表。
  2. Establishing a cursor on all targeted shards.在所有目标分片上建立游标。

The mongos then merges the data from each of the targeted shards and returns the result document. 然后,mongos合并来自每个目标分片的数据,并返回结果文档。Certain query modifiers, such as sorting, are performed on each shard before mongos retrieves the results.mongos检索结果之前,会对每个分片执行某些查询修饰符,例如排序

Changed in version 3.6.在版本3.6中更改

For aggregation operations that run on multiple shards, if the operations do not require running on the database's primary shard, these operations may route the results back to the mongos where the results are then merged.对于在多个分片上运行的聚合操作,如果这些操作不需要在数据库的主分片上运行,则这些操作可能会将结果路由回mongos,然后在mongos中合并结果。

There are two cases in which a pipeline is ineligible to run on mongos.有两种情况下,管道没有资格在mongos上运行。

The first case occurs when the merge part of the split pipeline contains a stage which must run on a primary shard. 第一种情况发生在拆分管道的合并部分包含必须在主分片上运行的阶段时。For instance, if $lookup requires access to an unsharded collection in the same database as the sharded collection on which the aggregation is running, the merge is obliged to run on the primary shard.例如,如果$lookup需要访问与正在运行聚合的分片集合位于同一数据库中的未分片集合,则合并必须在主分片上运行。

The second case occurs when the merge part of the split pipeline contains a stage which may write temporary data to disk, such as $group, and the client has specified allowDiskUse:true. 第二种情况发生在拆分管道的合并部分包含一个阶段,该阶段可能会将临时数据写入磁盘,例如$group,并且客户端已指定allowDiskUse:trueIn this case, assuming that there are no other stages in the merge pipeline which require the primary shard, the merge will run on a randomly-selected shard in the set of shards targeted by the aggregation.在这种情况下,假设合并管道中没有其他阶段需要主分片,合并将在聚合目标分片集中随机选择的分片上运行。

For more information on how the work of aggregation is split among components of a sharded cluster query, use explain:true as a parameter to the aggregate() call. 有关聚合工作如何在分片集群查询的组件之间分割的更多信息,请使用explain:true作为aggregate()调用的参数。The return will include three json objects. 返回将包括三个json对象。mergeType shows where the stage of the merge happens ("primaryShard", "anyShard", or "mongos"). mergeType显示合并阶段发生的位置(“primaryShard”、“anyShard”或“mongos”)。splitPipeline shows which operations in your pipeline have run on individual shards. splitPipeline显示管道中的哪些操作已在单个分片上运行。shards shows the work each shard has done.显示每个分片所做的工作。

In some cases, when the shard key or a prefix of the shard key is a part of the query, the mongos performs a targeted operation, routing queries to a subset of shards in the cluster.在某些情况下,当分片键或分片键的前缀是查询的一部分时,mongos会执行目标操作,将查询路由到集群中的分片子集。

mongos performs a broadcast operation for queries that do not include the shard key, routing queries to all shards in the cluster. mongos对不包含分片键的查询执行广播操作,将查询路由到集群中的所有分片。Some queries that do include the shard key may still result in a broadcast operation depending on the distribution of data in the cluster and the selectivity of the query.根据集群中数据的分布和查询的选择性,一些确实包含分片键的查询仍然可能导致广播操作。

See Targeted Operations vs. Broadcast Operations for more on targeted and broadcast operations.有关定向和广播操作的更多信息,请参阅定向操作与广播操作

Starting in MongoDB 4.4, mongos can support hedge reads to minimize latencies. 从MongoDB 4.4开始,mongos可以支持对冲读取以最小化延迟。See hedge reads for more information.更多信息请参见hedge reads

How mongos Handles Query Modifiersmongos如何处理查询修饰符

Sorting分类

If the result of the query is not sorted, the mongos instance opens a result cursor that "round robins" results from all cursors on the shards.如果查询结果未排序,mongos实例将打开一个结果游标,该游标将从分片上的所有游标中生成“round robins”。

Limits限制

If the query limits the size of the result set using the limit() cursor method, the mongos instance passes that limit to the shards and then re-applies the limit to the result before returning the result to the client.如果查询使用limit()游标方法限制结果集的大小,mongos实例会将该限制传递给分片,然后在将结果返回给客户端之前将该限制重新应用于结果。

Skips跳过

If the query specifies a number of records to skip using the skip() cursor method, the mongos cannotpass the skip to the shards, but rather retrieves unskipped results from the shards and skips the appropriate number of documents when assembling the complete result.如果查询使用skip()游标方法指定要跳过的记录数,mongos无法将跳过传递给分片,而是从分片中检索未跳过的结果,并在组装完整结果时跳过适当数量的文档。

When used in conjunction with a limit(), the mongos will pass the limit plus the value of the skip() to the shards to improve the efficiency of these operations.当与limit()结合使用时,mongos会将limit加上skip()的值传递给分片,以提高这些操作的效率。

Read Preference and Shards阅读偏好和分片

For sharded clusters, mongos applies the read preference when reading from the shards. 对于分片集群,mongos在读取分片时应用读取首选项The member selected is governed by both the read preference and replication.localPingThresholdMs settings, and is re-evaluated for each operation.所选成员受读取首选项replication.localPingThresholdMs设置的控制,并针对每个操作重新评估。

For details on read preference and sharded clusters, see Read Preference and Shards.有关读取首选项和分片群集的详细信息,请参阅读取首选项和分片

Hedged Reads模糊阅读

Starting in version 4.4, mongos instances can hedge reads that use non-primary read preferences. 从4.4版开始,mongos实例可以对冲使用非主读取首选项的读取。With hedged reads, the mongos instances route read operations to two replica set members per each queried shard and return results from the first respondent per shard. 对于对冲读取,mongos实例将读取操作路由到每个查询的分片的两个副本集成员,并从每个分片的第一个响应者返回结果。The additional read sent to hedge the read operation uses the maxTimeMS value of maxTimeMSForHedgedReads.为对冲读取操作而发送的额外读取使用maxTimeMSForHedgedReadsmaxTimeMS值。

Hedged reads are supported for the following operations:以下操作支持模糊读取:

Hedged Reads and Read Preference模糊阅读与阅读偏好

Hedged reads are specified per operation as part of the read preference. 作为读取首选项的一部分,每个操作都会指定模糊读取。Non-primary read preferences support hedged reads. 非主要阅读首选项支持模糊阅读。See Hedged Read Preference Option.请参阅模糊阅读首选项选项

For details on read preference and sharded clusters as well as member selection, see Read Preference and Shards.有关读取首选项和分片群集以及成员选择的详细信息,请参阅读取首选项和分片

Enable/Disable Support for Hedged Reads启用/禁用对模糊读取的支持

By default, mongos instances support using hedged reads. 默认情况下,mongos实例支持使用模糊读取。To turn off a mongos instance's support for hedged reads, see the readHedgingMode parameter. 要关闭mongos实例对模糊读取的支持,请参阅readHedgingMode参数。If the hedged read support is off, mongos does not use hedged reads regardless of the hedge option specified for the read preference.如果关闭了对冲读取支持,mongos不会使用对冲读取,无论为读取首选项指定了hedge(对冲)选项。

Hedged Reads Diagnostics模糊阅读诊断

The command serverStatus and its corresponding mongosh method db.serverStatus() return hedgingMetrics.命令serverStatus及其对应的mongosh方法db.serverStatus()返回hedgingMetrics

Confirm Connection to mongos Instances确认与mongos实例的连接

To detect if the MongoDB instance that your client is connected to is mongos, use the hello command. 要检测客户端连接的MongoDB实例是否为mongos,请使用hello命令。When a client connects to a mongos, hello returns a document with a msg field that holds the string isdbgrid. 当客户端字符串连接到hello时,isdbgrid字符串将返回到该msg字段。For example:例如:

{
   "isWritablePrimary" : true,
   "msg" : "isdbgrid",
   "maxBsonObjectSize" : 16777216,
   "ok" : 1,
   ...
}

If the application is instead connected to a mongod, the returned document does not include the isdbgrid string.如果应用程序连接到mongod,则返回的文档不包括isdbgrid字符串。

Targeted Operations vs. Broadcast Operations目标作战与广播作战

Generally, the fastest queries in a sharded environment are those that mongos route to a single shard, using the shard key and the cluster meta data from the config server. 通常,分片环境中最快的查询是mongos使用分片键和配置服务器中的集群元数据路由到单个分片的查询。These targeted operations use the shard key value to locate the shard or subset of shards that satisfy the query document.这些目标操作使用shard键值来定位满足查询文档的shard或shard子集。

For queries that don't include the shard key, mongos must query all shards, wait for their responses and then return the result to the application. 对于不包含shard键的查询,mongos必须查询所有shard,等待它们的响应,然后将结果返回给应用程序。These "scatter/gather" queries can be long running operations.这些“分散/聚集”查询可以是长时间运行的操作。

Broadcast Operations广播业务

mongos instances broadcast queries to all shards for the collection unless the mongos can determine which shard or subset of shards stores this data.实例将查询广播到集合的所有分片,除非mongos可以确定哪个分片或分片的子集存储此数据。

Read operations to a sharded cluster. Query criteria does not include the shard key. The query router ``mongos`` must broadcast query to all shards for the collection.

After the mongos receives responses from all shards, it merges the data and returns the result document. mongos收到来自所有分片的响应后,它合并数据并返回结果文档。The performance of a broadcast operation depends on the overall load of the cluster, as well as variables like network latency, individual shard load, and number of documents returned per shard. 广播操作的性能取决于集群的总体负载,以及网络延迟、单个分片负载和每个分片返回的文档数等变量。Whenever possible, favor operations that result in targeted operation over those that result in a broadcast operation.只要有可能,就要支持导致目标操作的操作,而不是导致广播操作的操作。

Multi-update operations are always broadcast operations.多次更新操作始终是广播操作。

The updateMany() and deleteMany() methods are broadcast operations, unless the query document specifies the shard key in full.updateMany()deleteMany()方法是广播操作,除非查询文档完整地指定了shard键。

Targeted Operations有针对性的行动

mongos can route queries that include the shard key or the prefix of a compound shard key a specific shard or set of shards. 可以将包含分片键或复合分片键前缀的查询路由到特定分片或分片集。mongos uses the shard key value to locate the chunk whose range includes the shard key value and directs the query at the shard containing that chunk.mongos使用shard key值来定位其范围包括shard key值的块,并将查询指向包含该块的块。

Read operations to a sharded cluster. Query criteria includes the shard key. The query router ``mongos`` can target the query to the appropriate shard or shards.

For example, if the shard key is:

{ a: 1, b: 1, c: 1 }

The mongos program can route queries that include the full shard key or either of the following shard key prefixes at a specific shard or set of shards:mongos程序可以在特定的分片或分片集上路由包含完整分片键或以下分片键前缀之一的查询:

{ a: 1 }
{ a: 1, b: 1 }

All insertOne() operations target to one shard. 所有insertOne()操作都以一个分片为目标。Each document in the insertMany() array targets to a single shard, but there is no guarantee all documents in the array insert into a single shard.insertMany()数组中的每个文档都指向一个分片,但不能保证数组中的所有文档都插入一个分片。

All updateOne(), replaceOne() and deleteOne() operations must include the shard key or _id in the query document. 所有updateOne()replaceOne()deleteOne()操作都必须在查询文档中包含shard key或_idMongoDB returns an error if these methods are used without the shard key or _id.如果使用这些方法时没有分片键或_id,MongoDB将返回一个错误。

Depending on the distribution of data in the cluster and the selectivity of the query, mongos may still perform a broadcast operation to fulfill these queries.根据集群中数据的分布和查询的选择性,mongos仍然可以执行广播操作来完成这些查询。

Index Use索引使用

If the query does not include the shard key, the mongos must send the query to all shards as a "scatter/gather" operation. 如果查询不包括分片键mongos必须将查询作为“分散/聚集”操作发送到所有分片。Each shard will, in turn, use either the shard key index or another more efficient index to fulfill the query.每个分片将依次使用分片键索引或另一个更有效的索引来完成查询。

If the query includes multiple sub-expressions that reference the fields indexed by the shard key and the secondary index, the mongos can route the queries to a specific shard and the shard will use the index that will allow it to fulfill most efficiently.如果查询包含多个子表达式,这些子表达式引用由分片键和辅助索引索引的字段,mongos可以将查询路由到特定分片,分片将使用允许其最有效地完成的索引。

Sharded Cluster Security分片集群安全

Use Internal/Membership Authentication to enforce intra-cluster security and prevent unauthorized cluster components from accessing the cluster. 使用内部/成员身份验证来实施集群内安全,并防止未经授权的集群组件访问集群。You must start each mongod or mongos in the cluster with the appropriate security settings in order to enforce internal authentication.必须使用适当的安全设置启动群集中的每个mongodmongos,以强制执行内部身份验证。

Starting in MongoDB 5.3, SCRAM-SHA-1 cannot be used for intra-cluster authentication. 从MongoDB 5.3开始,SCRAM-SHA-1不能用于集群内身份验证。Only SCRAM-SHA-256 is supported.仅支持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-SHA-1和SCRAM-SHA-256都可以用于集群内身份验证,即使没有明确启用SCRAM。

See Deploy Sharded Cluster with Keyfile Authentication for a tutorial on deploying a secured sharded cluster.有关部署安全分片集群的教程,请参阅使用密钥文件身份验证部署分片集群

Cluster Users群集用户

Sharded clusters support Role-Based Access Control (RBAC) for restricting unauthorized access to cluster data and operations. 分片集群支持基于角色的访问控制(RBAC),以限制对集群数据和操作的未经授权访问。You must start each mongod in the cluster, including the config servers, with the --auth option in order to enforce RBAC. 您必须使用--auth选项启动群集中的每个mongod,包括配置服务器,以强制实施RBAC。Alternatively, enforcing Internal/Membership Authentication for inter-cluster security also enables user access controls via RBAC.或者,为集群间安全实施内部/成员身份验证也可以通过RBAC实现用户访问控制。

With RBAC enforced, clients must specify a --username, --password, and --authenticationDatabase when connecting to the mongos in order to access cluster resources.在实施RBAC的情况下,客户端在连接到mongos时必须指定--username--password--authenticationDatabase,以便访问集群资源。

Each cluster has its own cluster users. 每个集群都有自己的集群用户。These users cannot be used to access individual shards.这些用户不能用于访问单个分片。

See Enable Access Control for a tutorial on enabling adding users to an RBAC-enabled MongoDB deployment.有关如何将用户添加到启用RBAC的MongoDB部署的教程,请参阅启用访问控制

Metadata Operations元数据操作

mongos uses "majority" write concern for the following operations that affect the sharded cluster metadata:写下影响分片群集元数据的以下操作的注意事项:

CommandMethod方法Note
addShardsh.addShard()
createdb.createCollection()
dropdb.collection.drop()
dropDatabasedb.dropDatabase()Changed in MongoDB 3.6MongoDB 3.6中的更改
enableShardingsh.enableSharding()
movePrimary
renameCollectiondb.collection.renameCollection()
shardCollectionsh.shardCollection()
removeShard
setFeatureCompatibilityVersion

Additional Information其他信息

FCV CompatibilityFCV兼容性

Starting in MongoDB 4.0, the mongos binary will crash when attempting to connect to mongod instances whose feature compatibility version (fCV) is greater than that of the mongos. 从MongoDB 4.0开始,当试图连接到功能兼容性版本(fCV)高于mongosmongod实例时,mongos二进制文件将崩溃。For example, you cannot connect a MongoDB 4.0 version mongos to a 4.2 sharded cluster with fCV set to 4.2. 例如,不能将MongoDB 4.0版mongos连接到fCV设置为4.2的4.2分片集群。You can, however, connect a MongoDB 4.0 version mongos to a 4.2 sharded cluster with fCV set to 4.0.但是,您可以将MongoDB 4.0版本的mongos连接到fCV设置为4.0的4.2分片集群。

Connection Pools连接池

Starting in MongoDB 4.2, MongoDB adds the parameter ShardingTaskExecutorPoolReplicaSetMatching. 从MongoDB 4.2开始,MongoDB添加了参数ShardingTaskExecutorPoolReplicaSetMatchingThis parameter determines the minimum size of the mongod/mongos instance's connection pool to each member of the sharded cluster. 此参数确定mongod/mongos实例到分片集群每个成员的连接池的最小大小。This value can vary during runtime.该值在运行期间可能会有所不同。

mongod and mongos maintain connection pools to each replica set secondary for every replica set in the sharded cluster. mongodmongos为分片集群中的每个副本集维护到每个副本集的连接池。By default, these pools have a number of connections that is at least the number of connections to the primary.默认情况下,这些池的连接数至少等于主池的连接数。

To modify, see ShardingTaskExecutorPoolReplicaSetMatching.要修改,请参阅ShardingTaskExecutorPoolReplicaSetMatching

Using Aggregation Pipelines with Clusters通过集群使用聚合管道

For more information on how sharding works with aggregations, read the sharding chapter in the Practical MongoDB Aggregations e-book.有关分片如何与聚合一起工作的更多信息,请阅读实用MongoDB聚合电子书中的分片一章。

←  Config ServersShard Keys →