I applied the pymongo MongoClient class to connect to replication, which has three nodes, 1 primary, 2 secondary. The code snippet is as follows:
c = MongoClient([secondary1_hostname, secondary2_hostname], replicaSet='rs0')
When you check the three mongod logs, I find that there is always a connection created for the primary host, but the other 2 secondary ones did not receive the connection request from the client or immediately disconnected the connection. It seems that the client first reached one secondary, received the primary address, and then dropped the connection and created a long-term connection with the primary.
However, when I use the MongoReplicaSetClient class with the following code: sinppet:
c = MongoReplicaSetClient(secondary1_name, replicaSet='rs0')
For each member of the replica set, 3 connections are always created from the mongod log file.
So why does MongoClient's behavior always create a relationship with the primary? I read the PyMongo manual but did not find an answer. Any suggestion is appreciated.
source share