mongo*Just what do those commands do?           By Russell Smith
/usr/bin/whoami•   Russell Smith•   Consultant for UKD1 Limited•   I Specialise in helping companies going through rapid g...
Huh?•   MongoDB ships with a variety of CLI tools•   Some popular;    mongodump, mongorestore, mongostat, mongo•   Some le...
Core
mongo•   Probably the most commonly used•   ‘Hidden’ gems;    •   ~/.mongorc.js - will be executed by default    •   --eva...
mongo (cont.)~ mongo test --eval "printjson(db.getCollectionNames())"MongoDB shell version: 2.1.0-pre-connecting to: test[...
~/.mongorc.jsx = db.queue.count({state:4});if (x > 0){  print(WARNING:  + x +  hung jobs);}
~/.mongorc.js~ mongoMongoDB shell version: 2.1.0-pre-connecting to: testWARNING: 2 hung jobs>
mongod•   The server itself•   Useful:    •   --fastsync - allows (much) faster starting of a replset member by        usi...
mongos•   Sits in front of your shard servers and directs queries•   --keyFile
Common
mongodump•   Used to take backups•   Common options:    •   --oplog    •   -d    •   -c
mongodump (cont.)~ mongodump -d test -c queue -q "{state:4}"connected to: 127.0.0.1DATABASE: test to  dump/test test.queue...
mongorestore•   Restores backups taken with mongodump•   Interesting options;    •   --filter    •   --oplogReplay•   Possi...
mongorestore (cont.)mongorestore -d test -c queue --filter "{state:4}" dump/test/queue.bsonconnected to: 127.0.0.1Mon Sep 1...
mongostat•   Produces periodically updated stats•   Useful in watching server load & spotting general performance issues
Common       Cursor        Totaloperations   paging      data size            queues                       fsync          ...
mongostat (cont’d)•   Watch for;    •   faults (page faults)    •   idx miss (index btree page misses)    •   locked (perc...
Others
mongoexport•   Useful to export data for use with another database or program•   You may specify down to the field level us...
mongoimport•   Imports data in to mongodb•   Currently supports importing;    •   JSON    •   CSV    •   TSV
mongoimport (cont’d)•   Useful options include;    •   --upsert - allows updating objects that exist already        (see -...
bsondump                             bsondump test.bson --type json                             { "_id" :                 ...
bsondump (cont.)                         bsondump test.bson --type debug                         --- new object ---       ...
mongofiles~ mongofiles put test.txtconnected to: 127.0.0.1added file: { _id: ObjectId(4e761b07c21062d06b349819), filename:"tes...
mongofiles (cont.)~ mongofiles listconnected to: 127.0.0.1test.txt 12256
mongotop•   New in 1.9•   Collection level load stats
mongosniff•   Used for sniffing the traffic between client / server•   Can forward traffic•   Only really useful for debuggin...
mongo-perf•   Does not ship with mongodb    See : https://github.com/mongodb/mongo-perf•   I can’t compile it, but it prob...
Useful third party tools
tungsten replicatormysql> create table foo(id int primary key, msg varchar(35));Query OK, 0 rows affected (0.05 sec)mysql>...
automongobackup•   A port of the popular AutoMySQLBackup•   Rotates & compresses backups•   https://github.com/micahwedeme...
mongolog•   Full query logging•   Uses mongosniff•   Will be on github soon
Other interesting projects•   Eliots Munin plugins : https://github.com/erh/mongo-munin•   gridfs-fuse : https://github.co...
Further reading•   Tungsten with Mongo details    http://scale-out-blog.blogspot.com/2011/05/introducing-mysql-to-mongodb....
Questions?
MongoDB Command Line Tools
Upcoming SlideShare
Loading in …5
×

MongoDB Command Line Tools

9,138 views

Published on

An introduction tot

Published in: Technology, Business
  • Be the first to comment

MongoDB Command Line Tools

  1. 1. mongo*Just what do those commands do? By Russell Smith
  2. 2. /usr/bin/whoami• Russell Smith• Consultant for UKD1 Limited• I Specialise in helping companies going through rapid growth;• Code, architecture, infrastructure, devops, sysops, capacity planning, etc• <3 MongoDB, Neo4j, MySQL, Riak, Gearman, Kohana, PHP, Debian, Puppet, etc...
  3. 3. Huh?• MongoDB ships with a variety of CLI tools• Some popular; mongodump, mongorestore, mongostat, mongo• Some less so; mongosniff, mongoexport, mongoimport, bsondump, etc
  4. 4. Core
  5. 5. mongo• Probably the most commonly used• ‘Hidden’ gems; • ~/.mongorc.js - will be executed by default • --eval “some js” - evaluates Javascript and exits
  6. 6. mongo (cont.)~ mongo test --eval "printjson(db.getCollectionNames())"MongoDB shell version: 2.1.0-pre-connecting to: test[ "fs.chunks", "fs.files", "system.indexes", "system.profile", "test" ]~ mongo test --quiet --eval "printjson(db.getCollectionNames())"[ "fs.chunks", "fs.files", "system.indexes", "system.profile", "test" ]
  7. 7. ~/.mongorc.jsx = db.queue.count({state:4});if (x > 0){ print(WARNING: + x + hung jobs);}
  8. 8. ~/.mongorc.js~ mongoMongoDB shell version: 2.1.0-pre-connecting to: testWARNING: 2 hung jobs>
  9. 9. mongod• The server itself• Useful: • --fastsync - allows (much) faster starting of a replset member by using a (recent) snapshot of a peer • --keyFile - replset / sharding authentication file
  10. 10. mongos• Sits in front of your shard servers and directs queries• --keyFile
  11. 11. Common
  12. 12. mongodump• Used to take backups• Common options: • --oplog • -d • -c
  13. 13. mongodump (cont.)~ mongodump -d test -c queue -q "{state:4}"connected to: 127.0.0.1DATABASE: test to dump/test test.queue to dump/test/queue.bson 2 objects
  14. 14. mongorestore• Restores backups taken with mongodump• Interesting options; • --filter • --oplogReplay• Possible performance issues with custom (non ObjectId) _id
  15. 15. mongorestore (cont.)mongorestore -d test -c queue --filter "{state:4}" dump/test/queue.bsonconnected to: 127.0.0.1Mon Sep 19 07:34:03 dump/test/queue.bsonMon Sep 19 07:34:03 going into namespace [test.queue]764 objects found2 objects processed
  16. 16. mongostat• Produces periodically updated stats• Useful in watching server load & spotting general performance issues
  17. 17. Common Cursor Totaloperations paging data size queues fsync active locked flushes clients
  18. 18. mongostat (cont’d)• Watch for; • faults (page faults) • idx miss (index btree page misses) • locked (percentage of time globally locked) • repl (it should normally be M or SEC)
  19. 19. Others
  20. 20. mongoexport• Useful to export data for use with another database or program• You may specify down to the field level using a filter if needed• Can export as; • JSON (default) • CSV
  21. 21. mongoimport• Imports data in to mongodb• Currently supports importing; • JSON • CSV • TSV
  22. 22. mongoimport (cont’d)• Useful options include; • --upsert - allows updating objects that exist already (see --upsertFields) • --headerline - if your CSV / TSV contains headers • -f / --fields - specify field names
  23. 23. bsondump bsondump test.bson --type json { "_id" : ObjectId( "4e72f51414345f302b0eea2e" ),• Convert bson -> json "test" : 1 } 1 objects found
  24. 24. bsondump (cont.) bsondump test.bson --type debug --- new object --- size : 36 _id• Debug info: type: 7 size: 17 test type: 1 size: 14 etc
  25. 25. mongofiles~ mongofiles put test.txtconnected to: 127.0.0.1added file: { _id: ObjectId(4e761b07c21062d06b349819), filename:"test.txt", chunkSize: 262144, uploadDate: new Date(1316363015625),md5: "d41d8cd98f00b204e9800998ecf8427e", length: 12256 }done!
  26. 26. mongofiles (cont.)~ mongofiles listconnected to: 127.0.0.1test.txt 12256
  27. 27. mongotop• New in 1.9• Collection level load stats
  28. 28. mongosniff• Used for sniffing the traffic between client / server• Can forward traffic• Only really useful for debugging...
  29. 29. mongo-perf• Does not ship with mongodb See : https://github.com/mongodb/mongo-perf• I can’t compile it, but it probably works
  30. 30. Useful third party tools
  31. 31. tungsten replicatormysql> create table foo(id int primary key, msg varchar(35));Query OK, 0 rows affected (0.05 sec)mysql> insert into foo values(1, hello from MySQL!);Query OK, 1 row affected (0.00 sec)> db.foo.find(){ "_id" : ObjectId("4dc77bacad9092bd1aef046d"), "id" : "1", "data" :"hello from MySQL!" }• http://code.google.com/p/tungsten-replicator/• http://code.google.com/p/tungsten-replicator/wiki/
  32. 32. automongobackup• A port of the popular AutoMySQLBackup• Rotates & compresses backups• https://github.com/micahwedemeyer/automongobackup
  33. 33. mongolog• Full query logging• Uses mongosniff• Will be on github soon
  34. 34. Other interesting projects• Eliots Munin plugins : https://github.com/erh/mongo-munin• gridfs-fuse : https://github.com/mikejs/gridfs-fuse
  35. 35. Further reading• Tungsten with Mongo details http://scale-out-blog.blogspot.com/2011/05/introducing-mysql-to-mongodb.html• Javascript shell API http://api.mongodb.org/js/current/• Admin UI’s http://www.mongodb.org/display/DOCS/Admin+UIs• Hacking the shell http://bit.ly/r2vghH
  36. 36. Questions?

×