Forums

PyMongo with MongoDB Atlas

I've just opened a free account. I have an app which needs to connect to an existing MongoDB database hosted by MongoDB Atlas cloud service.

I have just tried connecting using PA and got a connection refused error. Is this because I am using a free account? Do you know if this will work with a paid account?

Thanks

Jules

To answer my own question - I just switched to a Hacker account and ran my test script again, and all worked fine!

Thanks for upgrading! Yup, that was the problem. Free accounts can only access external sites via HTTP and HTTPS (which rules out Mongo) and also can only access sites on a whitelist. Paid accounts get unrestricted Internet access.

Thanks Giles!

Found a new issue now - my test script, which connects to my MongoDB Atlas instance and inserts one document - it runs fine when I run it manually via python 3.6 console. When i run it as a scheduled task, it fails with the error below.

Any idea why I would get a different outcome when running as a scheduled task?

Traceback (most recent call last): File "/home/julesjoseph/ReTweeter/daily-UserSummary.py", line 23, in <module> result = db.followers.insert_one(dailyCountRecord).inserted_id File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 622, in insert_one with self._socket_for_writes() as sock_info: 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 716, in _get_socket server = self._get_topology().select_server(selector) File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 142, in select_server address)) File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 118, in select_servers self._error_message(selector)) pymongo.errors.ServerSelectionTimeoutError: No replica set members found yet

That's running under Python 2.7, which may have a different version of the mongo libraries. Try running it in the same Python environment as your console tests: http://help.pythonanywhere.com/pages/ScheduledTasks/

Also, you might need some extra connection parameters if you're using it in a website's code. Here's a help page about MongoDB on PythonAnywhere that hopefully will be useful. Any suggestions for extra stuff we should put on there much appreciated!