MongoDB Security
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
| Reason | Benefit |
|---|---|
| Protects your data | Without authentication anyone who can reach the port can read, change, or delete data. |
| Limits damage | A user with readWrite on one database cannot touch the others. |
| Reduces exposure | Binding to localhost or a private interface keeps the server off the public internet. |
| Auditable access | Separate users per service make it clear who did what. |
How To Use It
- Start mongod without authorization and connect with mongosh locally.
- In the admin database, create an administrative user with userAdminAnyDatabase.
- Create an application user on its own database with readWrite only.
- Edit mongod.conf and set security.authorization to enabled.
- Confirm net.bindIp lists only the addresses you need, such as 127.0.0.1 or a private IP.
- Restart mongod and verify that unauthenticated commands are rejected.
- Connect with the new user using a connection string that includes credentials and authSource.
- Open the port only to trusted hosts with a firewall, and use TLS across networks.
mongosh Examples
// 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
| Topic | Detail |
|---|---|
| security.authorization: enabled | mongod.conf setting that requires users to authenticate and enforces roles |
| net.bindIp | Comma-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 / readWrite | Built-in roles for a single database |
| dbAdmin / userAdmin | Administrative roles for a database |
| root | Superuser role; use sparingly |
| userAdminAnyDatabase | Role that lets a user manage users on all databases |
| db.auth() | Authenticate in the shell as a user |
| authSource | Database holding the user's credentials, often admin |
| TLS | Encrypts 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.
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.
8 people found this page helpful
