Hi all,
I have configured a replica set with primary ‘n1’ (default port), second ‘n2’ and arbiter n1:27018. n1 and n2 are configured in /etc/hosts and are resolved as IPs. My replica set also uses those names with no issue.
When both nodes are up all is working as expected. However if n1 goes down , PyMongo can’t read the DB and fails with timeout. It is important to emphasise that in mongo-shell the DB is responding properly, the problem is from Python and pymongo more specifically.
I’ve read everything I managed to find on the topic and appears the proper way to read from the secondary in such case is to set ReadPreference to ‘secondary’, but this approach also times out and fails.
Never mind my code , I’m trying very basic test directly in a python shell - here is what I did:
from pymongo import ReadPreference
from pymongo import MongoClient
uri = “mongodb://n1:27017,n2:27017,n1:27018/?replicaSet=rs0” # n1:27018 is my arbiter
client = MongoClient(uri, socketTimeoutMS=6000,connectTimeoutMS=6000,readPreference=‘secondary’)
configdata = client.get_database(‘audiobridge’, read_preference=ReadPreference.SECONDARY)
When I print configdata it shows:
Database(MongoClient(host=[‘n1:27017’, ‘n2:27017’, ‘n1:27018’], document_class=dict, tz_aware=False, connect=True, replicaset=‘rs0’, sockettimeoutms=6000, connecttimeoutms=6000, readpreference=‘secondary’), ‘audiobridge’)
And yet when I try :
configdata.ha.find_one({‘ha_exists’: True})
it crashes with :
pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector “Secondary(tag_sets=None, max_staleness=-1, hedge=None)”, Timeout: 30s, Topology Description: <TopologyDescription id: 6756cc9f09429c36ed83449d, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (‘n1’, 27017) server_type: Unknown, rtt: None, error=NetworkTimeout(‘n1:27017: timed out (configured timeouts: socketTimeoutMS: 6000.0ms, connectTimeoutMS: 6000.0ms)’)>, <ServerDescription (‘n1’, 27018) server_type: Unknown, rtt: None, error=NetworkTimeout(‘n1:27018: timed out (configured timeouts: socketTimeoutMS: 6000.0ms, connectTimeoutMS: 6000.0ms)’)>, <ServerDescription (‘n2’, 27017) server_type: RSOther, rtt: 0.0010362059028441717>]>
Any suggestions , ideas are much appreciated !