MongoDB default username and password
Codemia Knowledge Hub ·
MongoDB has no default username or password. On a fresh install, authentication is disabled entirely, and any client that can reach the server can read and write any database without credentials. That is convenient for local development but a critical security risk for any networked or production deployment.
The first step after installing is to create an admin user, enable authentication and restart the server with access control enforced.
Why there are no default credentials
MySQL has a root user with an empty or generated password, and PostgreSQL uses OS-level peer authentication. MongoDB ships with no users at all, and the authentication system stays off until you enable it.
| State | Who can connect | What they can do |
| Fresh install (default) | Anyone who can reach the network port | Full read/write to all databases |
| After enabling auth | Only users with valid credentials | Only what their roles permit |
MongoDB listens on localhost:27017 by default, which limits exposure to a single machine. If bindIp is changed to 0.0.0.0 or a public interface before authentication is enabled, the database is open to the network.
Creating the first admin user
Step 1: connect without authentication
No users exist yet, so connect to the running mongod directly:
Step 2: create the admin user
Switch to the admin database and create a user with administrative privileges:
passwordPrompt() keeps the password out of your shell history. To script user creation, pass the password directly:
Step 3: enable authentication
Edit the configuration file (typically /etc/mongod.conf on Linux or mongod.cfg on Windows):
Then restart MongoDB:
After the restart, unauthenticated connections are rejected.
Connecting with credentials
From the shell
-p without a value prompts for the password. Passing it inline exposes it in process listings and shell history:
With a connection string
authSource=admin tells MongoDB which database holds the user. Without it, authentication fails even with correct credentials, because MongoDB looks for the user in the database you are connecting to.
From application code
Creating application users with scoped roles
Applications should not use the admin user. Create users with the minimum privileges they need:
| Role | Scope | Permissions |
read | Single database | Read-only access |
readWrite | Single database | Read and write access |
dbAdmin | Single database | Schema management, indexing, stats |
userAdmin | Single database | Create and manage users |
readWriteAnyDatabase | All databases | Read/write across all databases |
userAdminAnyDatabase | All databases | Manage users across all databases |
root | All | Superuser (use sparingly) |
Setting credentials in Docker
The official MongoDB image accepts initial credentials as environment variables:
This creates the root user and enables authentication automatically, with no manual user creation step.
In Docker Compose:
JavaScript files mounted into /docker-entrypoint-initdb.d/ run on first start, which is a convenient place to create application users and databases:
MongoDB Atlas
The managed Atlas service has no server-level defaults. You create database users through the web console or API before any client can connect, and Atlas enforces network access lists (IP allowlists) by default.
Atlas users are managed separately from the database's built-in user system, and authentication is always on.
Production security checklist
- Authentication is enabled in
mongod.conf(security.authorization: enabled) - Admin user exists with a strong password
- Application users are scoped to specific databases with minimal roles
- Network binding is restricted (
bindIpset to specific interfaces, not0.0.0.0) - TLS/SSL is enabled for encrypted connections
- Firewall rules restrict access to port 27017
- Audit logging is enabled for compliance requirements
Mistakes that cause lockouts and breaches
- Trying
admin/adminorroot/root. There are no built-in credentials of any kind. - Leaving out
authSource=admin. You get "authentication failed" even with the right username and password, because the default auth source is the target database. - Enabling authentication before creating a user. That locks you out. Create the admin user first, then enable auth and restart.
- Exposing MongoDB to the internet without auth (
bindIp: 0.0.0.0). This has led to numerous data breaches. Botnets actively scan port 27017 for open instances. - Connecting applications as admin or root. This violates least privilege. Give each application its own scoped user.
- Hardcoding passwords in connection strings that end up in source control or logs. Use environment variables, a secrets manager, or
passwordPrompt()interactively.
Related reading
- MongoDB won't start after server crash
- MongoDB/Mongoose querying at a specific date?
- MongoDB/NoSQL Keeping Document Change History
- Mongodump from remote server
- Mongoose password hashing
- Mounting NFS Persistent Volumes with authentication
- MongoError connect ECONNREFUSED 127.0.0.127017
- mongoError Topology was destroyed
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.