簡體   English   中英

從 Google Colaboratory 連接到 Mongodb Atlas 時出錯

[英]Error connecting to Mongodb Atlas from Google Colaboratory

我無法從 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。 完美運行!!!!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM