FAQ: Sharding with MongoDB常见问题解答:使用MongoDB共享

On this page本页内容

This document answers common questions about Sharding. 本文档回答了有关分片的常见问题。See also the Sharding section in the manual, which provides an overview of sharding, including details on:另请参阅手册中的分片部分,该部分提供了分片的概述,包括以下详细信息:

Is sharding appropriate for a new deployment?分片是否适合新部署?

Sometimes. However, if your data set fits on a single server, you should begin with an unsharded deployment as sharding while your data set is small provides little advantage .有时但是,如果您的数据集适合于单个服务器,那么您应该从非共享部署开始,因为当您的数据集中很小时,分片并没有什么好处

Can I select a different shard key after sharding a collection?在对集合进行分片后,我可以选择其他分片键吗?

Your options for changing a shard key depend on the version of MongoDB that you are running:更改分片键的选项取决于您正在运行的MongoDB版本:

  • Starting in MongoDB 5.0, you can reshard a collection by changing a document's shard key.从MongoDB 5.0开始,您可以通过更改文档的分片键来重新分片集合
  • Starting in MongoDB 4.4, you can refine a shard key by adding a suffix field or fields to the existing shard key.从MongoDB 4.4开始,您可以通过向现有的分片键添加后缀字段来细化分片键
  • In MongoDB 4.2 and earlier, the choice of shard key cannot be changed after sharding.在MongoDB 4.2及更早版本中,分片后不能更改分片键的选择。

In MongoDB 4.2 and earlier, if you must change a shard key after sharding a collection and cannot upgrade, the best option is to:在MongoDB 4.2及更早版本中,如果在对集合进行分片后必须更改分片键,并且无法升级,最好的选择是:

  • dump all data from MongoDB into an external format.将MongoDB中的所有数据转储为外部格式。
  • drop the original sharded collection.删除原始分片集合。
  • configure sharding using a more ideal shard key.使用更理想的分片键配置分片。
  • pre-split预划分 the shard key range to ensure initial even distribution.分片键范围,以确保初始均匀分布。
  • restore the dumped data into MongoDB.将转储的数据恢复到MongoDB中。
Tip提示
See also: 参阅:

Why are my documents not distributed across the shards?为什么我的文档没有跨分片分发?

The balancer starts distributing data across the shards once the distribution of chunks has reached certain thresholds. 一旦块的分布达到一定的阈值,均衡器就开始跨分片分发数据。See Migration Thresholds.请参见迁移阈值

In addition, MongoDB cannot move a chunk if the number of documents in the chunk exceeds a certain number. 此外,如果区块中的文档数量超过一定数量,MongoDB就无法移动区块。See Maximum Number of Documents Per Chunk to Migrate and Indivisible/Jumbo Chunks.请参见要迁移的每个块的最大文档数不可见/巨型块

How does mongos detect changes in the sharded cluster configuration?mongos如何检测分片集群配置中的更改?

mongos instances maintain a cache of the config database that holds the metadata for the sharded cluster.mongos实例维护config数据库的缓存,该缓存保存了分片集群的元数据。

mongos updates its cache lazily by issuing a request to a shard and discovering that its metadata is out of date. 通过向分片发出请求并发现其元数据已过期,从而懒洋洋地更新其缓存。To force the mongos to reload its cache, you can run the flushRouterConfig command against each mongos directly.要强制mongos重新加载其缓存,可以直接对每个mongos运行fflushRouterConfig命令。

What does writebacklisten in the log mean?日志中的writebacklisten是什么意思?

The writeback listener is a process that opens a long poll to relay writes back from a mongod or mongos after migrations to make sure they have not gone to the wrong server. 写回侦听器是一个过程,它打开一个长轮询,以在迁移后中继来自mongodmongos的写回,以确保它们没有转到错误的服务器。The writeback listener sends writes back to the correct server if necessary.如果需要,写回侦听器将写回发送到正确的服务器。

These messages are a key part of the sharding infrastructure and should not cause concern.这些消息是分片基础设施的关键部分,不应引起关注。

How does mongos use connections?mongos如何使用连接?

Each mongos instance maintains a pool of connections to the members of the sharded cluster. 每个mongos实例都维护一个到分片集群成员的连接池。Client requests use these connections one at a time; i.e. requests are not multiplexed or pipelined.客户端请求一次使用一个连接;即,请求不是多路复用或流水线的。

When client requests complete, the mongos returns the connection to the pool. 当客户端请求完成时,mongos将连接返回到池。These pools do not shrink when the number of clients decreases. 当客户端数量减少时,这些池不会收缩。This can lead to an unused mongos with a large number of open connections. 这可能导致未使用的mongos具有大量开放连接。If the mongos is no longer in use, it is safe to restart the process to close existing connections.如果mongos不再使用,可以安全地重新启动进程以关闭现有连接。

To return aggregated statistics related to all of the outgoing connection pools used by the mongos, connect mongosh to the mongos, and run the connPoolStats command:要返回与mongos使用的所有传出连接池相关的聚合统计信息,请将mongosh连接到mongos,然后运行connPoolStats命令:

db.adminCommand("connPoolStats");

See the System Resource Utilization section of the UNIX ulimit Settings document.请参见UNIX ulimit设置文档的系统资源利用率部分。

←  FAQ: ConcurrencyFAQ: Replication and Replica Sets →