Database Administration and Maintenance
Now that you've mastered MongoDB's core operations, data modeling, and GUI tools, it's time to dive into the crucial role of database administration and maintenance. In this lesson, you'll learn how to keep your MongoDB databases healthy, performant, and reliable through routine administrative tasks.
Learning Goals:
- Monitor database health and performance metrics
- Manage database users and roles effectively
- Perform routine maintenance operations
- Understand storage engine considerations
- Handle database cleanup and compaction
Database Health Monitoring
Monitoring your database's health is essential for proactive maintenance. MongoDB provides several tools to check your database status.
Checking Server Status
The db.serverStatus() command provides a comprehensive overview of your MongoDB instance:
// Get comprehensive server status
const status = db.serverStatus();
// Check key metrics
print("MongoDB Version: " + status.version);
print("Uptime: " + status.uptime + " seconds");
print("Connections: " + status.connections.current + "/" + status.connections.available);
print("Memory Usage: " + (status.mem.resident / 1024).toFixed(2) + " MB");
Monitoring Operations
Use db.currentOp() to see currently running operations:
// View all current operations
db.currentOp()
// Filter for long-running operations (more than 3 seconds)
db.currentOp({
"active": true,
"secs_running": { "$gt": 3 }
})
Set up regular monitoring with db.serverStatus() to establish baseline performance metrics. This helps you quickly identify when something is abnormal.
User and Role Management
Proper user management is crucial for database security and access control.
Creating Database Users
// Switch to admin database for user management
use admin
// Create a user with read-write permissions for a specific database
db.createUser({
user: "appUser",
pwd: "securePassword123",
roles: [
{
role: "readWrite",
db: "ecommerce"
},
{
role: "read",
db: "reports"
}
]
})
// Create a backup user with backup role
db.createUser({
user: "backupAgent",
pwd: "backupSecure456",
roles: ["backup"]
})
Managing User Roles
use admin
// List all users
db.getUsers()
// Update user roles
db.updateUser("appUser", {
roles: [
{
role: "readWrite",
db: "ecommerce"
},
{
role: "dbAdmin",
db: "ecommerce"
}
]
})
// Remove a user
db.dropUser("oldUser")
Routine Maintenance Operations
Regular maintenance keeps your database running smoothly and efficiently.
Database Cleanup
// Switch to target database
use ecommerce
// Remove old sessions (older than 30 days)
db.sessions.deleteMany({
lastAccessed: { $lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
})
// Clean up temporary collections
db.getCollectionNames().forEach(function(collection) {
if (collection.startsWith("temp_")) {
db[collection].drop()
}
})
Index Maintenance
use ecommerce
// List all indexes with sizes
db.products.getIndexes().forEach(function(index) {
const stats = db.products.stats();
const indexSize = stats.indexSizes[index.name] || 0;
print("Index: " + index.name + " - Size: " + (indexSize / 1024 / 1024).toFixed(2) + " MB");
})
// Remove unused indexes (be careful!)
// db.products.dropIndex("old_index_name")
Storage Engine Management
Understanding your storage engine helps optimize performance and maintenance.
WiredTiger Statistics
// Get WiredTiger storage engine statistics
const stats = db.serverStatus().wiredTiger;
print("Cache Usage: " + (stats.cache['bytes currently in the cache'] / 1024 / 1024).toFixed(2) + " MB");
print("Cache Max: " + (stats.cache['maximum bytes configured'] / 1024 / 1024).toFixed(2) + " MB");
print("Cache Hit Ratio: " + (stats.cache['pages read into cache'] / stats.cache['pages requested from the cache'] * 100).toFixed(2) + "%");
Compact Collections
For WiredTiger storage engine, use compact to reclaim disk space:
use ecommerce
// Compact a collection to reclaim disk space
db.runCommand({
compact: "orders",
force: true
})
// Check storage reduction
const before = db.orders.storageSize();
const after = db.orders.storageSize();
print("Storage reduced by: " + ((before - after) / 1024 / 1024).toFixed(2) + " MB");
The compact operation requires additional disk space and blocks operations on the collection. Run during maintenance windows and always have backups.
Performance and Health Checks
Regular health checks help identify issues before they become critical.
Comprehensive Health Check
function databaseHealthCheck() {
const result = {
status: "healthy",
issues: []
};
// Check connection count
const connStats = db.serverStatus().connections;
if (connStats.current > (connStats.available * 0.8)) {
result.issues.push("High connection usage: " + connStats.current + "/" + connStats.available);
result.status = "warning";
}
// Check replication lag (if replica set)
try {
const replStatus = db.adminCommand({ replSetGetStatus: 1 });
if (replStatus.members) {
replStatus.members.forEach(member => {
if (member.optimeDate && member.stateStr === "SECONDARY") {
const lag = new Date() - member.optimeDate;
if (lag > 10000) { // 10 seconds
result.issues.push("Replication lag detected: " + (lag / 1000) + " seconds");
result.status = "warning";
}
}
});
}
} catch (e) {
// Not a replica set, ignore
}
// Check for long operations
const longOps = db.currentOp({ "active": true, "secs_running": { "$gt": 60 } });
if (longOps.inprog.length > 0) {
result.issues.push("Long-running operations detected: " + longOps.inprog.length);
result.status = "warning";
}
return result;
}
// Run health check
const health = databaseHealthCheck();
print("Database Status: " + health.status);
health.issues.forEach(issue => print(" - " + issue));
Common Pitfalls
- Ignoring connection limits: MongoDB has default connection limits that can cause application failures if exceeded
- Skipping index maintenance: Unused indexes consume storage and slow down write operations
- Running maintenance during peak hours: Operations like
compactcan impact performance - Forgetting user management: Regularly review and update user permissions as applications evolve
- Missing baseline metrics: Without baseline performance data, it's hard to identify abnormal behavior
Summary
Database administration in MongoDB involves regular monitoring, user management, and maintenance operations. Key responsibilities include monitoring server health with db.serverStatus(), managing users and roles appropriately, performing routine cleanup, maintaining indexes, and understanding storage engine behavior. Regular health checks help proactively identify issues, while proper maintenance scheduling ensures optimal database performance without disrupting applications.
Quiz
Show quiz
-
Which command provides comprehensive information about MongoDB server status and metrics?
- A)
db.currentOp() - B)
db.serverStatus() - C)
db.stats() - D)
db.healthCheck()
- A)
-
What is the primary purpose of the
compactcommand in MongoDB?- A) To optimize query performance
- B) To reclaim disk space by defragmenting collections
- C) To compress data for backup
- D) To rebuild all indexes
-
When should you avoid running maintenance operations like
compact?- A) During development
- B) During peak application usage hours
- C) On weekends
- D) When database is small
-
What does
db.currentOp({"secs_running": {"$gt": 60}})help identify?- A) Fast queries
- B) Database size
- C) Long-running operations
- D) User connections
-
Why is it important to regularly review and update database users?
- A) To change passwords frequently
- B) To ensure proper access control as applications evolve
- C) To improve query performance
- D) To reduce storage usage
Answers:
- B)
db.serverStatus() - B) To reclaim disk space by defragmenting collections
- B) During peak application usage hours
- C) Long-running operations
- B) To ensure proper access control as applications evolve