Mongo admin'ing

2,915 views

Published on

Tips and suggestions for administering mongodb from a ruby/rails perspective (1.6 and 1.8)

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,915
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Mongo admin'ing

    1. 1. Mongo Admin’ing Jesse Wolgamotttwitter: @jwo slides: http://github.com/jwo
    2. 2. Quick Install• Dev: brew install mongodb• Server: apt-get install mongodb • http://www.mongodb.org/display/ DOCS/Ubuntu+and+Debian+packages
    3. 3. Don’t Fear theCommand Line Mongoid / Mongo Mapper Ruby DriverCommand Line Javascript (mongo)
    4. 4. Quick Server Statusdbdb.system.namespaces.find()db.serverStatus()db.currentOp()
    5. 5. Command Line Basicsuse dbadb.collection.find({json params})db.collection.findOne({json params})db.collection.mapReduce(map,reduce)db.collection.save({json})
    6. 6. Why Bother?• Mongo queries enhance Mongoid queries• Drop to command line when needed• Game.where(:outcome=>{‘$in’=>[‘winning’ ,‘tie’]})• Game.collection.update({:outcome=>‘win ning’, ‘$set’=>{:winning=>true}})
    7. 7. All Admin is Command Line• Setup Replica Sets• Check on Status
    8. 8. Replica / Slave / Journaling“If you aren’t running a replica set, you don’t care about your data” -- Mongo DB dude
    9. 9. Master / Slave What You’d Expect M Slave Slave Slave
    10. 10. Master / Slave What You’d Expect M Writes Go Here Slave Slave Slave
    11. 11. Master / Slave What You’d Expect M Writes Go Here Slave Slave Slave Reads CAN Go Here
    12. 12. Replica Sets The new Hotness (and Recommended)Primary Secondary Secondary Secondary Arbiter
    13. 13. Replica Sets The new Hotness (and Recommended) Primary Secondary Secondary SecondaryWrites Go Here Arbiter
    14. 14. Replica Sets The new Hotness (and Recommended) Primary Secondary Secondary SecondaryWrites Go Here CAN Read Arbiter
    15. 15. Replica Sets The new Hotness (and Recommended) Primary Secondary Secondary SecondaryWrites Go Here CAN Read Arbiter Nothing Goes Here
    16. 16. Arbiter?• Arbiters exist when even number of hosts• They cast votes on primary elections• Can exist on your app server
    17. 17. Journaling• Added in 1.8• mongod -- journal• Will write out changes to a journal directory• You can restore from journal
    18. 18. Replica Set Funconfig = { _id: houstonrb, members: [ {_id: 0, host: localhost:20000}, {_id: 1, host: localhost:20001}, {_id: 2, host: localhost:20002, arbiterOnly: true}]}// Start it uprs.initiate(config);// Check it Outrs.status()// Who’s your daddy?rs.isMaster()
    19. 19. Non-Master Readserror: { "$err" : "not master", "code" : 10107 } OK, so we’ll do: db.getMongo().setSlaveOk() // Awesome
    20. 20. Backup / Restoremongodump --db houstonrbmongorestore --db houstonrb -ccollections --drop --indexesLastDirectoryName* Have all your .bson files in adirectory named your database
    21. 21. Repairing• The Data Gobbler -- mongodb likes hard- drive space and won’t give it back• Use mongod --repair to reclaim your space!
    22. 22. Nightly Planservice mongodb stop && mongod --repair --dbpath /var/lib/mongodb && service mongodb startrm -f /data/mongodump-backup.tar.bz2 && mongodump -o /data/backups && tar -jcf /data/mongodump-backup.tar.bz2 /data/backups && rm -rf /data/backups/
    23. 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. 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. 25. No, Really• I’ve been surprised LOTS at how often a table scan happens.• Test it out: mongod --notablescan• Stuff will break.
    26. 26. Sharding• Don’t Shard Early• Helps in map-reduce• You define a “shard-key”, like lastname from A-H on this shard
    27. 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
    28. 28. Neat Snippets Replication Setup Sharding Setuphttps://github.com/mongodb/mongo-snippets
    29. 29. EC2 Notes• Reformat drive to EXT4 or XFS• Don’t use EBS for long term backups• Can use EBS for setting up replica sets

    ×