ServerSelectionTimeoutError:通过pymongo连接到Atlas时发生超时错误

时间:2019-05-16 19:17:30

标签: mongodb pymongo mongodb-atlas

我正在尝试通过pymongo连接到我的Atlas mongodb数据库。我进行连接并进行基本查询,只是为了计算文档数并超时。

我能够在个人linux上运行相同的字符串(并设法从一个干净的Docker使其运行),但是却无法从我用于工作的Mac上使其运行(我的同事也没有)而且我也无法使其在干净的Docker映像中运行)。如果有问题,我正在运行pymongo 3.8,请确保我已安装pip install pymongo[tls]。我也尝试过降级,也尝试过pip install pymongo[tls,srv]

个人猜测:可能与代理/防火墙阻止连接有关?我检查了端口是否打开。在服务器上,我将0.0.0.0/0列入了白名单,所以这应该不是问题。

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

我收到以下错误

/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

0 个答案:

没有答案