pymongo MongoClient connect to ReplicaSet

…衆ロ難τιáo~ 提交于 2019-12-08 20:30:06

问题


I adopted pymongo's MongoClient class to do connect to a replicaset which has three node, 1 primary 2 secondary. The code snippet as following:

c = MongoClient([secondary1_hostname, secondary2_hostname], replicaSet='rs0')

When check the three mongod's log, I found there is always a connection created to the primary host, but other 2 secondary not received the connection request from client or got connection immediately disconnected. Seems the client first reached one secondary got the primary address then dropped the connection and created long-term connection to primary.

However, when I use MongoReplicaSetClient class, with the follwing code sinppet:

c = MongoReplicaSetClient(secondary1_name, replicaSet='rs0')

There are always 3 connection created to each replica set member, got from the mongod's log file.

So, why the behavior of MongoClient is always only create connection to the primary? I read the manual of PyMongo, but didn't find the answer. Any suggestion is appreciated.


回答1:


MongoClient is for single connections only, and when speaking to MongoD it will chose the last in the list of databases. When adding a replicaSet pymongo it will verify that the replica set it connects to matches this name. Implies that the hosts specified are a seed list and pymongo should attempt to find all members of the set, then it will connect to the Primary node.

Another reason MongoClient accepts multiple hosts is for handling Mongos and high availability: http://api.mongodb.org/python/current/examples/high_availability.html#high-availability-and-mongos MongoClient also handles replicaset configurations for when speaking to a replicaSet via Mongos.

MongoReplicaSetClient is specifically for replicaset connections, it attempts to find all members of the set. It also launches the replica-set monitor, which allows it to quickly respond to changes in replica set configuration.




回答2:


With current pymongo (=3.2):

c = pymongo.MongoClient('mongodb://user:passwd@node1:p1,node2:p2/?replicaSet=rsname')
time.sleep(2)
print c.nodes
frozenset([(u'node1', p1), (u'node2', p2)])

As explained in pymongo high availability documentation:

The addresses passed to MongoClient() are called the seeds. As long as at least one of the seeds is online, MongoClient discovers all the members in the replica set, and determines which is the current primary and which are secondaries or arbiters.




回答3:


The MongoClient class only connects to a single host, as the documentation (http://api.mongodb.org/python/current/api/pymongo/mongo_client.html) says:

Create a new connection to a single MongoDB instance at host:port.

You need to use the MongoReplicaSetClient class that you've already discovered to work with a Replica Set.



来源:https://stackoverflow.com/questions/17852641/pymongo-mongoclient-connect-to-replicaset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!