</>
Skip to content
MongoDB lessons (31/38)

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

RoleDescription
readRead data
readWriteRead and write
dbAdminDatabase admin
userAdminUser management
rootFull 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

PracticeDescription
Enable authRequire authentication
Use TLSEncrypt connections
Limit networkBind to specific IP
Audit loggingTrack operations
Regular updatesPatch vulnerabilities

Mini Practice

  1. Enable authentication
  2. Create users
  3. Assign roles
  4. 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.