Forums

Can't connect to mongodb though webapp

Hello.

I'm trying to connect my Flask app to a mongodb free instance on https://cloud.mongodb.com.

I already read the help page https://help.pythonanywhere.com/pages/MongoDB but still get the error:

pymongo.errors.ServerSelectionTimeoutError: <INSTANCENAME>-shard-00-02-apcnd.mongodb.net:27017: [Errno 111] Connection refused,<INSTANCENAME>-shard-00-01-apcnd.mongodb.net:27017: [Errno 111] Connection refused,<INSTANCENAME>-shard-00-00-apcnd.mongodb.net:27017: [Errno 111] Connection refused

My code is:

from pymongo import MongoClient
client = MongoClient('mongodb+srv://<LOGIN>:<PASSWORD>@<INSTANCENAME>-apcnd.mongodb.net/test?retryWrites=true&w=majority', connectTimeoutMS=30000, socketTimeoutMS=None, socketKeepAlive=True, connect=False, maxPoolsize=1)
db = client.newdatabase 
col = db.newcollection

Additionally: - whitelisted the CIDR 0.0.0.0/0 on my mongoDB instance (can access it directly from my pc) - installed package pymongo[srv]

Thank you.

edit: title through /:

Free accounts can only connect out of PythonAnywhere using http(s) to a whitelist of sites. cloud.mongodb.com is on the whitelist, so you can use the mongo http api, but not the server connection (like you're trying to do).