Database Manual / Self-Managed Deployments / Administration / Monitoring

FAQ: Self-Managed MongoDB Diagnostics常见问题解答:自我管理MongoDB诊断

This document provides answers to common diagnostic questions and issues.本文档提供了常见诊断问题的答案。

If you don't find the answer you're looking for, check the complete list of FAQs or post your question to the MongoDB Community.如果你没有找到你想要的答案,请查看完整的常见问题列表或将问题发布到MongoDB社区。

Where can I find information about a mongod process that stopped running unexpectedly?我在哪里可以找到有关意外停止运行的mongod进程的信息?

If mongod shuts down unexpectedly on a UNIX or UNIX-based platform, and if mongod fails to log a shutdown or error message, then check your system logs for messages pertaining to MongoDB. 如果mongod在UNIX或基于UNIX的平台上意外关闭,并且mongod未能记录关闭或错误消息,请检查系统日志中是否有与MongoDB相关的消息。For example, for logs located in /var/log/messages, use the following commands:例如,对于位于/var/log/messages中的日志,使用以下命令:

sudo grep mongod /var/log/messages
sudo grep score /var/log/messages

Does TCP keepalive time affect MongoDB Deployments?TCPkeepalive时间会影响MongoDB部署吗?

If you experience network timeouts or socket errors in communication between clients and servers, or between members of a sharded cluster or replica set, check the TCP keepalive value for the affected systems.如果在客户端和服务器之间,或分片集群或副本集的成员之间的通信中遇到网络超时或套接字错误,请检查受影响系统的TCP keepalive值。

Many operating systems set this value to 7200 seconds (two hours) by default. For MongoDB, you will generally experience better results with a shorter keepalive value, on the order of 120 seconds (two minutes).许多操作系统默认将此值设置为7200秒(两小时)。对于MongoDB,您通常会体验到更短的keepalive值的更好结果,大约为120秒(两分钟)。

If your MongoDB deployment experiences keepalive-related issues, you must alter the keepalive value on all affected systems. This includes all machines running mongod or mongos processes and all machines hosting client processes that connect to MongoDB.如果MongoDB部署遇到了与keepalive相关的问题,你必须在所有受影响的系统上更改keepalive值。这包括所有运行mongodmongos进程的机器,以及所有托管连接到MongoDB的客户端进程的机器。

Adjusting the TCP keepalive value:调整TCP保活值:

Linux
  • To view the keepalive setting on Linux, use one of the following commands:要查看Linux上的keepalive设置,请使用以下命令之一:

    sysctl net.ipv4.tcp_keepalive_time

    Or:或者:

    cat /proc/sys/net/ipv4/tcp_keepalive_time

    The value is measured in seconds.该值以秒为单位进行测量。

    Note

    Although the setting name includes ipv4, the tcp_keepalive_time value applies to both IPv4 and IPv6.虽然设置名称包括ipv4,但tcp_kealive_time值适用于IPv4和IPv6。

  • To change the tcp_keepalive_time value, you can use one of the following commands, supplying a <value> in seconds:要更改tcp_keepalive_time值,可以使用以下命令之一,以秒为单位提供一个<value>

    sudo sysctl -w net.ipv4.tcp_keepalive_time=<value>

    Or:或者:

    echo <value> | sudo tee /proc/sys/net/ipv4/tcp_keepalive_time

    These operations do not persist across system reboots. To persist the setting, add the following line to /etc/sysctl.conf, supplying a <value> in seconds, and reboot the machine:这些操作在系统重新启动后不会持续。要保持设置不变,请将以下行添加到/etc/sysctl.conf中,以秒为单位提供一个<value>,然后重新启动计算机:

    net.ipv4.tcp_keepalive_time = <value>

    Keepalive values greater than 300 seconds, (5 minutes) will be overridden on mongod and mongos sockets and set to 300 seconds.大于300秒(5分钟)的Keepalive值将在mongodmongos套接字上被覆盖,并设置为300秒。

