MongoDB what are the default user and password?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MongoDB does not have a default username or password. When you install MongoDB and start it for the first time, authentication is disabled entirely. Any client that can reach the server can read and write any database without credentials. This is by design for local development convenience, but it is a critical security risk for any networked or production deployment.
The correct first step after installation is to create an admin user, enable authentication, and restart the server with access control enforced.
Why There Are No Default Credentials
Unlike databases such as MySQL (which has a root user with an empty or generated password) or PostgreSQL (which uses OS-level peer authentication), MongoDB ships with no users at all. The authentication system is simply turned off until you explicitly enable it.
This means:
| 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 on a single machine. But if bindIp is changed to 0.0.0.0 or a public interface without first enabling authentication, the database is open to the network.
Creating the First Admin User
The process has three steps: connect to the server, create a user in the admin database, and then enable authentication in the server configuration.
Step 1: Connect Without Authentication
Since no users exist yet, connect to the running mongod instance directly:
Step 2: Create an Admin User
Switch to the admin database and create a user with administrative privileges:
The passwordPrompt() function avoids putting the password in your shell history. If you need to script user creation, you can pass the password directly:
Step 3: Enable Authentication
Edit the MongoDB configuration file (typically /etc/mongod.conf on Linux or mongod.cfg on Windows):
Then restart MongoDB:
After restart, unauthenticated connections are rejected.
Connecting with Credentials
Once authentication is enabled, you must provide credentials to connect.
From the Shell
The -p flag without a value prompts for the password. You can also specify it inline, but that exposes it in process listings and shell history:
Using a Connection String
The authSource=admin parameter is essential. It tells MongoDB which database contains the user credentials. Omitting it causes authentication to fail even with correct credentials, because MongoDB defaults to looking for the user in the database being connected to.
From Application Code
Creating Application-Specific Users
The admin user should not be used by applications. Create scoped users with minimum necessary privileges:
Key Built-in Roles
| 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) |
MongoDB in Docker
The official MongoDB Docker image supports setting initial credentials through environment variables:
This creates the root user and enables authentication automatically. No manual user creation step is needed.
In Docker Compose:
Initialization Scripts
You can mount JavaScript files to /docker-entrypoint-initdb.d/ to create application-specific users and databases on first start:
MongoDB Atlas (Cloud)
If you are using MongoDB Atlas (the managed cloud service), there are no server-level defaults to worry about. Atlas requires you to create database users through the web console or API before any client can connect. Atlas also 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 enabled.
Security Checklist for Production
After moving beyond development, verify these settings:
- 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
Common Pitfalls
Assuming MongoDB has default credentials like admin/admin or root/root and spending time trying them is a common waste of effort. There are no built-in credentials of any kind.
Forgetting to specify authSource=admin in the connection string causes "authentication failed" errors even when the username and password are correct. The default auth source is the target database, not admin.
Enabling authentication before creating any user locks you out of the database. Always create the admin user first, then enable auth and restart.
Exposing MongoDB to the internet (bindIp: 0.0.0.0) without authentication has led to numerous data breaches. Botnets actively scan for open MongoDB instances on port 27017.
Using the admin/root user for application connections violates the principle of least privilege. Create dedicated users with scoped roles for each application.
Putting passwords directly in connection strings that end up in source control or logs is a security risk. Use environment variables, secrets managers, or passwordPrompt() for interactive sessions.
Summary
- MongoDB has no default username or password. Authentication is disabled on fresh installations.
- Create an admin user, enable
security.authorizationin the config file, and restart the server to enforce authentication. - Always specify
authSource=adminin connection strings when authenticating against the admin database. - Create scoped, application-specific users with the minimum necessary roles instead of sharing admin credentials.
- In Docker, use
MONGO_INITDB_ROOT_USERNAMEandMONGO_INITDB_ROOT_PASSWORDenvironment variables to bootstrap authentication automatically. - For production, enable TLS, restrict network binding, and audit access in addition to authentication.

