Search code examples
pythoneve

How to add MongoDB Atlas URI to Python Eve application


I am trying to connect MongoDB Atlas Cluster using Python Eve Framework. However, It keeps giving me Authentication error when I have provided both username and password.

Here is my settings.py

MONGO_URI = "mongodb://<username>:<password>@cluster0-shard-00-00.0fjja.mongodb.net:27017,cluster0-shard-00-01.0fjja.mongodb.net:27017,cluster0-shard-00-02.0fjja.mongodb.net:27017/ocr_pipeline?ssl=true&replicaSet=atlas-eiajbk-shard-0&authSource=admin&retryWrites=true&w=majority"

# Skip this block if your db has no auth. But it really should.
# Name of the database on which the user can be authenticated,
# needed if --auth mode is enabled.
MONGO_AUTH_SOURCE = 'eve'
MONGO_DBNAME = 'test'

I have tried to go through multiple stackoverflow question on this but none seem to work. This setting works well with localhost but throws auth error when working with MongoDB Atlas


Solution

  • Using "DNS seed" format for an Atlas cluster to set eve's MONGO_URI like below works OK for me:

    MONGO_URI = "mongodb+srv://username:[email protected]/your_database?retryWrites=true&w=majority"
    

    You should find your cluster's connection string in Atlas UI.

    See MongoDB Atlas deployment in connection string examples for more:(https://www.mongodb.com/docs/manual/reference/connection-string/)