Docker: MongoServerError: Authentication failed — after changing MONGO_INITDB_ROOT_PASSWORD
The official Mongo image creates the root user only on the very first start with an empty data directory; changing env vars later has no effect.
MongoServerError: Authentication failed docker mongo MONGO_INITDB_ROOT_PASSWORD changedMONGO_INITDB_ROOT_USERNAME not working existing volumedocker-entrypoint-initdb.d scripts not running mongoMongo docker root user not created Meaning
Env vars MONGO_INITDB_ROOT_* and scripts in /docker-entrypoint-initdb.d run only when /data/db is empty. Remove the volume (docker compose down -v) for a fresh init in dev, or create/update users manually with db.createUser / db.changeUserPassword.
Common causes
- Persistent volume already initialised
- Password changed in compose later
- initdb scripts not re-run
- authSource mismatch
- User created in another database
⚡ Quick fix
- Dev:
docker compose down -v && docker compose up -d - Prod:
db.changeUserPassword("root","new")in mongosh - Check
authSource=admin
Detailed fix by platform
Docker
docker compose exec mongo mongosh -u root -p --authenticationDatabase admin
How to diagnose
- Volume — Existing data?
- Init — Scripts run?
- authSource — admin?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Atlas: bad auth: Authentication failed MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password)
- Authentication token manipulation error passwd: Authentication token manipulation error
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
Most viewed in MongoDB
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
- MongoParseError MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
- StrictPopulateError: Cannot populate path because it is not in your schema Mongoose 6+: StrictPopulateError: Cannot populate path `author` because it is not in your schema
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026