简体   繁体   中英

Error connecting to Mongodb Atlas from Google Colaboratory

I am unable to connect to Mongodb Atals from Google colab notebooks. 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.

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. Works perfectly!!!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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