0

I updated my backend deployment (Flask app) with a new feature not related to database logic, and suddenly MongoDB Atlas connections no longer work. Before the update, everything was fine.

Local environment still works → only deployed instance fails.


✅ App startup (normal)

Environment loads correctly:

MONGODB_URI: mongodb+srv://...
DB_NAME: expTracker

App + scheduler boot normally, health checks pass.


❌ Error during DB call

Calling db.users.find_one() fails:

pymongo.errors.ServerSelectionTimeoutError:
... timed out ...
topology_type: ReplicaSetNoPrimary
server_type: Unknown
ac-xxx-shard-00-00.mongodb.net:27017 timed out
ac-xxx-shard-00-01.mongodb.net:27017 timed out
ac-xxx-shard-00-02.mongodb.net:27017 timed out

So no shard becomes primary; all time out.

No code or URI changes were made.


Code Snippets

MONGO_CONNECTION_ARGS = {
 "tls": True,
 "tlsDisableOCSPEndpointCheck": True, # This was in the original, keeping it
 "serverSelectionTimeoutMS": 8000,
 "connectTimeoutMS": 5000,
 "socketTimeoutMS": 10000,
}
def get_db():
 if 'db_client' not in g:
 uri = current_app.config['MONGODB_URI']
 # This MongoClient constructor fails in deployment
 g.db_client = MongoClient(uri, **MONGO_CONNECTION_ARGS)
 g.db = g.db_client[current_app.config['DB_NAME']]
 return g.db

What I’ve checked

✅ Same URI works locally
✅ Env vars printing correctly
✅ Service starts successfully
❌ Deployment instance cannot query MongoDB

3
  • This looks like a connection failure - any additional detail in the error messages? Commented Nov 9 at 3:13
  • Just ServerSelectionTimeoutError with topology_type=ReplicaSetNoPrimary, and all shard hosts time out. also, i’m running on Koyeb. if I change the MongoDB user password, it works once, but after the next redeploy it fails again with the same timeout. Commented Nov 9 at 3:35
  • What is your connection string (without password)? Commented Nov 9 at 7:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.