Forums

connect to mongodb atlas

hey there,

I try to write to mongo db atlas. Connection and stuff works just fine and the code runs perfect on my local machine. but when i run it at pythonanywehere i get these messages:

Traceback (most recent call last):
  File "scraping.py", line 25, in <module>
    records.insert_one({'ipsum':'lorum'})
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 698, in insert_one
    session=session),
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 612, in _insert
    bypass_doc_val, session)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 600, in _insert_one
    acknowledged, _insert_command, session)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 1491, in _retryable_write
    with self._tmp_session(session) as s:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 1820, in _tmp_session
    s = self._ensure_session(session)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 1807, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 1760, in __start_session
    server_session = self._get_server_session()
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 1793, in _get_server_session
    return self._topology.get_server_session()
  File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 482, in get_server_session
    None)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 205, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed

.

processed_soup = make_soup(url)
ticket = Ticket(processed_soup)

db = client.get_database(ticket.get_event_name()[:30])
records = db[ticket.get_ticket()]
records.insert_one({'ipsum':'lorum'})

[edit by admin: formatting]

The MongoDB doesn't actually connect until you try to read from or write to the database, so I think this is actually a general connection failure. Have you set up your Atlas DB so that it accepts connections from our IPs? There are some hints and tips on doing that here.

Aah, yes, thanks, it works now :)