2

I'm trying to connect to my Atlas mongodb database through pymongo. I make the connection and do a basic query just to count the documents and it times out.

I am able to run the same string on my personal linux (and managed to get it working from a clean Docker), but did not manage to get it working from my Mac that I use for work (and neither did my colleagues and I wasn't able to make it work in a clean Docker image either). If it matters I'm running pymongo 3.8, that I installed with pip install pymongo[tls]. I tried downgrading as well and tried also pip install pymongo[tls,srv].

Personal guesses: something to do with proxy/firewall blocking the connection maybe? I checked whether the port was open On the server I whitelisted the 0.0.0.0/0 so that shouldn't be the issue.

import pymongo

client = pymongo.MongoClient("mongodb+srv://whatever:yep@cluster0-xxxxx.mongodb.net/test?retryWrites=true")
client.test.matches.count_documents({}) # this blocks and then errors

I get the following error

/usr/local/lib/python3.7/site-packages/pymongo/collection.py in count_documents(self, filter, session, **kwargs)
   1693         collation = validate_collation_or_none(kwargs.pop('collation', None))
   1694         cmd.update(kwargs)
-> 1695         with self._socket_for_reads(session) as (sock_info, slave_ok):
   1696             result = self._aggregate_one_result(
   1697                 sock_info, slave_ok, cmd, collation, session)

/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py in __enter__(self)
    110         del self.args, self.kwds, self.func
    111         try:
--> 112             return next(self.gen)
    113         except StopIteration:
    114             raise RuntimeError("generator didn't yield") from None

/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py in _socket_for_reads(self, read_preference)
   1133         topology = self._get_topology()
   1134         single = topology.description.topology_type == TOPOLOGY_TYPE.Single
-> 1135         server = topology.select_server(read_preference)
   1136
   1137         with self._get_socket(server) as sock_info:

/usr/local/lib/python3.7/site-packages/pymongo/topology.py in select_server(self, selector, server_selection_timeout, address)
    224         return random.choice(self.select_servers(selector,
    225                                                  server_selection_timeout,
--> 226                                                  address))
    227
    228     def select_server_by_address(self, address,

/usr/local/lib/python3.7/site-packages/pymongo/topology.py in select_servers(self, selector, server_selection_timeout, address)
    182         with self._lock:
    183             server_descriptions = self._select_servers_loop(
--> 184                 selector, server_timeout, address)
    185
    186             return [self.get_server_by_address(sd.address)

/usr/local/lib/python3.7/site-packages/pymongo/topology.py in _select_servers_loop(self, selector, timeout, address)
    198             if timeout == 0 or now > end_time:
    199                 raise ServerSelectionTimeoutError(
--> 200                     self._error_message(selector))
    201
    202             self._ensure_opened()

ServerSelectionTimeoutError: cluster0-shard-00-01-eflth.mongodb.net:27017: timed out,cluster0-shard-00-00-eflth.mongodb.net:27017: timed out,cluster0-shard-00-02-eflth.mongodb.net:27017: timed out
nraw
  • 83
  • 7
  • Are you able to connect to the server using `mongo` shell from the same machine using the same connection URI? – Wan B. May 29 '19 at 06:26
  • Just as a datapoint - I had the same issue where linux worked and mac did not. I could go to web UI but VPN was blocking the mongodb connections; Looks like this error indeed indicates connection problem. – Konstantin Levinski Jan 29 '20 at 07:40

0 Answers0