MongoDB Security

Beginner
9 min read
Updated: Nov 2026
Getting Started

What Is Security?

A self-managed MongoDB server must be secured by you. The basics are: turn on authentication and authorization, create users with only the roles they need (least privilege), and limit where the server listens with net.bindIp. Older default setups ran without authentication, and exposed servers have been a common source of data leaks, so never expose an unauthenticated mongod to a network.

Why It Matters

ReasonBenefit
Protects your dataWithout authentication anyone who can reach the port can read, change, or delete data.
Limits damageA user with readWrite on one database cannot touch the others.
Reduces exposureBinding to localhost or a private interface keeps the server off the public internet.
Auditable accessSeparate users per service make it clear who did what.

How To Use It

  1. Start mongod without authorization and connect with mongosh locally.
  2. In the admin database, create an administrative user with userAdminAnyDatabase.
  3. Create an application user on its own database with readWrite only.
  4. Edit mongod.conf and set security.authorization to enabled.
  5. Confirm net.bindIp lists only the addresses you need, such as 127.0.0.1 or a private IP.
  6. Restart mongod and verify that unauthenticated commands are rejected.
  7. Connect with the new user using a connection string that includes credentials and authSource.
  8. Open the port only to trusted hosts with a firewall, and use TLS across networks.

mongosh Examples

mongosh / mongod.conf
// 1. Create the admin user (run before enabling authorization)
use admin
db.createUser({
  user: "admin",
  pwd: passwordPrompt(),
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})

// 2. Create a least-privilege application user
use shop
db.createUser({
  user: "shopApp",
  pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "shop" }]
})

// 3. mongod.conf (YAML)
// security:
//   authorization: enabled
// net:
//   bindIp: 127.0.0.1

// 4. Connect as the app user
// mongosh "mongodb://shopApp@localhost:27017/shop?authSource=shop" --password

Edge Cases & Pitfalls

Binding to 0.0.0.0

Listening on all interfaces exposes the server to any network that can reach it. Combine with authentication and a firewall, or avoid it.

Localhost exception

With authorization on and no users yet, MongoDB allows a localhost connection to create the first user. Create it, then it closes.

Passwords in shell history

Use passwordPrompt() instead of typing passwords in commands.

Using root for apps

Applications should never run as root or admin. Give them a role scoped to one database.

Authentication without TLS

Credentials and data travel unencrypted on the network. Enable TLS for remote connections.

Cheat Sheet

TopicDetail
security.authorization: enabledmongod.conf setting that requires users to authenticate and enforces roles
net.bindIpComma-separated list of interfaces mongod listens on; default is 127.0.0.1
db.createUser()Create a user with credentials and roles on a database
read / readWriteBuilt-in roles for a single database
dbAdmin / userAdminAdministrative roles for a database
rootSuperuser role; use sparingly
userAdminAnyDatabaseRole that lets a user manage users on all databases
db.auth()Authenticate in the shell as a user
authSourceDatabase holding the user's credentials, often admin
TLSEncrypts traffic between clients and servers

Key Takeaways

Enable security.authorization: enabled, bind only to the interfaces you need, give each application its own user with the minimum role, and use TLS when traffic crosses a network.

Frequently Asked Questions

How do I enable authentication in MongoDB?

Create an admin user, then set security.authorization to enabled in mongod.conf and restart mongod. Clients must then authenticate.

What does bindIp do?

net.bindIp controls which network interfaces mongod accepts connections on. By default it is localhost only (127.0.0.1).

What is least privilege?

Giving each user only the permissions it needs, such as readWrite on one database instead of root.

Which role should my application use?

Typically readWrite on its own database. Use read for reporting users.

Is MongoDB secure by default?

A fresh self-managed install does not require authentication until you enable it, so you must configure security yourself. Atlas enables it for you.

Do I need TLS?

Yes, whenever data crosses a network you do not fully control. TLS encrypts the traffic between clients and servers.

Next: MongoDB Aggregation

Continue the MongoDB learning path.

MongoDB Aggregation →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful