mongodb 客户端库无法连接到副本集

mongodb client libraries fail to connect to replica set

使用最近的客户端库(pymongo 3.4,mongodb (nodejs) 2.2.27),我无法通过复制连接到我的 mongodb 服务器。 副本集配置包含服务器的内部 ips 或主机名。我收到以下错误:

pymongo.errors.ServerSelectionTimeoutError: mongodbdriver20151129-arbiter-1:27017: [Errno 8] nodename nor servname provided, or not known,mongodbdriver20151129-instance-1:27017: [Errno 8] nodename nor servname provided, or not known,mongodbdriver20151129-instance-2:27017: [Errno 8] nodename nor servname provided, or not known

pymongo.errors.ServerSelectionTimeoutError: 10.0.0.5:27017: timed out,10.0.0.6:27017: timed out,10.0.0.4:27017: timed out

我目前正在通过更改 replicaset 配置以包含服务器的外部 ips 来解决它,但我想这会减慢服务器间通信的速度。如何使用原始 rsconf 从外部位置连接到我的服务器?

[更新] 注意:我正在尝试连接到服务器的外部 ip,并且在使用 pymongo 2.8 或 mongodb (js) 2.1.4

时工作正常

[更新] 关注此 chat 了解更多 details/examples

所有官方支持的 MongoDB 驱动程序(包括节点驱动程序)的更高版本遵循 Server Discovery and Monitoring spec (SDAM), which mandates that all drivers to monitor all nodes in a replica set (see Monitoring).

进行此监控的原因是能够随时发现整个副本集的状态,并在当前主节点因任何原因离线时重新连接到新的主节点。参见 What's the point of periodic monitoring

为了能够监控副本集中的所有节点,驱动程序必须能够访问每个副本集成员。由于您的副本集是使用驱动程序无法访问的内部 IP 定义的,因此驱动程序无法连接到它们。这就是您所看到的错误的原因。

有几种方法可以解决这个问题:

  1. 为驱动程序可访问的副本集配置使用 IP 地址或主机名(推荐)。
  2. 在不指定副本集的情况下连接到其中一个节点,实质上将该节点视为独立节点(不推荐)。

如果较旧的驱动程序可以毫无怨言地连接,那么要么驱动程序非常过时,要么不遵循正确的 SDAM 规范,不应使用,因为无法保证其行为。 MongoDB 发布 SDAM 规范并强制所有驱动程序遵循它是有充分理由的。