Docs HomeMongoDB Manual

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

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: 另请参阅:

Shard Keys

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 Range 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 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.

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. 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. 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. If the mongos is no longer in use, it is safe to restart the process to close existing connections.

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:

db.adminCommand("connPoolStats");

See the System Resource Utilization section of the UNIX ulimit Settings document.