MongoDB — Security
Authentication
// Enable authentication
// In mongod.conf:
// security.authorization: enabled
// Create admin user
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: ["root"]
})
Authorization Roles
| Role | Description |
|---|---|
| read | Read data |
| readWrite | Read and write |
| dbAdmin | Database admin |
| userAdmin | User management |
| root | Full access |
Create User
use mydb
db.createUser({
user: "appuser",
pwd: "password",
roles: [{ role: "readWrite", db: "mydb" }]
})
Connect with Auth
mongo -u admin -p password --authenticationDatabase admin
TLS/SSL
# mongod.conf
net:
tls:
mode: requireTLS
certificateKeyFile: /path/to/server.pem
Security Checklist
| Practice | Description |
|---|---|
| Enable auth | Require authentication |
| Use TLS | Encrypt connections |
| Limit network | Bind to specific IP |
| Audit logging | Track operations |
| Regular updates | Patch vulnerabilities |
Mini Practice
- Enable authentication
- Create users
- Assign roles
- Connect with auth
Up Next
Continue with Users — user management.
Related Topics
Frequently Asked Questions about Security
What is Security in MongoDB?
Security is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Security?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Security.
Why is Security important in MongoDB?
Security is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.