[英]Getting pymongo.errors.ServerSelectionTimeoutError when attempting to connect to a CosmosDB database
I'm getting a Getting pymongo.errors.ServerSelectionTimeoutError when attempting to connect to a CosmosDB database from my local computer.尝试从本地计算机连接到 CosmosDB 数据库时,我收到了 Getting pymongo.errors.ServerSelectionTimeoutError。
uri = "uri given by the CosmosDB console"
client = pymongo.MongoClient(uri)
db = client.test
collection = db.test
collection.insert_one({"field1": "foo", "field2": "bar"})
On the last statement it throws: pymongo.errors.ServerSelectionTimeoutError: connection closed在它抛出的最后一条语句上:pymongo.errors.ServerSelectionTimeoutError: connection closed
I've also tried adding the ssl_cert_reqs=CERT_NONE parameter which some blog posts recommended, but it still threw the same error.我也尝试添加一些博客文章推荐的 ssl_cert_reqs=CERT_NONE 参数,但它仍然抛出相同的错误。 I'm using pymongo 3.10.1
我正在使用 pymongo 3.10.1
I had the same issue earlier, you could add ?ssl=true&ssl_cert_reqs=CERT_NONE
after the database name in the string you are passing to MongoClient , which would look like,我之前遇到过同样的问题,您可以在传递给 MongoClient 的字符串中的数据库名称之后添加
?ssl=true&ssl_cert_reqs=CERT_NONE
CERT_NONE ,看起来像,
db_name = os.getenv("MONGO_DB")
host = os.getenv("MONGO_HOST")
port = 10255
username = os.getenv("MONGO_USERNAME")
password = os.getenv("MONGO_PASSWORD")
args = "ssl=true&retrywrites=false&ssl_cert_reqs=CERT_NONE"
connection_uri = f"mongodb://{username}:{password}@{host}:{port}/{db_name}?{args}"
Ok, figured this out.好的,想通了。 I needed to add my ip address to the firewall to allow it through.
我需要将我的 IP 地址添加到防火墙以允许它通过。 Then I got an AutoReconnect error and needed to add retrywrites=false to the connection string.
然后我收到了一个 AutoReconnect 错误,需要将 retrywrites=false 添加到连接字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.