經常使用的參看 mongodb 狀態的三個命令 db.stat() db.serverStatus() db.currentOp(),下面分別詳細介紹一下html
1 db.stat()mongodb
獲取當前數據庫的信息,好比Obj總數、數據庫總大小、平均Obj大小等數據庫
SECONDARY> db.stats() { "db" : "onroad", #庫名 "collections" : 9, #集合數 "objects" : 130751421, // 記錄在數據庫中的全部文檔總數 "avgObjSize" : 559.257981907516, // 數據庫中全部文檔的平均大小,等於 dataSize/objects "dataSize" : 73123775840, // 數據庫全部文檔的總大小,以字節爲單位 ---> 73G "storageSize" : 80531728032, // 分配給每個文檔的磁盤空間 "numExtents" : 162, ,#事件數 "indexes" : 14, #索引數 "indexSize" : 19496636768, #索引大小 "fileSize" : 332680921088, #文件大小 "nsSizeMB" : 16, #命名空間文件的大小 "dataFileVersion" : { #包含 數據庫文件的磁盤格式信息 的文檔 "major" : 4, #主要版本號的 磁盤格式數據庫的數據文件 "minor" : 5 #次要版本號 }, "extentFreeList" : { "num" : 436, "totalSize" : 229138838864 }, "ok" : 1 }
獲取當前數據庫中集合的信息服務器
> db.gps_point.stats() { "ns" : "onroad.gps_point", #命名空間 "count" : 51101154, #記錄數 "size" : 31394549796, #大小 "avgObjSize" : 614, "storageSize" : 32212257856, "numExtents" : 61, "nindexes" : 2, "lastExtentSize" : 16224256, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 5701353728, "indexSizes" : { "_id_" : 2655262288, #索引爲_id_的索引大小 "user_id_1_data_time_-1" : 3046091440 }, "capped" : true, #爲固定大小集合 "max" : NumberLong("9223372036854775807"), "ok" : 1 }
2 db.serverStatus()app
獲取服務器的狀態ide
詳細說明 參考 http://docs.mongoing.com/manual/reference/command/serverStatus.html 性能
PRIMARY> db.serverStatus() { "host" : "cd9a511a2d0e", #主機名 "version" : "2.6.1", ,#版本號 "process" : "mongod", #進程名 "pid" : NumberLong(41), #進程ID "uptime" : 3785943, #運行時間 "uptimeMillis" : NumberLong("3785942955"), "uptimeEstimate" : 3755878, "localTime" : ISODate("2015-10-16T02:46:27.368Z"), #當前時間 "asserts" : { #各個斷言的數量 "regular" : 0, "warning" : 0, "msg" : 0, "user" : 58, "rollovers" : 0 }, "backgroundFlushing" : { "flushes" : 63098, #刷新次數 "total_ms" : 144997713, #刷新花費總時間 "average_ms" : 2297.97637009097, #平均時間 "last_ms" : 10913, #最後一次時間 "last_finished" : ISODate("2015-10-16T02:45:40.363Z") #最後刷新時間 }, "connections" : { "current" : 1444, #當前鏈接數 "available" : 18556, #可用鏈接數 "totalCreated" : NumberLong(2429949) }, "cursors" : { "note" : "deprecated, use server status metrics", "clientCursors_size" : 1, #客戶端遊標大小 "totalOpen" : 1, #打開遊標數 "pinned" : 0, "totalNoTimeout" : 481, "timedOut" : 84 #超時時間 }, "dur" : { "commits" : 27, "journaledMB" : 2.711552, "writeToDataFilesMB" : 5.213888, "compression" : 0.5158220112430492, "commitsInWriteLock" : 0, "earlyCommits" : 0, "timeMs" : { "dt" : 3003, "prepLogBuffer" : 1, "writeToJournal" : 228, "writeToDataFiles" : 10, "remapPrivateView" : 2 } }, "extra_info" : { "note" : "fields vary by platform", "heap_usage_bytes" : 96529464, #堆使用狀況(字節) "page_faults" : 11253067 #頁面故障數 }, "globalLock" : { "totalTime" : NumberLong("3785942955000"), "lockTime" : NumberLong("47441423960"), "currentQueue" : { "total" : 0, "readers" : 0, "writers" : 0 }, "activeClients" : { "total" : 0, "readers" : 0, "writers" : 0 } }, "indexCounters" : { "accesses" : 5980339643, #索引被訪問數 "hits" : 5980339322, #索引命中數 "misses" : 0, #索引誤差數 "resets" : 0, #復位數 "mi***atio" : 0 #未命中率 }, "locks" : { "." : { "timeLockedMicros" : { "R" : NumberLong(1300731481), "W" : NumberLong("47441423960") }, "timeAcquiringMicros" : { "R" : NumberLong("83350794378"), "W" : NumberLong("8842280365") } }, }, "network" : { "bytesIn" : 182494603618, #輸入數據(byte) "bytesOut" : NumberLong("2936449550300"), #輸出數據(byte) "numRequests" : 600302443 #請求數 }, "opcounters" : { "insert" : 214538892, #插入操做數 "query" : 388689, #查詢操做數 "update" : 3462611, #更新操做數 "delete" : 0, ,#刪除操做數 "getmore" : 188590632, #獲取更多的操做數 "command" : 197825527 #其餘命令操做數 }, "opcountersRepl" : { "insert" : 1, "query" : 0, "update" : 0, "delete" : 0, "getmore" : 0, "command" : 0 }, "recordStats" : { "accessesNotInMemory" : 8377141, "pageFaultExceptionsThrown" : 2926497, "admin" : { "accessesNotInMemory" : 94, "pageFaultExceptionsThrown" : 0 } }, "repl" : { #複製狀況 "setName" : "c562ca6c-1d72-4c6a-a943-b46fe87e47ca", "setVersion" : 105039, "ismaster" : true, "secondary" : false, "hosts" : [ "10.10.1.1:27017", "10.10.1.2:27017" ], "arbiters" : [ "10.10.1.2:27017" ], "primary" : "10.10.1.4:27017", "me" : "10.10.1.5:27017" }, "writeBacksQueued" : false, "mem" : { #內存狀況 "bits" : 64, #64位操做系統 "resident" : 4430, #佔有物理內存數 "virtual" : 813370, #佔有虛擬內存數 "supported" : true, #是否支持內存擴展 "mapped" : 405498, "mappedWithJournal" : 810996 }, "metrics" : { "cursor" : { "timedOut" : NumberLong(84), "open" : { "noTimeout" : NumberLong(481), "pinned" : NumberLong(0), "total" : NumberLong(1) } }, "document" : { "deleted" : NumberLong(0), "inserted" : NumberLong(214538892), "returned" : NumberLong("6735629965"), "updated" : NumberLong(6502807) }, "getLastError" : { "wtime" : { "num" : 0, "totalMillis" : 0 }, "wtimeouts" : NumberLong(0) }, "operation" : { "fastmod" : NumberLong(3483995), "idhack" : NumberLong(39), "scanAndOrder" : NumberLong(6) }, "queryExecutor" : { "scanned" : NumberLong(107218344), "scannedObjects" : NumberLong(107217952) }, "record" : { "moves" : NumberLong(1604) }, "repl" : { "apply" : { "batches" : { "num" : 2, "totalMillis" : 8 }, "ops" : NumberLong(1) }, "buffer" : { "count" : NumberLong(0), "maxSizeBytes" : 268435456, "sizeBytes" : NumberLong(0) }, "network" : { "bytes" : NumberLong(709), "getmores" : { "num" : 18, "totalMillis" : 86108 }, "ops" : NumberLong(2), "readersCreated" : NumberLong(35) }, "preload" : { "docs" : { "num" : 0, "totalMillis" : 0 }, "indexes" : { "num" : 1, "totalMillis" : 0 } } }, "storage" : { "freelist" : { "search" : { "bucketExhausted" : NumberLong(0), "requests" : NumberLong(6551285), "scanned" : NumberLong(12001208) } } }, "ttl" : { "deletedDocuments" : NumberLong(0), "passes" : NumberLong(63048) } }, "ok" : 1 }
須要關心的地方:ui
1 connections當前鏈接和可用鏈接數,聽過一個同行介紹過,mongodb最大處理到2000個鏈接就不行了(要根據你的機器性能和業務來設定),因此設大了沒意義。設個合理值的話,到達這個值mongodb就拒絕新的鏈接請求,避免被太多的鏈接拖垮。spa
2 indexCounters:btree:misses索引的不命中數,和hits的比例高就要考慮索引是否正確創建。操作系統
Mongodb 的命令通常很快就完成,可是在一臺繁忙的機器或者有比較慢的命令時,你能夠經過db.currentOp()獲取當前正在執行的操做。
在沒有負載的機器上,該命令基本上都是返回空的
若是你發現一個操做太長,把數據庫卡死的話,能夠用這個命令殺死他
> db.killOp("shard3:466404288")
這是一個負載比較高的 主從結構的 mongoDB中 主庫 的結果 (insert操做)
> db.currentOp() { "inprog" : [ { "opid" : 411, "active" : false, "op" : "none", "ns" : "", "query" : { }, "desc" : "repl writer worker 1", # 和副本集之間的操做有關 "threadId" : "0x7f2f2203d700", "waitingForLock" : false, "numYields" : 0, "lockStats" : { "timeLockedMicros" : { "r" : NumberLong(0), "w" : NumberLong(8503) }, "timeAcquiringMicros" : { } } }, { "opid" : 1037017522, "active" : true, "secs_running" : 0, "op" : "insert", #操做名 "ns" : "onroad.gps_point", #命名空間 "insert" : { "_id" : ObjectId("56206e122dda660e74674bd1"), #要插入數據的ID "user_id" : 315936739, #要插入的用戶ID "data" : "xxxxxxxx", #要插入的數據 "data_time" : NumberLong(1444965431) }, "client" : "10.10.1.2:46880", "desc" : "conn2377693", "threadId" : "0x7e6b91b13700", "connectionId" : 2377693, "locks" : { "^onroad" : "W" }, "waitingForLock" : true, "numYields" : 0, "lockStats" : { "timeLockedMicros" : { }, "timeAcquiringMicros" : { } } },