我无法从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
答案 0 :(得分:0)
转到 mongo atlas -> Security->Database accesses 并将用户的角色更改为 atlasAdmin。完美运行!!!!!!