Forums

Connection refused with Flask Rest API on MongoDB Atlas database

I get a "connection refused" error when trying to connect to a mongodb atlas database ("pymongo.errors.ServerSelectionTimeoutError: connection closed")

I have upgraded to the hacker package, to be able to access more than just the whitelist (https://www.pythonanywhere.com/whitelist/)

I have a free mongodb atlas database which i want to access via a flask rest api. The code can be found here (https://pastebin.com/ejaKJBEa).

The most important snippet is here:

APP = Flask(__name__)

APP.config['DEBUG'] = True
APP.config['MONGO_URI'] = "mongodb://myusername:mypassword@myclustername:27017/TestDB?authSource=admin&ssl=true"

MONGO = PyMongo(APP)

The python code works fine when i start it at my own home pc and i can access the database and execute the rest api methods without problems. I think there might an issue in the ssl connection, but maybe there is other stuff, that you guys can point me at.

How to avoid the error and what does the error exactly mean?

This forum post looks like someone solving the same problem.

Sadly, i am not able to find a solution with that post.

I tried to add maxPoolSize=1 at the end, but it does not help. This code runs fine in a console on my own pc, but it neither the web app nor a console call of the file does work. Are there more options to be considered?

I might add the error here as well:

2018-02-06 21:10:36,907: Error running WSGI application
2018-02-06 21:10:36,917: pymongo.errors.ServerSelectionTimeoutError: connection closed
2018-02-06 21:10:36,917:   File "/var/www/tschm_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-02-06 21:10:36,917:     from rest import APP as application
2018-02-06 21:10:36,918: 
2018-02-06 21:10:36,918:   File "/home/tschm/mysite/rest.py", line 18, in <module>
2018-02-06 21:10:36,918:     MONGO = PyMongo(APP)
2018-02-06 21:10:36,918: 
2018-02-06 21:10:36,919:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask_pymongo/__init__.py", line 97, in __init__
2018-02-06 21:10:36,919:     self.init_app(app, config_prefix)
2018-02-06 21:10:36,919: 
2018-02-06 21:10:36,919:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask_pymongo/__init__.py", line 283, in init_app
2018-02-06 21:10:36,919:     mechanism=auth_mechanism)
2018-02-06 21:10:36,919: 
2018-02-06 21:10:36,920:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/pymongo/database.py", line 1167, in authenticate
2018-02-06 21:10:36,920:     connect=True)
2018-02-06 21:10:36,920: 
2018-02-06 21:10:36,920:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 583, in _cache_credentials
2018-02-06 21:10:36,920:     writable_preferred_server_selector)
2018-02-06 21:10:36,921: 
2018-02-06 21:10:36,921:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/pymongo/topology.py", line 224, in select_server
2018-02-06 21:10:36,921:     address))
2018-02-06 21:10:36,921: 
2018-02-06 21:10:36,921:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/pymongo/topology.py", line 183, in select_servers
2018-02-06 21:10:36,921:     selector, server_timeout, address)
2018-02-06 21:10:36,922: 
2018-02-06 21:10:36,922:   File "/home/tschm/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
2018-02-06 21:10:36,922:     self._error_message(selector))

just to confirm- when you run this not as a webapp but from the pythonanywhere console, does it work? in particular, using new consoles after your upgrade instead of old ones before the upgrade.

Here's a help page about MongoDB on PythonAnywhere that hopefully will be useful for anyone with questions about this in the future -- I think the OP on this thread needed the extra connection parameters. Any suggestions for extra stuff we should put on there much appreciated!