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. 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
Blockquote
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)
Blockquote
When I print configdata it shows:
Blockquote
Blockquote
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’)
Blockquote
And yet when I try :
Blockquote
Blockquote
configdata.ha.find_one({‘ha_exists’: True})
Blockquote
it crashes with :
Blockquote
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>]>
Blockquote
Any suggestions , ideas are much appreciated