gpt4 book ai didi

python - 从 Google Colaboratory 连接到 Mongodb Atlas 时出错

转载 作者:行者123 更新时间:2023-12-05 07:29:47 25 4
gpt4 key购买 nike

我无法从 Google colab 笔记本连接到 Mongodb Atals。建立连接不会返回任何错误,但任何读取或写入数据的尝试都会返回错误:

[Errno -5] 没有与主机名关联的地址。

下面是我使用的简单代码。谁能帮忙?

client = pymongo.MongoClient("mongodb://[username]:[password]@mongodb-ihjrm.mongodb.net/?retryWrites=true")
db = client.mytestdb
mycol = db.mytestcol

mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)



---------------------------------------------------------------------------
ServerSelectionTimeoutError Traceback (most recent call last)
<ipython-input-18-a498fc98ed98> in <module>()
1 mydict = { "name": "John", "address": "Highway 37" }
2
----> 3 x = mycol.insert_one(mydict)

/usr/local/lib/python3.6/dist-packages/pymongo/collection.py in insert_one(self, document, bypass_document_validation, session)
691 write_concern=write_concern,
692 bypass_doc_val=bypass_document_validation,
--> 693 session=session),
694 write_concern.acknowledged)
695


/usr/lib/python3.6/contextlib.py in __enter__(self)
79 def __enter__(self):
80 try:
---> 81 return next(self.gen)
82 except StopIteration:
83 raise RuntimeError("generator didn't yield") from None



/usr/local/lib/python3.6/dist-packages/pymongo/topology.py in get_server_session(self)
425 any_server_selector,
426 self._settings.server_selection_timeout,
--> 427 None)
428 elif not self._description.readable_servers:
429 self._select_servers_loop(

/usr/local/lib/python3.6/dist-packages/pymongo/topology.py in _select_servers_loop(self, selector, timeout, address)
197 if timeout == 0 or now > end_time:
198 raise ServerSelectionTimeoutError(
--> 199 self._error_message(selector))
200
201 self._ensure_opened()

ServerSelectionTimeoutError: mongodb-ihjrm.mongodb.net:27017: [Errno -5] No address associated with hostname

最佳答案

进入mongo atlas -> Security->Database accesses,修改用户角色为atlasAdmin。完美运行!!!!!!

关于python - 从 Google Colaboratory 连接到 Mongodb Atlas 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52606202/

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