MongoDB
default user
default password
database security
user authentication

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.

StateWho can connectWhat they can do
Fresh install (default)Anyone who can reach the network portFull read/write to all databases
After enabling authOnly users with valid credentialsOnly 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:

bash
1# Using mongosh (MongoDB Shell, version 6.0+)
2mongosh
3
4# Or using the legacy mongo shell
5mongo

Step 2: create the admin user

Switch to the admin database and create a user with administrative privileges:

javascript
1use admin
2
3db.createUser({
4  user: "adminUser",
5  pwd: passwordPrompt(),   // prompts for password interactively
6  roles: [
7    { role: "userAdminAnyDatabase", db: "admin" },
8    { role: "readWriteAnyDatabase", db: "admin" }
9  ]
10})

passwordPrompt() keeps the password out of your shell history. To script user creation, pass the password directly:

javascript
1db.createUser({
2  user: "adminUser",
3  pwd: "your-secure-password-here",
4  roles: [
5    { role: "userAdminAnyDatabase", db: "admin" },
6    { role: "readWriteAnyDatabase", db: "admin" }
7  ]
8})

Step 3: enable authentication

Edit the configuration file (typically /etc/mongod.conf on Linux or mongod.cfg on Windows):

yaml
security:
  authorization: enabled

Then restart MongoDB:

bash
1# Linux (systemd)
2sudo systemctl restart mongod
3
4# macOS (Homebrew)
5brew services restart mongodb-community
6
7# Windows (services)
8net stop MongoDB && net start MongoDB

After the restart, unauthenticated connections are rejected.

Connecting with credentials

From the shell

bash
mongosh -u adminUser -p --authenticationDatabase admin

-p without a value prompts for the password. Passing it inline exposes it in process listings and shell history:

bash
mongosh -u adminUser -p 'your-secure-password-here' --authenticationDatabase admin

With a connection string

text
mongodb://adminUser:your-secure-password-here@localhost:27017/?authSource=admin

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

python
1# Python (pymongo)
2from pymongo import MongoClient
3
4client = MongoClient(
5    "mongodb://adminUser:your-secure-password-here@localhost:27017/?authSource=admin"
6)
7db = client["myapp"]
8print(db.list_collection_names())
javascript
1// Node.js (mongodb driver)
2const { MongoClient } = require('mongodb');
3
4const uri = "mongodb://adminUser:your-secure-password-here@localhost:27017/?authSource=admin";
5const client = new MongoClient(uri);
6
7async function run() {
8    await client.connect();
9    const db = client.db("myapp");
10    const collections = await db.listCollections().toArray();
11    console.log(collections);
12    await client.close();
13}
14
15run();

Creating application users with scoped roles

Applications should not use the admin user. Create users with the minimum privileges they need:

javascript
1use admin
2
3// Read-write access to a specific database
4db.createUser({
5  user: "appUser",
6  pwd: passwordPrompt(),
7  roles: [
8    { role: "readWrite", db: "myapp" }
9  ]
10})
11
12// Read-only access for reporting
13db.createUser({
14  user: "reportUser",
15  pwd: passwordPrompt(),
16  roles: [
17    { role: "read", db: "myapp" }
18  ]
19})
RoleScopePermissions
readSingle databaseRead-only access
readWriteSingle databaseRead and write access
dbAdminSingle databaseSchema management, indexing, stats
userAdminSingle databaseCreate and manage users
readWriteAnyDatabaseAll databasesRead/write across all databases
userAdminAnyDatabaseAll databasesManage users across all databases
rootAllSuperuser (use sparingly)

Setting credentials in Docker

The official MongoDB image accepts initial credentials as environment variables:

bash
1docker run -d \
2  --name mongodb \
3  -p 27017:27017 \
4  -e MONGO_INITDB_ROOT_USERNAME=adminUser \
5  -e MONGO_INITDB_ROOT_PASSWORD=securePassword \
6  -v mongodb_data:/data/db \
7  mongo:7

This creates the root user and enables authentication automatically, with no manual user creation step.

In Docker Compose:

yaml
1services:
2  mongodb:
3    image: mongo:7
4    ports:
5      - "27017:27017"
6    environment:
7      MONGO_INITDB_ROOT_USERNAME: adminUser
8      MONGO_INITDB_ROOT_PASSWORD: securePassword
9    volumes:
10      - mongodb_data:/data/db
11
12volumes:
13  mongodb_data:

JavaScript files mounted into /docker-entrypoint-initdb.d/ run on first start, which is a convenient place to create application users and databases:

javascript
1// init-users.js (mounted to /docker-entrypoint-initdb.d/)
2db = db.getSiblingDB('myapp');
3
4db.createUser({
5  user: 'appUser',
6  pwd: 'app-password',
7  roles: [{ role: 'readWrite', db: 'myapp' }]
8});
9
10db.createCollection('users');

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.

bash
# Connect to Atlas
mongosh "mongodb+srv://cluster0.abc123.mongodb.net/myapp" --username atlasUser

Atlas users are managed separately from the database's built-in user system, and authentication is always on.

Production security checklist

  1. Authentication is enabled in mongod.conf (security.authorization: enabled)
  2. Admin user exists with a strong password
  3. Application users are scoped to specific databases with minimal roles
  4. Network binding is restricted (bindIp set to specific interfaces, not 0.0.0.0)
  5. TLS/SSL is enabled for encrypted connections
  6. Firewall rules restrict access to port 27017
  7. Audit logging is enabled for compliance requirements
yaml
1# Production mongod.conf example
2security:
3  authorization: enabled
4
5net:
6  bindIp: 127.0.0.1,10.0.1.5
7  tls:
8    mode: requireTLS
9    certificateKeyFile: /etc/ssl/mongodb.pem
10    CAFile: /etc/ssl/ca.pem

Mistakes that cause lockouts and breaches

  • Trying admin/admin or root/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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design