简体   繁体   English

从 Google Colaboratory 连接到 Mongodb Atlas 时出错

[英]Error connecting to Mongodb Atlas from Google Colaboratory

I am unable to connect to Mongodb Atals from Google colab notebooks.我无法从 Google colab 笔记本连接到 Mongodb Atals。 Establishing the connection does not return any errors, but any attempt to read or write data returns the error:建立连接不会返回任何错误,但任何读取或写入数据的尝试都会返回错误:

[Errno -5] No address associated with hostname. [Errno -5] 没有与主机名关联的地址。

Below is the simple code I am using.下面是我正在使用的简单代码。 Can anyone help?任何人都可以帮忙吗?

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

Go to mongo atlas -> Security->Database accesses and change the role of the user to atlasAdmin.转到 mongo atlas -> Security->Database accesses 并将用户的角色更改为 atlasAdmin。 Works perfectly!!!!!!完美运行!!!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM