This document summarizes how to administer MongoDB using the command line interface. It discusses installing MongoDB, using basic commands like finding and saving documents, setting up replica sets for high availability and scaling, performing backups and repairs, and preventing slow queries through proper indexing. Sharding is mentioned as an advanced technique for scaling databases with large amounts of data or write operations.
20. Backup / Restore
mongodump --db houstonrb
mongorestore --db houstonrb -c
collections --drop --indexesLast
DirectoryName
* Have all your .bson files in a
directory named your database
21. Repairing
• The Data Gobbler -- mongodb likes hard-
drive space and won’t give it back
• Use mongod --repair to reclaim your
space!
22. Nightly Plan
service mongodb stop && mongod --repair --dbpath /var/
lib/mongodb && service mongodb start
rm -f /data/mongodump-backup.tar.bz2 && mongodump -
o /data/backups && tar -jcf /data/mongodump-
backup.tar.bz2 /data/backups && rm -rf /data/backups/
23. Catch Up!
• When a Replica Set gets ‘stale’
• rs.status will remain “5” (Recovering)
• On Master, will say “Error RS102 too stale
to catch up”
• stop mongo on all servers, copy over all
data files, including local.*, start all
24. Table Scans == Devil
• When you get to 500,000 records, your
DB may slow to crawl.
• /var/log/mongodb/mongodb.log for logged
queries (table scans)
• db.collection.getIndexKeys()
• Mongoid: rake
db:mongoid:create_indexes
25. No, Really
• I’ve been surprised LOTS at how often a
table scan happens.
• Test it out: mongod --notablescan
• Stuff will break.
26. Sharding
• Don’t Shard Early
• Helps in map-reduce
• You define a “shard-key”, like lastname
from A-H on this shard
27. Scaling (Secret Sauce)
• Many Shard Masters, even more slaves
• Add Shards for write and data size scaling
• Add Slaves for read scaling (map-reduce)
and redundancy
• Sharding is Hard