简体   繁体   English

PyMongo MongoClient副本集无法连接

[英]PyMongo MongoClient replica set won't connect

A bit of background: I used Bitnami to spin up a 3 node Mongo cluster on Azure (1 arbiter), each mongod hosted on separate VMs. 背景知识:我使用Bitnami在Azure(1个仲裁器)上启动了一个3节点的Mongo群集,每个mongod托管在单独的VM上。 I've confirmed that the replica set exists, and that each node is able to connect to one another. 我已经确认副本集存在,并且每个节点都可以相互连接。 I've confirmed that when I take down my primary node, the secondary node steps up. 我已经确认,当我卸下主要节点时,次要节点会升高。 When the primary node returns, it assumes the primary position again. 当主节点返回时,它将再次占据主位置。

My problem is that I can't connect to my MongoDB replica set using MongoClient when specifying replicaset. 我的问题是指定副本集时,无法使用MongoClient连接到我的MongoDB副本集。 I would get this error: 我会收到此错误:

pymongo.errors.ServerSelectionTimeoutError: ArbiterIP:27017: [WinError 10061] No connection could be made because the target machine actively refused it,PrimaryIP:27017: [WinError 10061] No connection could be made because the target machine actively refused it,SecondaryIP:27017: timed out pymongo.errors.ServerSelectionTimeoutError:ArbiterIP:27017:[WinError 10061]无法建立连接,因为目标机器主动拒绝了,PrimaryIP:27017:[WinError 10061]无法建立连接是因为目标机器主动拒绝了,第二IP: 27017:超时

Using MongoClient, if I do: 如果使用MongoClient,请执行以下操作:

connection = MongoClient('MyIP1:27017', w=2)

, it connects fine. ,连接正常。 When I do 当我做

connection = MongoClient('MyIP1:27017', w=2, replicaset="repsetname")

, that's when I get the error. ,那是我得到错误的时间。

Would it be related to the fact that the arbiter node has no user info for authentication? 与仲裁者节点没有用于身份验证的用户信息这一事实有关吗?

Taking a stab at it: if you connect with the shell and do db.isMaster(), are the hostnames you see in the config the same as the hostnames like "MyIP" that you're passing to PyMongo? 采取行动:如果您连接外壳并执行db.isMaster(),在配置中看到的主机名是否与传递给PyMongo的主机名(如“ MyIP”)相同?

It sounds like when you pass "replicaset=", PyMongo takes the hostnames from the isMaster response and connects to those instead of MyIP, but the way your replica set is configured, that set of hostnames isn't available. 听起来像是当您传递“ replicaset =”时,PyMongo从isMaster响应中获取主机名并连接到这些主机名,而不是连接到MyIP,但是在配置副本集的方式下,该主机名集不可用。

For more info on why PyMongo acts this way: 有关为什么PyMongo这样行事的更多信息:

https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#clients-use-the-hostnames-listed-in-the-replica-set-config-not-the-seed-list https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#clients-use-the-hostnames-listed-in-the-副本集配置不是种子列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM