简体   繁体   English

使用 pymongo 连接到 Atlas MongoDb - ServerSelectionTimeoutError

[英]Connecting to Atlas MongoDb using pymongo - ServerSelectionTimeoutError

I'm having trouble connecting to a db I set up in Atlas MongoDB.我无法连接到我在 Atlas MongoDB 中设置的数据库。 I have already read multiple other questions but no answer made my connection work.我已经阅读了多个其他问题,但没有答案使我的连接正常工作。 I want to point out that I am trying to access it behind a company's proxy (if that has anything to do with it).我想指出我正试图在公司的代理后面访问它(如果这与它有任何关系)。 My code is this and fails only on the last line where I try to count the documents:我的代码是这样的,并且仅在我尝试计算文档的最后一行失败:

from pymongo import MongoClient


client = MongoClient(
    "mongodb+srv://name:pass@iliastrialcluster-1tl2y.azure.mongodb.net/test?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE"
)
db = client.get_database('sample_airbnb')
print(db)
listings_and_reviews = db.listingAndReviews
print(listings_and_reviews)
listings_and_reviews.count_documents({})

The error I get is:我得到的错误是:

Traceback (most recent call last):
  File "C:~/mongo_connection1.py", line 11, in <module>
    listings_and_reviews.count_documents({})
  File "C:~\lib\site-packages\pymongo\collection.py", line 1721, in count_documents
    _cmd, self._read_preference_for(session), session)
  File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1454, in _retryable_read
    read_pref, session, address=address)
  File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
    server = topology.select_server(server_selector)
  File "C:~\lib\site-packages\pymongo\topology.py", line 235, in select_server
    address))
  File "C:~\lib\site-packages\pymongo\topology.py", line 193, in select_servers
    selector, server_timeout, address)
  File "C:~\lib\site-packages\pymongo\topology.py", line 209, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: iliastrialcluster-shard-00-00-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-02-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-01-1tl2y.azure.mongodb.net:27017: timed out

ServerSelectionTimeout error is the client's way of telling you you can't connect to the server. ServerSelectionTimeout错误是客户端告诉您无法连接到服务器的方式。 The primary cause of these errors when using MongoDB Atlas is the failure to enable access for the IP address of the node the client is running on.使用 MongoDB Atlas 时出现这些错误的主要原因是未能启用对运行客户端的节点的 IP 地址的访问

To verify try connecting using the MongoDB shell mongo using the same connection string.要验证尝试使用 MongoDB shell mongo使用相同的连接字符串进行连接。 if you get a connection failed then you know it's not a Python problem.如果连接失败,那么您知道这不是 Python 问题。 Your client code looks OK BTW so I am pretty sure this is what it is.顺便说一句,您的客户端代码看起来不错,所以我很确定这就是它。

The connection to the server is lazily evaluated so we don't try to initiate a connection until you make an actual request.与服务器的连接是延迟评估的,因此在您发出实际请求之前,我们不会尝试启动连接。 In this case the count_documents call.在这种情况下, count_documents调用。 This is why this is the call that generates the error.这就是为什么这是生成错误的调用。

@JoeDrumgoole Thanks for your answer. @JoeDrumgoole 感谢您的回答。 The problem was the proxy after all, after deactivating it the connection works fine.毕竟问题是代理,在停用它后连接工作正常。 Good to know that the connection to the server is lazily evaluated.很高兴知道与服务器的连接是延迟评估的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM