gpt4 book ai didi

python - 使用示例代码时出现 Pymongo pymongo.errors.ServerSelectionTimeoutError

转载 作者:太空宇宙 更新时间:2023-11-03 20:11:04 28 4
gpt4 key购买 nike

我正在尝试运行非常简单的代码来弄清楚如何将 pymongo 与 MongoDB Atlas Cloud 结合使用。

这是示例代码

import pymongo

client = pymongo.MongoClient("mongodb+srv://{myusername}:{mypassword}@cluster0-uywu8.mongodb.net/test?retryWrites=true&w=majority")
db = client.BroadwayMatch
print(db)
collection = db.Artists
print(collection)

print(collection.insert_one({'x': 1}))

BroadwayMatch 和 Artists 是我上周能够插入的现有数据库和集合,我不确定发生了什么变化。它似乎成功连接到数据库和集合,但由于某种原因无法读取或写入它。可以访问集合的所有属性,但所有方法都会导致 ServerSelectionTimeoutError。这是此代码片段的输出

Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch')
Collection(Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch'), 'Artists')
Traceback (most recent call last):
File "C:\Python37\Spotify-Match\mongotest.py", line 10, in <module>
print(collection.insert_one({'x': 1}))
File "C:\Python37\lib\site-packages\pymongo\collection.py", line 700, in insert_one
session=session),
File "C:\Python37\lib\site-packages\pymongo\collection.py", line 614, in _insert
bypass_doc_val, session)
File "C:\Python37\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
acknowledged, _insert_command, session)
File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1279, in _retryable_write
with self._tmp_session(session) as s:
File "C:\Python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1611, in _tmp_session
s = self._ensure_session(session)
File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1598, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1551, in __start_session
server_session = self._get_server_session()
File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1584, in _get_server_session
return self._topology.get_server_session()
File "C:\Python37\lib\site-packages\pymongo\topology.py", line 434, in get_server_session
None)
File "C:\Python37\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed

我不确定我做错了什么,有人可以帮忙吗?

最佳答案

您可能无法连接到 Atlas 服务器的原因:

  • 您的white list尚未启用您当前的 IP 地址
  • 您使用了错误的用户名和/或密码。在您的情况下,您的 fString 似乎在开头缺少 f 。

诊断这些情况时,剪切并粘贴 MongoDB shell 的 MongoDB Atlas 连接字符串(见下文)或 MongoDB Compass通常会暴露用户名和/或密码错误。

Screenhost of MongoDB Atlas Connection screen dialog for MongoDB Shell

关于python - 使用示例代码时出现 Pymongo pymongo.errors.ServerSelectionTimeoutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58715984/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com