MONGO_INITDB_ROOT_USERNAME ignored (data volume exists) MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: MongoDB Docker

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

  1. Dev: docker compose down -v && docker compose up -d
  2. Prod: db.changeUserPassword("root","new") in mongosh
  3. Check authSource=admin

Detailed fix by platform

Docker

  1. docker compose exec mongo mongosh -u root -p --authenticationDatabase admin

How to diagnose

  1. Volume — Existing data?
  2. Init — Scripts run?
  3. 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:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…Authentication token manipulation error — passwd: Authentication token manipulation error (Linux)Authentication token …bad auth : Authentication failed — MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18) (MongoDB)bad auth : Authentica…BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…denied — denied: requested access to the resource is denied (docker push) (Docker)denieddocker-credential-desktop — error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH (Docker)docker-credential-des…ECR token expired — denied: Your authorization token has expired. Reauthenticate and try again. (Amazon ECR) (AWS)ECR token expiredError saving credentials — Error saving credentials: error storing credentials - err: exit status 1 (Docker)Error saving credenti…MONGO_INI…

Explore the map of this error Full error map →

🧠 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.