Master MongoDB authentication methods including SCRAM, X.509 certificates, LDAP, and Kerberos. Learn user creation, role assignment, and securing MongoDB deployments.
Instrucciones de origen · Vista previa de solo lectura
name
mongodb-authentication
version
2.1.0
description
Master MongoDB authentication methods including SCRAM, X.509 certificates, LDAP, and Kerberos. Learn user creation, role assignment, and securing MongoDB deployments.
{"common_errors":[{"code":"AUTH001","condition":"Authentication failed","recovery":"Verify username, password, authSource database"},{"code":"AUTH002","condition":"User not found","recovery":"Check user exists in correct authenticationDatabase"},{"code":"AUTH003","condition":"Password policy violation","recovery":"Ensure password meets complexity requirements"}]}
prerequisites
{"mongodb_version":"4.0+","required_knowledge":["mongodb-basics","user-management"],"security_requirements":["mongod started with --auth or authorization: enabled"]}
# Start MongoDB with authentication
mongod --auth --dbpath /data/db
# Or in config file (mongod.conf)
security:
authorization: enabled
Create Admin User
// Connect to local server without auth firstconst mongo = newMongoClient('mongodb://localhost:27017')
const admin = mongo.db('admin')
// Create admin userawait admin.command({
createUser: 'admin',
pwd: 'securepassword', // Or use passwordPrompt()roles: ['root']
})
// Now restart mongod --auth
Authentication Methods
SCRAM (Salted Challenge Response)
// Default, password-based authentication// Connection stringmongodb://username:password@localhost:27017/database// With optionsmongodb://username:password@localhost:27017/database?authSource=admin// Create SCRAM user
db.createUser({
user: 'appuser',
pwd: 'password123',
roles: ['readWrite']
})
// Requirements for production:// ✅ Minimum 12 characters// ✅ Mix of uppercase, lowercase, numbers, symbols// ✅ No dictionary words// ✅ Not related to username// Example strong password// P@ssw0rd2024!MongoDB// DON'T USE// password, 123456, monkey, qwerty, password123
Password Rotation
// Change passwords regularly// Monthly for service accounts// Quarterly for normal users// Update password
db.changeUserPassword('username', 'newpassword')
// Check user details
db.getUser('username')
Connection with Authentication
MongoDB Shell
# Connect with authentication
mongosh --username admin --password --authenticationDatabase admin mongodb://localhost:27017
# Or with connection string
mongosh 'mongodb://admin:password@localhost:27017/?authSource=admin'