Windows
  • To view the keepalive setting on Windows, issue the following command:要查看Windows上的保活设置,请发出以下命令:

    reg query HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters /v KeepAliveTime

    The registry value is not present by default. The system default, used if the value is absent, is 7200000 milliseconds or 0x6ddd00 in hexadecimal.默认情况下,注册表值不存在。如果该值不存在,则使用系统默认值7200000毫秒或十六进制0x6ddd00

  • To change the KeepAliveTime value, use the following command in an Administrator Command Prompt, where <value> is expressed in hexadecimal (e.g. 120000 is 0x1d4c0):要更改KeepAliveTime值,请在管理员命令提示符中使用以下命令,其中<value>以十六进制表示(例如1200000x1d4c0):

    reg add HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ /t REG_DWORD /v KeepAliveTime /d <value>

    Windows users should consider the Windows Server Technet Article on KeepAliveTime for more information on setting keepalive for MongoDB deployments on Windows systems. Windows用户应该考虑Windows Server Technet上关于KeepAliveTime的文章,以获取有关在Windows系统上为MongoDB部署设置keepalive的更多信息。Keepalive values greater than or equal to 600000 milliseconds (10 minutes) will be ignored by mongod and mongos.大于或等于600000毫秒(10分钟)的Keepalive值将被mongodmongos忽略。

macOS
  • To view the keepalive setting on macOS, issue the following command:要查看macOS上的keepalive设置,请发出以下命令:

    sysctl net.inet.tcp.keepidle

    The value is measured in milliseconds.该值以毫秒为单位进行测量。

  • To change the net.inet.tcp.keepidle value, you can use the following command, supplying a <value> in milliseconds:要更改net.inet.tcp.keepidle值,可以使用以下命令,提供以毫秒为单位的<value>

    sudo sysctl net.inet.tcp.keepidle=<value>

    This operation does not persist across system reboots, and must be set each time your system reboots. 此操作在系统重新启动后不会持续,每次系统重新启动时都必须进行设置。See your operating system's documentation for instructions on setting this value persistently. 有关持久设置此值的说明,请参阅操作系统的文档。Keepalive values greater than or equal to 600000 milliseconds (10 minutes) will be ignored by mongod and mongos.大于或等于600000毫秒(10分钟)的Keepalive值将被mongodmongos忽略。

    Note

    In macOS 10.15 Catalina, Apple no longer allows for configuration of the net.inet.tcp.keepidle option.在macOS 10.15 Catalina中,Apple不再允许配置net.inet.tcp.keepidle选项。

You will need to restart mongod and mongos processes for new system-wide keepalive settings to take effect.您需要重新启动mongodmongos进程,以便新的系统范围保活设置生效。

Do TCP Retransmission Timeouts affect MongoDB Deployments?TCP重传超时会影响MongoDB部署吗?

If you experience long stalls (stalls greater than two minutes) followed by network timeouts or socket errors between clients and server or between members of a sharded cluster or replica set, check the tcp_retries2 value for the affected systems.如果您在客户端和服务器之间或分片集群或副本集的成员之间遇到长时间停滞(超过两分钟的停滞),然后出现网络超时或套接字错误,请检查受影响系统的tcp_retries2值。

Most Linux operating systems set this value to 15 by default, while Windows sets it to 5. For MongoDB, you experience better results with a lower tcp_retries2 value, on the order of 5 (12 seconds) or lower.大多数Linux操作系统默认将此值设置为15,而Windows将其设置为5。对于MongoDB,您可以使用较低的tcp_retries2值获得更好的结果,大约为5(12秒)或更低。

If your MongoDB deployment experiences TCP retransmission timeout-related issues, change the tcp_retries2 value (TcpMaxDataRetransmission on Windows) for all affected systems. 如果MongoDB部署遇到与TCP重传超时相关的问题,请更改所有受影响系统的tcp_retries2值(Windows上的TcpMaxDataRetransmission)。This includes all machines running mongod or mongos processes and all machines hosting client processes that connect to MongoDB.这包括所有运行mongodmongos进程的机器,以及所有托管连接到MongoDB的客户端进程的机器。

Adjust the TCP Retransmission Timeout调整TCP重传超时

Linux

On most Linux operating systems, control the TCP retransmission by adjusting the net.ipv4.tcp_retries2 sysctl setting.在大多数Linux操作系统上,通过调整net.ipv4.tcp_retries2 sysctl设置来控制TCP重传。

Note

