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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With