Skip to content

Instantly share code, notes, and snippets.

View normgraham's full-sized avatar

Norman Graham normgraham

View GitHub Profile
@normgraham
normgraham / dbProfilerQueries.js
Created January 30, 2014 19:07
Useful aggregations and queries over the system.profile collection.
// response time by operation type
db.system.profile.aggregate( {
$group : {
_id :"$op",
count:{$sum:1},
"max response time":{$max:"$millis"},
"avg response time":{$avg:"$millis"}
}
});
@normgraham
normgraham / growSystemProfile.js
Created January 30, 2014 19:05
Increase system.profile from 1MB of queries to 100MB of queries and capture all operations.
use <dbname>
db.setProfilingLevel(0)
db.system.profile.drop()
db.runCommand( { create : "system.profile", capped : true, size : 104857600 } )
db.setProfilingLevel(2)
@normgraham
normgraham / allChunkInfo.js
Created January 30, 2014 19:02
Display detail and aggregate chunk info for a sharded collection. To run, load the following code for allChunkInfo() into a mongos process in your cluster and run allChunkInfo( "dbname.collname" ).
var allChunkInfo = function(ns){
var chunks = db.getSiblingDB("config").chunks.find({"ns" : ns}).sort({min:1}); //this will return all chunks for the ns ordered by min
//some counters for overall stats at the end
var totalChunks = 0;
var totalSize = 0;
var totalEmpty = 0;
print("ChunkID,Shard,ChunkSize,ObjectsInChunk"); // header row
// iterate over all the chunks, print out info for each
chunks.forEach(
function printChunkInfo(chunk) {
@normgraham
normgraham / dumpConfigDB.sh
Last active August 29, 2015 13:55
You can create a dump of the config database by running the following command on one of your config servers or by specifying parameters for one of your config servers. When you're done, the dump information will be in the file /tmp/mongodump.tgz
mkdir -p /tmp/mongodump
mongodump --host $HOST --port $PORT --username $USERNAME --password $PASSWORD --authenticationDatabase $DBNAME --db config --out /tmp/mongodump/ > /tmp/mongodump/mongodump.log
tar -cz -C /tmp -f /tmp/mongodump.tgz mongodump
@normgraham
normgraham / chkConfigSvr.js
Created January 30, 2014 18:53
In the case of unsynchronized config servers, determine which one has the most up-to-date data. Run on each config server and compare the results.
// Connect a Mongo shell to each of your config servers, and run the following:
use config
db.runCommand({dbhash:1})
db.changelog.find().sort({ $natural: 1 }).limit(10)
db.chunks.find().sort({ $natural: 1 }).limit(10)