MongoDB Atlas Auth connection fails in python3 cron job

I am running a cron job that does some database updation everyday. I am using MongoDB Atlas. My script is running perfectly fine in my local device.

But when running it on render it returns the following error in everyline a request is sent to MongoDB:

pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}

Here is the link to my python code: (Pastery - The sweetest pastebin in the world!)

I am using the same python version that I am using on local, that is, 3.8.9 and have also installed all the dependancies by generating a requirements file using pip freeze command.

Hey @suryansh,

The error seems to indicate that there is something wrong with the credentials that are being passed into MongoClient when it is instantiated. Can you verify that all of those values are identical between the environments? It also looks like IP access restrictions are required to use MongoDB Atlas. Have you configured this to allow your Render service to access your database? Render services don’t have a static IPs to allow, but we have many customers using QuotaGuard when a static IP is required.

I got a similiar issue using rails, I’ve created a topic here: MongoDB authentication error

@suryansh Could you verify if the solution in the link Thomas posted above solves the problem for you - or at least works?

It does solve my problem!