Daily MongoDB Administration Tasks & Solutions
https://learn.mongodb.com/learning-paths/mongodb-database-admin-self-managed-path
https://learn.mongodb.com/learning-paths/mongodb-database-admin-self-managed-path
// Create database
use myDatabase
// List databases
show dbs
// Drop database
db.dropDatabase()
// Create collection
db.createCollection("users")
// List collections
show collections
// Create user
db.createUser({
user: "admin",
pwd: "password",
roles: [
{ role: "userAdmin", db: "admin" },
{ role: "dbAdmin", db: "myDatabase" }
]
})
// List users
db.getUsers()
// Update user
db.updateUser("admin", {
roles: [
{ role: "readWrite", db: "myDatabase" }
]
})
Server status
Resource usage
Connection monitoring
Operation statistics
Performance metrics
// Server status
db.serverStatus()
// Current operations
db.currentOp()
// Database stats
db.stats()
LVM snapshots
Cloud provider snapshots
Storage array snapshots
Point-in-time recovery
Consistent backups
fsync command will force the MongoDB server to flush all pending writes to disk
use admin
switched to db admin
> db.runCommand({"fsync" : 1, "lock" : 1});
After performing the backup, we need to unlock the database again:
> db.$cmd.sys.unlock.findOne(); { "ok" : 1, "info" : "unlock requested" } > db.currentOp(); { "inprog" : [ ] }
To repair a single database on a running server, you can use the repairDatabasemethod from the shell. If we wanted to repair the database test, we would do the following:
> use test
switched to db test
> db.repairDatabase()
{ "ok" : 1 }
Index maintenance
Data compaction
Log rotation
Cache management
Storage cleanup
// Compact collection
db.runCommand({ compact: "users" })
// Repair database
db.repairDatabase()
// Clean up unused indexes
db.collection.dropIndexes()
Data recovery planning
Disaster recovery
Point-in-time recovery
Recovery testing
Documentation
MongoDB Dump (mongodump)
Filesystem snapshots
Continuous backup
Point-in-time recovery
Backup verification
# Create backup
mongodump --db myDatabase --out /backup
# Specific collection backup
mongodump --db myDatabase --collection users --out /backup
# Compressed backup
mongodump --db myDatabase --gzip --out /backup
# Backup with authentication
mongodump --uri="mongodb://user:password@localhost:27017/myDatabase" --out /backup
*****************************************
# Restore entire database
mongorestore --db myDatabase /backup/myDatabase
# Restore specific collection
mongorestore --db myDatabase --collection users /backup/myDatabase/users.bson
# Restore compressed backup
mongorestore --gzip --db myDatabase /backup/myDatabase
# Restore with authentication
mongorestore --uri="mongodb://user:password@localhost:27017/myDatabase" /backup/myDatabase
Oplog-based recovery
Timestamp-based recovery
Consistent state recovery
Partial recovery
Cross-cluster recovery