Although the setting name includes ipv4, the tcp_retries2 setting applies to both IPv4 and IPv6.虽然设置名称包括ipv4,但tcp_retries2设置适用于ipv4和IPv6。

  • To view the current setting, use the sysctl command:要查看当前设置,请使用sysctl命令:

    sysctl net.ipv4.tcp_retries2
    net.ipv4.tcp_retries = 15
  • To change the tcp_retries2 setting at runtime, use the sysctl command:要在运行时更改tcp_retries2设置,请使用sysctl命令:

    sysctl -w net.ipv4.tcp_retries2=8
  • To make the change permanent, edit the configuration file:要使更改永久化,请编辑配置文件:

    1. Open /etc/sysctl.conf in your preferred text editor:在您喜欢的文本编辑器中打开/etc/sysctl.conf

      vi /etc/sysctl.conf
    2. Configure the net.ipv4.tcp_retries2 setting:配置net.ipv4.tcp_retries2设置:

      net.ipv4.tcp_retries2 = 8
    3. Restart the system.重新启动系统。

    Your system now uses the new tcp_retries2 setting.系统现在使用新的tcp_retries2设置。

Windows

On Windows, control TCP Retransmission by adjusting the TcpMaxDataRetransmissions parameter.在Windows上,通过调整TcpMaxDataRetransmissions参数来控制TCP重传。

  • To view the TcpMaxDataRetransmissions setting on Windows, issue the following command:要查看Windows上的TcpMaxDataRetransmissions设置,请发出以下命令:

    reg query HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters /v TcpMaxDataRetransmissions

    By default, the parameter is not set. The system default, used if the value is absent, is 5 retries.默认情况下,该参数未设置。如果该值不存在,则使用系统默认值,即重试5次。

  • To change the TcpMaxDataRetransmissions value, use the following command in an Administrator Command Prompt, where <value> is an integer:要更改TcpMaxDataRetransmissions值,请在管理员命令提示符中使用以下命令,其中<value>是一个整数:

    reg add HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ /t REG_DWORD /v TcpMaxDataRetransmission /d <value>

Why does MongoDB log so many "Connection Accepted" events?为什么MongoDB会记录这么多“连接已接受”事件?

If you see a very large number of connection and re-connection messages in your MongoDB log, then clients are frequently connecting and disconnecting to the MongoDB server. This is normal behavior for applications that do not use request pooling, such as CGI. Consider using FastCGI, an Apache Module, or some other kind of persistent application server to decrease the connection overhead.如果你在MongoDB日志中看到大量的连接和重新连接消息,那么客户端经常连接和断开到MongoDB服务器。对于不使用请求池的应用程序(如CGI)来说,这是正常的行为。考虑使用FastCGI、Apache模块或其他类型的持久应用服务器来减少连接开销。

If these connections do not impact your performance you can use the run-time quiet option or the command-line option --quiet to suppress these messages from the log.如果这些连接不影响性能,您可以使用run-time quiet选项或命令行选项--quiet从日志中抑制这些消息。

What tools are available for monitoring MongoDB?有哪些工具可用于监控MongoDB?

The MongoDB Cloud Manager and Ops Manager, an on-premises solution available in MongoDB Enterprise Advanced include monitoring functionality, which collects data from running MongoDB deployments and provides visualization and alerts based on that data.MongoDB Cloud ManagerOps Manager是MongoDB Enterprise Advanced中提供的本地解决方案,包括监控功能,该功能从运行的MongoDB部署中集合数据,并根据这些数据提供可视化和警报。

For more information, see also the MongoDB Cloud Manager documentation and Ops Manager documentation.有关更多信息,请参阅MongoDB Cloud Manager文档Ops Manager文档

A full list of third-party tools is available as part of the Monitoring a Self-Managed MongoDB Deployment documentation.第三方工具的完整列表可作为监控自我管理MongoDB部署文档的一部分。

Memory Diagnostics for the WiredTiger Storage EngineWiredTiger存储引擎的内存诊断

Must my working set size fit RAM?我的工作集大小必须适合RAM吗?

No.不。

If the cache does not have enough space to load additional data, WiredTiger evicts pages from the cache to free up space.如果缓存没有足够的空间加载其他数据,WiredTiger会从缓存中驱逐页面以释放空间。

Note

