1.db.server.Status()
>>echo "db.serverStatus()" |mongo 127.0.0.1:27017 --quiet
{
"host" : "tong", --server的hostname
"version" : "4.4.0", --mongo版本
"process" : "mongod", --進程名
"uptime" : 8886, --啟動時間(單位:S)
"uptimeEstimate" : 8885527, --基于MongoDB內(nèi)部粗粒度定時器的運行時間
"localTime" : ISODate("2020-09-23T05:42:47.200Z"), --server的本地時間
"globalLock" : {
"totalTime" : 1238418105923, --全局鎖創(chuàng)建的時間(單位:ms 微秒)
"lockTime" : 75055831911, --全局鎖保持的時間(單位:ms 微秒)
"ratio" : 0.06060621332329477, --lockTime和totalTime的比
"currentQueue" : {
"total" : 0, --等待全局鎖的隊列中操作數(shù)目
"readers" : 0, --等待讀鎖的隊列中操作數(shù)目
"writers" : 0 --等待寫鎖的隊列中操作數(shù)目
},
"activeClients" : {
"total" : 1, --連接到server的當前活動client數(shù)目
"readers" : 1, --執(zhí)行讀操作的當前活動client數(shù)目
"writers" : 0 --執(zhí)行寫操作的當前活動client數(shù)目
}
},
"mem" : {
"bits" : 64, --64位機器
"resident" : 18363, --占用物理內(nèi)存量肴焊。
"virtual" : 478810, --占用的虛擬內(nèi)存量
"supported" : true, --是否支持擴展內(nèi)存
"mapped" : 233311, --映射到內(nèi)存的數(shù)據(jù)文件大小,很接近于你的所有數(shù)據(jù)庫大小(mmap引擎才有)功戚。
"mappedWithJournal" : 466622,
"note" : "virtual minus mapped is large. could indicate a memory leak"
},
"connections" : {
"current" : 737, --當前活動連接量娶眷。連接到server的當前活躍連接數(shù)目
"available" : 82 --剩余空閑連接量。剩余的可用連接數(shù)目
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 3838448, --此過程中所有的堆字節(jié)數(shù)目啸臀。僅適用于Linux
"page_faults" : 31058356 --此過程中訪問內(nèi)存中頁面失敗的總次數(shù)届宠。僅適用于Linux
},
"indexCounters" : {
"btree" : {
"accesses" : 68229146, --Btree索引的訪問次數(shù)(索引被訪問量)
"hits" : 68229146, --內(nèi)存中的Btree頁的數(shù)目。(索引命中量)
"misses" : 0, --內(nèi)存中不存在的Btree也數(shù)目乘粒。(索引偏差量)(索引內(nèi)存訪問失敗次數(shù))
"resets" : 0, --索引計數(shù)器被重置為0的次數(shù)
"missRatio" : 0 --索引偏差率(未命中率)
}
},
"backgroundFlushing" : {
"flushes" : 20640, --數(shù)據(jù)庫刷新寫到磁盤的次數(shù)
"total_ms" : 2453287, --數(shù)據(jù)庫刷新數(shù)據(jù)到磁盤花費的微秒數(shù)
"average_ms" : 118.8608042635659, --執(zhí)行單次刷新花費的平均微秒數(shù)
"last_ms" : 1, --最后一次執(zhí)行完成刷新數(shù)據(jù)到磁盤花費的微秒數(shù)
"last_finished" : ISODate("2012-09-14T09:09:35.656Z") --當最后一次刷新數(shù)據(jù)完成時的時間戳
},
"cursors" : {
"totalOpen" : 0, --server為client保持的游標(cursor)總數(shù)
"clientCursors_size" : 0, --
"timedOut" : 24 --server啟動以來游標(cursor)超時的總數(shù)
},
"network" : {
"bytesIn" : NumberLong("1929833164782"), --發(fā)送到數(shù)據(jù)庫的數(shù)據(jù)總量(bytes)
"bytesOut" : 553137147925, --數(shù)據(jù)庫發(fā)出的數(shù)據(jù)總量(bytes)
"numRequests" : 2475184328 --發(fā)送到數(shù)據(jù)庫的請求量
},
"opcounters" : {
"insert" : 687531883, --server啟動以來總的insert數(shù)據(jù)量
"query" : 711010343, --server啟動以來總的query數(shù)據(jù)量
"update" : 0, --server啟動以來總的update數(shù)據(jù)量
"delete" : 0, --server啟動以來總的delete數(shù)據(jù)量
"getmore" : 6484, --server啟動以來調(diào)用任何游標的getMore總次數(shù)
"command" : 1287537 --server啟動以來執(zhí)行其他命令的總次數(shù)
},
"asserts" : {
"regular" : 0, --server啟動以來拋出正規(guī)斷言(assert 類似于異常處理的形式)總數(shù)目
"warning" : 1, --server啟動以來拋出的告警總數(shù)目
"msg" : 0, --消息斷言數(shù)目豌注。服務器內(nèi)部定義的良好字符串錯誤
"user" : 4, --用戶斷言數(shù)目。用戶產(chǎn)生的錯誤灯萍,譬如:磁盤空間滿轧铁;重復鍵。
"rollovers" : 0 --server啟動以來旦棉,assert counters have rolled over的次數(shù)
},
"writeBacksQueued" : false, --是否有從mongos執(zhí)行的retry操作
"dur" : {
"commits" : 30, --上一間隔journal日志發(fā)生commit的次數(shù)
"journaledMB" : 0, --上一間隔寫到journal日志的數(shù)據(jù)量(單位:MB)
"writeToDataFilesMB" : 0, --上一間隔journal日志寫到數(shù)據(jù)文件的數(shù)據(jù)量(單位:MB)
"compression" : 0, --
"commitsInWriteLock" : 0, --寫鎖期間發(fā)生commits的次數(shù)
"earlyCommits" : 0, --schedule時間前請求commit的次數(shù)
"timeMs" : {
"dt" : 3064,
"prepLogBuffer" : 0, --準備寫journal日志花費的時間
"writeToJournal" : 0, --寫journal日志花費的實際時間
"writeToDataFiles" : 0, --journal日志后寫數(shù)據(jù)文件花費的時間
"remapPrivateView" : 0 --The amount of time spent remapping copy-on-write memory mapped views
}
},
"ok" : 1 --serverStatus是否返回正確
"$clusterTime" : {
"clusterTime" : Timestamp(1600839763, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1600839763, 1)
}
2.關于mongodb連接數(shù)與內(nèi)存
tongRepl:SECONDARY> db.serverStatus().connections
{
"current" : 13,
"available" : 2035,
"totalCreated" : 714,
"active" : 3,
"exhaustIsMaster" : 2,
"awaitingTopologyChanges" : 79
}
1.current:表示當前有多少客戶端連接到了mongo服務端
2. available:可用連接數(shù)
3. totalCreated:總創(chuàng)建的連接數(shù)
4. active:當前活躍的連接數(shù)
5. exhaustIsMaster:已耗盡的isMaster請求的連接數(shù)齿风。
6. awaitingTopologyChanges:當前在isMaster請求中等待拓撲改變的客戶端數(shù)量。
3.linux缺省stack
1.stack:個人理解绑洛,就是程序使用遞歸操作時候救斑,將數(shù)據(jù)放入堆棧中,一旦超出設定值就會觸發(fā)Stack Overflow錯誤真屯。
2.open file句柄值需要修改
有時候Linux系統(tǒng)默認的open files(文件句柄)是1024脸候, 但是mongod官網(wǎng)建議是64000
查看堆棧設置大小(linux一般默認8MB)
[wantuser@SHQWANTAITEST02 ~]$ ulimit -a | grep stack
stack size (kbytes, -s) 8192
4.mongo查看用戶進限制(kb)
mac查看stack大小
[wantuser@SHQWANTAITEST02 ~]$ cat /proc/$(pidof mongod)/limits | grep stack | awk -F 'size' '{print int($NF)/1024}'
8192
臨時調(diào)整stack大小的方法
[root@f1-mongo1 journal]# ulimit -s 1024
查看stack讨跟、open file跟其他參數(shù)
tong@tongdembp goproj % ulimit -a
-t: cpu time (seconds) unlimited
-f: file size (blocks) unlimited
-d: data seg size (kbytes) unlimited
-s: stack size (kbytes) 8192
-c: core file size (blocks) 0
-v: address space (kbytes) unlimited
-l: locked-in-memory size (kbytes) unlimited
-u: processes 1392
-n: file descriptors 2560
永久調(diào)整Mongodb用戶文件句柄數(shù)纪他。
>>vi /etc/security/limits.conf
mongo soft nproc 64000
mongo hard nproc 64000
mongo soft nofile 64000
mongo hard nofile 64000
5.top查看
[wantuser@SHQWANTAITEST02 ~]$ top
top - 09:55:36 up 13 days, 14:24, 2 users, load average: 0.00, 0.02, 0.05
Tasks: 219 total, 1 running, 218 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16433084 total, 13704208 free, 1183664 used, 1545212 buff/cache
KiB Swap: 8257532 total, 8257532 free, 0 used. 14966964 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4329 wantuser 20 0 1095764 130168 9460 S 0.7 0.8 107:22.09 mongod
mac 命令行查看內(nèi)存
echo -e "\n$(top -l 1 | awk '/PhysMem/';)\n"
Mongodb 的WiredTiger CacheSize
Mongodb的wiredtiger cacheSizeGB 指的就是Cache size,包括數(shù)據(jù)和索引晾匠。Mongod本身使用內(nèi)存如連接池堆棧以及sorting buffer等都是額外的茶袒,不會被統(tǒng)計到這個數(shù)字里面。
如果索引在內(nèi)存凉馆,查詢冷數(shù)據(jù)取決于你的IO能力薪寓。如果IO latency很低亡资,系統(tǒng)也沒有是高負載,那響應時間應該是毫秒級的區(qū)別向叉。但是如果查詢很頻繁锥腻,又涉及到很多范圍、批量查詢母谎,IOPS又跟不上瘦黑,那就有可能到幾百幾千毫秒都有可能。
6.linux的buffer/cache
buffer
即寫如磁盤時奇唤,先保存到磁盤緩沖區(qū)(buffer)幸斥,然后再寫入到磁盤。
cache
即讀磁盤時咬扇,數(shù)據(jù)從磁盤讀出后甲葬,暫留在緩沖區(qū)(cache),為后續(xù)程序的使用做準備懈贺。
- 操作系統(tǒng)的角度:這塊內(nèi)存確實被操作系統(tǒng)使用了经窖。
- 用戶角度:如果用戶要使用,這塊內(nèi)存是可以很快被回收而被用戶空間程序使用梭灿,因此從用戶角度而言這塊內(nèi)存應被劃為空閑狀態(tài)画侣。
- 目的就是為了提升磁盤IO的性能,從低速的塊設備上讀取的數(shù)據(jù)會暫時保存在內(nèi)存中胎源,即使數(shù)據(jù)在當時已經(jīng)不再需要了棉钧,但在應用程序下一次訪問該數(shù)據(jù)時屿脐,它可以從內(nèi)存中直接讀取涕蚤,繞開了低速的塊設備,從而提高系統(tǒng)的整體性能的诵。
為了提高系統(tǒng)性能和不浪費內(nèi)存万栅,linux把多的內(nèi)存做了cache,以提高io速度.你的那些內(nèi)存并沒有被占用西疤。
7.TCP連接和請求處理
mongodb 驅(qū)動與mongod進行tcp連接時烦粒,會發(fā)送數(shù)據(jù)庫請求,數(shù)據(jù)庫會接受應答代赁。tcp協(xié)議棧為連接維護socket元數(shù)據(jù)扰她,每個連接會有一個read buffer及write buffer,這里的rb和tb就表示相應的buffer
對于每個連接,Mongod 會起一個單獨的線程芭碍,專門負責處理這條連接上的請求徒役,mongod 為處理連接請求的線程配置了最大1MB的線程棧,通常實際使用在幾十KB左右窖壕,通過 proc 文件系統(tǒng)看到這些線程棧的實際開銷忧勿。 除了處理請求的線程杉女,mongod 還有一系列的后臺線程,比如主從同步鸳吸、刷新 Journal熏挎、TTL、evict 頁面淘汰等線程晌砾,默認每個線程最大ulimit -s(一般8MB)的線程棧坎拐,由于這批線程數(shù)量比較固定,占的內(nèi)存也比較可控养匈。
8.tcmalloc(New in version 4.2.3: Also available in 3.6.17+ and 4.0.14+)
并發(fā)高導致內(nèi)存暴增廉白,主要也是因為tcmalloc的機制
https://gperftools.github.io/gperftools/tcmalloc.html#runtime
查看mongodb的tcmalloc內(nèi)存
db.serverStatus().tcmalloc
total_free_bytes表示是tcmalloc的空閑,并沒返回給系統(tǒng)乖寒。
其他的都是tcmalloc在使用的的內(nèi)存猴蹂。
tcmalloc 為性能考慮,每個線程會有自己的 local free page cache楣嘁,還有 central free page cache磅轻;內(nèi)存申請時,按 local thread free page cache ==> central free page cache 查找可用內(nèi)存逐虚,找不到可用內(nèi)存時才會從堆上申請聋溜;當釋放內(nèi)存時,也會歸還到 cache 里叭爱,tcmalloc 后臺慢慢再歸還給 OS撮躁,
默認情況下,tcmalloc 最多會 cache min(1GB买雾,1/8 * system_memory) 的內(nèi)存把曼, 通過 setParameter.tcmallocMaxTotalThreadCacheBytesParameter 參數(shù)可以配置這個值
"formattedString" : "------------------------------------------------
MALLOC: 11885113240 (11334.5 MiB) Bytes in use by application
MALLOC: + 2490413056 ( 2375.0 MiB) Bytes in page heap freelist
MALLOC: + 773440096 ( 737.6 MiB) Bytes in central cache freelist
MALLOC: + 489728 ( 0.5 MiB) Bytes in transfer cache freelist
MALLOC: + 590923016 ( 563.5 MiB) Bytes in thread cache freelists
MALLOC: + 100851968 ( 96.2 MiB) Bytes in malloc metadata
MALLOC: ------------
MALLOC: = 15841231104 (15107.4 MiB) Actual memory used (physical + swap)
MALLOC: + 1233395712 ( 1176.3 MiB) Bytes released to OS (aka unmapped)
MALLOC: ------------
MALLOC: = 17074626816 (16283.6 MiB) Virtual address space used
MALLOC:
MALLOC: 1088636 Spans in use
MALLOC: 96 Thread heaps in use
MALLOC: 4096 Tcmalloc page size
------------------------------------------------
Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).
Bytes released to the OS take up virtual address space but no physical memory.
eviction四個重要的參數(shù)
降低eviction_target、eviction_dirty_target漓穿,讓evict線程提前將數(shù)據(jù)從wiredtiger cache刷到操作系統(tǒng)的page cache嗤军,可以提早刷盤。
eviction_target 80(實際就是cachesize的百分比)當cache_used超過80%時候晃危,觸發(fā)觸發(fā)eviction叙赚,后臺evict線程開始淘汰clean page。類似LRU算法僚饭,淘汰冷數(shù)據(jù)
eviction_trigger 95 cache used超過eviction_trigger震叮,用戶線程也開始淘汰CLEAN PAGE。
eviction_dirty_target 5 cache dirty超過eviction_dirty_target鳍鸵,evict線程開始淘汰DIRTY PAGE苇瓣。
eviction_dirty_trigger 20 cache dirty超過eviction_dirty_trigger, 用戶線程也開始淘汰DIRTY PAGE。
關于cachesize
默認情況下权纤,cache_used超過80%將觸發(fā)eviction,類似mysql的LRU算法冷數(shù)據(jù)的淘汰, 避免cache用量持續(xù)增長坯钦。 如果物理內(nèi)存充足,設置足夠大的cache_size撤蚊,以加載全部數(shù)據(jù),避免不必要的eviction损话。
cache_size:
cache_size = (data + index) / 0.8
eviction:
查看wiredTiger.cache一些參數(shù)侦啸。
db.serverStatus().wiredTiger.cache
"eviction worker thread active" 默認為 4,
對于eviction線程,MongoDB默認配置是eviction=(threads_min=1,threads_max=4)丧枪,根據(jù)cache使用情況光涂,創(chuàng)建1-4個eviction線程去淘汰冷數(shù)據(jù)。
如果cache體積較大拧烦,或者機器性能很高忘闻。可以適當調(diào)高恋博。
Example:
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "eviction=(threads_min=1,threads_max=8)"})
wiredTigerConcurrentWriteTransactions
并發(fā)寫事務數(shù)默認128
通過mongostat查看運行狀態(tài)齐佳,如果qw持續(xù)較高、aw經(jīng)常是128(默認值)债沮,說明寫請求發(fā)生排隊炼吴,同時WT無法提供更高的并發(fā)寫。
查看wiredTigerConcurrentWriteTransactions配置
db.adminCommand({getParameter: 1, wiredTigerConcurrentWriteTransactions: 1})