简体   繁体   English

尝试连接到 CosmosDB 数据库时出现 pymongo.errors.ServerSelectionTimeoutError

[英]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.

相关问题 pymongo.errors.ServerSelectionTimeoutError - pymongo.errors.ServerSelectionTimeoutError 烧瓶 - pymongo.errors.ServerSelectionTimeoutError - Flask - pymongo.errors.ServerSelectionTimeoutError Pymongo pymongo.errors.ServerSelectionTimeoutError 使用示例代码时 - Pymongo pymongo.errors.ServerSelectionTimeoutError when using example code pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas - pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas 烧瓶 Web 服务中的 Mongodb 错误 [pymongo.errors.ServerSelectionTimeoutError] - Mongodb error [pymongo.errors.ServerSelectionTimeoutError] in flask web-service 连接到 mongo 图集给出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017 - Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017 pymongo.errors.ServerSelectionTimeoutError:本地主机:27017:[Errno 61]连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused pymongo.errors.ServerSelectionTimeoutError 错误与 MongoDB 图集和 Flask - pymongo.errors.ServerSelectionTimeoutError error with MongoDB atlas and Flask pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description: - pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM