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
-
This looks like a connection failure - any additional detail in the error messages?Joe– Joe2025年11月09日 03:13:56 +00:00Commented 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.SOEUNG Phearaneron– SOEUNG Phearaneron2025年11月09日 03:35:14 +00:00Commented Nov 9 at 3:35
-
What is your connection string (without password)?Wernfried Domscheit– Wernfried Domscheit2025年11月09日 07:32:51 +00:00Commented Nov 9 at 7:32