The storage.wiredTiger.engineConfig.cacheSizeGB limits the size of the WiredTiger internal cache. storage.wiredTiger.engineConfig.cacheSizeGB限制了WiredTiger内部缓存的大小。The operating system uses the available free memory for filesystem cache, which allows the compressed MongoDB data files to stay in memory. In addition, the operating system uses any free RAM to buffer file system blocks and file system cache.操作系统使用可用的空闲内存进行文件系统缓存,这允许压缩的MongoDB数据文件留在内存中。此外,操作系统使用任何空闲RAM来缓冲文件系统块和文件系统缓存。

To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.为了容纳额外的RAM用户,您可能需要减小WiredTiger内部缓存大小。

The default WiredTiger internal cache size value assumes that there is a single mongod instance per machine. 默认的WiredTiger内部缓存大小值假定每台机器只有一个mongod实例。If a single machine contains multiple MongoDB instances, decrease the setting to accommodate the other mongod instances.如果一台机器包含多个MongoDB实例,请减小设置以容纳其他MongoDB实例。

If you run mongod in a container (for example, lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container. 如果你在一个无法访问系统中所有可用RAM的容器(例如lxccgroups、Docker等)中运行mongod,你必须将storage.wiredTiger.engineConfig.cacheSizeGB设置为小于容器中可用RAM量的值。The exact amount depends on the other processes running in the container. See memLimitMB.确切的数量取决于容器中运行的其他进程。请参阅memLimitMB

You can only provide one of either storage.wiredTiger.engineConfig.cacheSizeGB or storage.wiredTiger.engineConfig.cacheSizePct.您只能提供storage.wiredTiger.engineConfig.cacheSizeGBstorage.wiredTiger.engineConfig.cacheSizePct中的一个。

To see statistics on the cache and eviction, use the serverStatus command. The wiredTiger.cache field holds the information on the cache and eviction.要查看缓存和驱逐的统计信息,请使用serverStatus命令。wiredTiger.cache字段保存有关缓存和驱逐的信息。

...
"wiredTiger" : {
...
"cache" : {
"tracked dirty bytes in the cache" : <num>,
"bytes currently in the cache" : <num>,
"maximum bytes configured" : <num>,
"bytes read into cache" :<num>,
"bytes written from cache" : <num>,
"pages evicted by application threads" : <num>,
"checkpoint blocked page eviction" : <num>,
"unmodified pages evicted" : <num>,
"page split during eviction deepened the tree" : <num>,
"modified pages evicted" : <num>,
"pages selected for eviction unable to be evicted" : <num>,
"pages evicted because they exceeded the in-memory maximum" : <num>,,
"pages evicted because they had chains of deleted items" : <num>,
"failed eviction of pages that exceeded the in-memory maximum" : <num>,
"hazard pointer blocked page eviction" : <num>,
"internal pages evicted" : <num>,
"maximum page size at eviction" : <num>,
"eviction server candidate queue empty when topping up" : <num>,
"eviction server candidate queue not empty when topping up" : <num>,
"eviction server evicting pages" : <num>,
"eviction server populating queue, but not evicting pages" : <num>,
"eviction server unable to reach eviction goal" : <num>,
"pages split during eviction" : <num>,
"pages walked for eviction" : <num>,
"eviction worker thread evicting pages" : <num>,
"in-memory page splits" : <num>,
"percentage overhead" : <num>,
"tracked dirty pages in the cache" : <num>,
"pages currently held in the cache" : <num>,
"pages read into cache" : <num>,
"pages written from cache" : <num>,
},
...

For an explanation of some key cache and eviction statistics, such as wiredTiger.cache.bytes currently in the cache and wiredTiger.cache.tracked dirty bytes in the cache, see wiredTiger.cache.有关某些关键缓存和驱逐统计信息的解释,例如当前缓存中的wiredTiger.cache.bytes currently in the cache和缓存中wiredTiger.cache.tracked脏字节,请参阅wiredTigercache

To adjust the size of the WiredTiger internal cache, see --wiredTigerCacheSizeGB and storage.wiredTiger.engineConfig.cacheSizeGB. 要调整WiredTiger内部缓存的大小,请参阅--wiredTigerCacheSizeGBstorage.wiredTiger.engineConfig.cacheSizeGBAvoid increasing the WiredTiger internal cache size above its default value. 避免将WiredTiger内部缓存大小增加到默认值以上。If your use case requires increased internal cache size, see --wiredTigerCacheSizePct and storage.wiredTiger.engineConfig.cacheSizePct.如果用例需要增加内部缓存大小,请参阅--wiredTigerCacheSizePctstorage.wiredTiger.engineConfig.cacheSizePct

How do I calculate how much RAM I need for my application?如何计算我的应用程序需要多少RAM?

With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.使用WiredTiger,MongoDB同时利用WiredTigeer内部缓存和文件系统缓存。

The default WiredTiger internal cache size is the larger of either:默认的WiredTiger内部缓存大小是以下两者中的较大值:

  • 50% of (RAM - 1 GB), or
  • 256 MB.

For example, on a system with a total of 4GB of RAM the WiredTiger cache uses 1.5GB of RAM (0.5 * (4 GB - 1 GB) = 1.5 GB). 例如,在总共有4GB RAM的系统上,WiredTiger缓存使用1.5GB RAM(0.5 * (4 GB - 1 GB) = 1.5 GB)。Conversely, on a system with a total of 1.25 GB of RAM WiredTiger allocates 256 MB to the WiredTiger cache because that is more than half of the total RAM minus one gigabyte (0.5 * (1.25 GB - 1 GB) = 128 MB < 256 MB).相反,在总共有1.25 GB RAM的系统上,WiredTiger将256 MB分配给WiredTigeer缓存,因为这超过了总RAM减去1 GB的一半(0.5 * (1.25 GB - 1 GB) = 128 MB < 256 MB)。

Note

In some instances, such as when running in a container, the database can have memory constraints that are lower than the total system memory. In such instances, this memory limit, rather than the total system memory, is used as the maximum RAM available.在某些情况下,例如在容器中运行时,数据库的内存限制可能低于总系统内存。在这种情况下,这个内存限制,而不是整个系统内存,被用作可用的最大RAM。

To see the memory limit, see hostInfo.system.memLimitMB.要查看内存限制,请参阅hostInfo.system.memLimitMB

By default, WiredTiger uses Snappy block compression for all collections and prefix compression for all indexes. Compression defaults are configurable at a global level and can also be set on a per-collection and per-index basis during collection and index creation.默认情况下,WiredTiger对所有集合使用Snappy块压缩,对所有索引使用前缀压缩。压缩默认值可以在全局级别进行配置,也可以在集合和索引创建期间根据每个集合和每个索引进行设置。

Different representations are used for data in the WiredTiger internal cache versus the on-disk format:与磁盘格式相比,WiredTiger内部缓存中的数据使用了不同的表示方式:

  • Data in the filesystem cache is the same as the on-disk format, including benefits of any compression for data files. The filesystem cache is used by the operating system to reduce disk I/O.文件系统缓存中的数据与磁盘上的格式相同,包括对数据文件进行任何压缩的好处。操作系统使用文件系统缓存来减少磁盘I/O。
  • Indexes loaded in the WiredTiger internal cache have a different data representation to the on-disk format, but can still take advantage of index prefix compression to reduce RAM usage. Index prefix compression deduplicates common prefixes from indexed fields.加载在WiredTiger内部缓存中的索引具有与磁盘上格式不同的数据表示形式,但仍然可以利用索引前缀压缩来减少RAM使用。索引前缀压缩可消除索引字段中的常见前缀的重复。
  • Collection data in the WiredTiger internal cache is uncompressed and uses a different representation from the on-disk format. Block compression can provide significant on-disk storage savings, but data must be uncompressed to be manipulated by the server.WiredTiger内部缓存中的集合数据是未压缩的,并使用与磁盘格式不同的表示形式。块压缩可以显著节省磁盘存储空间,但数据必须解压缩才能由服务器操作。

With the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes.使用文件系统缓存,MongoDB会自动使用WiredTiger缓存或其他进程未使用的所有空闲内存。

To adjust the size of the WiredTiger internal cache, see --wiredTigerCacheSizeGB and storage.wiredTiger.engineConfig.cacheSizeGB. 要调整WiredTiger内部缓存的大小,请参阅--wiredTigerCacheSizeGBstorage.wiredTiger.engineConfig.cacheSizeGBAvoid increasing the WiredTiger internal cache size above its default value. If your use case requires increased internal cache size, see --wiredTigerCacheSizePct and storage.wiredTiger.engineConfig.cacheSizePct.避免将WiredTiger内部缓存大小增加到默认值以上。如果用例需要增加内部缓存大小,请参阅--wiredTigerCacheSizePctstorage.wiredTiger.engineConfig.cacheSizePct

Note

The storage.wiredTiger.engineConfig.cacheSizeGB limits the size of the WiredTiger internal cache. storage.wiredTiger.engineConfig.cacheSizeGB限制了WiredTiger内部缓存的大小。The operating system uses the available free memory for filesystem cache, which allows the compressed MongoDB data files to stay in memory. In addition, the operating system uses any free RAM to buffer file system blocks and file system cache.操作系统使用可用的空闲内存进行文件系统缓存,这允许压缩的MongoDB数据文件留在内存中。此外,操作系统使用任何空闲RAM来缓冲文件系统块和文件系统缓存。

To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.为了容纳额外的RAM用户,您可能需要减小WiredTiger内部缓存大小。

The default WiredTiger internal cache size value assumes that there is a single mongod instance per machine. If a single machine contains multiple MongoDB instances, decrease the setting to accommodate the other mongod instances.默认的WiredTiger内部缓存大小值假定每台机器只有一个mongod实例。如果一台机器包含多个MongoDB实例,请减小设置以容纳其他MongoDB实例。

If you run mongod in a container (for example, lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container. 如果你在一个无法访问系统中所有可用RAM的容器(例如lxccgroups、Docker等)中运行mongod,你必须将storage.wiredTiger.engineConfig.cacheSizeGB设置为小于容器中可用RAM量的值。The exact amount depends on the other processes running in the container. See memLimitMB.确切的数量取决于容器中运行的其他进程。请参阅memLimitMB

You can only provide one of either storage.wiredTiger.engineConfig.cacheSizeGB or storage.wiredTiger.engineConfig.cacheSizePct.您只能提供storage.wiredTiger.engineConfig.cacheSizeGBstorage.wiredTiger.engineConfig.cacheSizePct中的一个。

To view statistics on the cache and eviction rate, see the wiredTiger.cache field returned from the serverStatus command.要查看缓存和驱逐率的统计信息,请参阅serverStatus命令返回的wiredTiger.cache字段。

Sharded Cluster Diagnostics分片化集群诊断

The two most important factors in maintaining a successful sharded cluster are:维护成功的分片集群的两个最重要因素是:

While you can change your shard key later, it is important to carefully consider your shard key choice to avoid scalability and performance issues. 虽然您可以稍后更改分片键,但重要的是要仔细考虑分片键的选择,以避免可扩展性和性能问题。Continue reading for specific issues you may encounter in a production environment.继续阅读您在生产环境中可能遇到的具体问题。

In a new sharded cluster, why does all data remain on one shard?在一个新的分片集群中,为什么所有数据都保留在一个分片上?

Your cluster must have sufficient data for sharding to make sense. Sharding works by migrating chunks between the shards until each shard has roughly the same number of chunks.集群必须有足够的数据才能进行分片。分片的工作原理是在分片之间迁移块,直到每个分片的块数大致相同。

The default chunk size is 128 megabytes. MongoDB will not begin migrations until the imbalance of chunks in the cluster exceeds the migration threshold. This behavior helps prevent unnecessary chunk migrations, which can degrade the performance of your cluster as a whole.默认块大小为128 MB。MongoDB将不会开始迁移,直到集群中块的不平衡超过迁移阈值。这种行为有助于防止不必要的块迁移,这可能会降低整个集群的性能。

If you have just deployed a sharded cluster, make sure that you have enough data to make sharding effective. If you do not have sufficient data to create more than eight 128 megabyte chunks, then all data will remain on one shard. Either lower the chunk size setting, or add more data to the cluster.如果您刚刚部署了一个分片集群,请确保您有足够的数据来使分片有效。如果你没有足够的数据来创建超过8个128兆字节的块,那么所有数据都将保留在一个分片上。降低块大小设置,或向集群添加更多数据。

As a related problem, the system will split chunks only on inserts or updates, which means that if you configure sharding and do not continue to issue insert and update operations, the database will not create any chunks. You can either wait until your application inserts data or split chunks manually.作为一个相关的问题,系统只会在插入或更新时分割块,这意味着如果您配置了分片并且不继续执行插入和更新操作,数据库将不会创建任何块。您可以等到应用程序插入数据或手动拆分块

Finally, if your shard key has a low cardinality, MongoDB may not be able to create sufficient splits among the data.最后,如果分片键基数较低,MongoDB可能无法在数据之间创建足够的分割。

Why would one shard receive a disproportionate amount of traffic in a sharded cluster?为什么一个分片在分片集群中会收到不成比例的流量?

In some situations, a single shard or a subset of the cluster will receive a disproportionate portion of the traffic and workload. In almost all cases this is the result of a shard key that does not effectively allow write scaling.在某些情况下,单个分片或集群的子集将接收不成比例的流量和工作负载。在几乎所有情况下,这都是由于分片键不能有效地允许写扩展造成的。

It's also possible that you have "hot chunks." In this case, you may be able to solve the problem by splitting and then migrating parts of these chunks.也有可能你有“热块”。在这种情况下,你可以通过拆分然后迁移这些块的一部分来解决这个问题。

You may have to consider resharding your collection with a different shard key to correct this pattern.您可能需要考虑使用不同的分片键重新分片集合,以纠正这种模式。

What can prevent a sharded cluster from balancing?什么会阻止分片集群的平衡?

If you have just deployed your sharded cluster, you may want to consider the troubleshooting suggestions for a new cluster where data remains on a single shard.如果您刚刚部署了分片集群,您可能需要考虑针对数据保留在单个分片上的新集群的故障排除建议

If the cluster was initially balanced, but later developed an uneven distribution of data, consider the following possible causes:如果集群最初是平衡的,但后来数据分布不均,请考虑以下可能的原因:

  • You have deleted or removed a significant amount of data from the cluster. If you have added additional data, it may have a different distribution with regards to its shard key.您已从群集中删除或移除了大量数据。如果你添加了额外的数据,它的分片键可能会有不同的分布。
  • Your shard key has low cardinality and MongoDB cannot split the chunks any further.分片键基数较低,MongoDB无法进一步分割块。
  • Your data set is growing faster than the balancer can distribute data around the cluster. This is uncommon and typically is the result of:数据集增长速度超过了平衡器在集群中分发数据的速度。这种情况并不常见,通常是由于:

    • a balancing window that is too short, given the rate of data growth.考虑到数据增长的速度,平衡窗口太短。
    • an uneven distribution of write operations that requires more data migration. You may have to choose a different shard key to resolve this issue.写操作分布不均,需要更多的数据迁移。您可能需要选择不同的分片键来解决此问题。
    • poor network connectivity between shards, which may lead to chunk migrations that take too long to complete. Investigate your network configuration and interconnections between shards.分片之间的网络连接较差,这可能会导致块迁移需要太长时间才能完成。调查网络配置和分片之间的互连。

Why do chunk migrations affect sharded cluster performance?为什么块迁移会影响分片集群的性能?

If migrations impact your cluster or application's performance, consider the following options, depending on the nature of the impact:如果迁移会影响集群或应用程序的性能,请根据影响的性质考虑以下选项:

  1. If migrations only interrupt your clusters sporadically, you can limit the balancing window to prevent balancing activity during peak hours. Ensure that there is enough time remaining to keep the data from becoming out of balance again.如果迁移只是偶尔中断集群,则可以限制平衡窗口,以防止在高峰时段进行平衡活动。确保有足够的时间来防止数据再次失去平衡。
  2. If the balancer is always migrating chunks to the detriment of overall cluster performance:如果平衡器总是迁移块,从而损害了整体集群性能:

It's also possible that your shard key causes your application to direct all writes to a single shard. This kind of activity pattern can require the balancer to migrate most data soon after writing it. 也有可能是分片键导致应用程序将所有写入都指向一个分片。这种活动模式可能要求平衡器在写入数据后立即迁移大部分数据。You may have to consider resharding your collection with a different shard key that provides better write scaling.您可能必须考虑使用不同的分片键重新分片集合,以提供更好的写入扩展