10. 监控工具
1. 为什么要监控¶
监控及时获得应用的运行状态信息,在问题出现时及时发现
2. 监控什么¶
-cpu、内存、磁盘i/o
-应用程序(MongoDB)
-进程监控(ps -aux)
-错误日志监控
3. 内部监控命令¶
db.serverStatus()
-查看实例运行状态(内存使用、锁、用户连接等信息)
-通过比对前后快照进行性能分析
#1.进入复制集主节点
[mongod@151 ~]$ mongo --port 28017
MongoDB shell version: 3.2.22
connecting to: 127.0.0.1:28017/test
my_repl:PRIMARY>
#2.查看,信息非常多。。。
my_repl:PRIMARY> db.serverStatus()
{
"host" : "151:28017",
"advisoryHostFQDNs" : [ ],
"version" : "3.2.22",
"process" : "mongod",
"pid" : NumberLong(70561),
"uptime" : 2661,
"uptimeMillis" : NumberLong(2661442),
"uptimeEstimate" : 2174,
"localTime" : ISODate("2021-05-13T08:10:14.808Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 0,
"rollovers" : 0
},
"connections" : {
"current" : 7,
"available" : 812,
"totalCreated" : NumberLong(43)
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 83676960,
"page_faults" : 13
},
"globalLock" : {
"totalTime" : NumberLong("2661439000"),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 22,
"readers" : 0,
"writers" : 0
}
},
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(69123),
"w" : NumberLong(26139),
"R" : NumberLong(1),
"W" : NumberLong(4019)
},
"acquireWaitCount" : {
"r" : NumberLong(312)
},
"timeAcquiringMicros" : {
"r" : NumberLong(745344)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(19464),
"w" : NumberLong(26102),
"R" : NumberLong(18),
"W" : NumberLong(37)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(6198),
"w" : NumberLong(18076),
"W" : NumberLong(4)
}
},
"Metadata" : {
"acquireCount" : {
"w" : NumberLong(8024)
}
},
"oplog" : {
"acquireCount" : {
"r" : NumberLong(13551),
"w" : NumberLong(8025)
}
}
},
"network" : {
"bytesIn" : NumberLong(11009726),
"bytesOut" : NumberLong(9449118),
"numRequests" : NumberLong(25947)
},
"opcounters" : {
"insert" : 22013,
"query" : 20,
"update" : 0,
"delete" : 0,
"getmore" : 7201,
"command" : 16697
},
"opcountersRepl" : {
"insert" : 2004,
"query" : 0,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 0
},
"repl" : {
"hosts" : [
"192.168.178.151:28019",
"192.168.178.151:28020",
"192.168.178.151:28018",
"192.168.178.151:28017"
],
"setName" : "my_repl",
"setVersion" : 16,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.178.151:28017",
"me" : "192.168.178.151:28017",
"electionId" : ObjectId("7fffffff0000000000000003"),
"rbid" : 579904464
},
"storageEngine" : {
"name" : "wiredTiger",
"supportsCommittedReads" : true,
"persistent" : true
},
"tcmalloc" : {
"generic" : {
"current_allocated_bytes" : 83678480,
"heap_size" : 109355008
},
"tcmalloc" : {
"pageheap_free_bytes" : 14688256,
"pageheap_unmapped_bytes" : 90112,
"max_total_thread_cache_bytes" : NumberLong(1073741824),
"current_total_thread_cache_bytes" : 4137864,
"total_free_bytes" : 10898160,
"central_cache_free_bytes" : 2633048,
"transfer_cache_free_bytes" : 4127248,
"thread_cache_free_bytes" : 4137864,
"aggressive_memory_decommit" : 0,
"pageheap_committed_bytes" : 109264896,
"pageheap_scavenge_count" : 24,
"pageheap_commit_count" : 233,
"pageheap_total_commit_bytes" : 187850752,
"pageheap_decommit_count" : 24,
"pageheap_total_decommit_bytes" : 78585856,
"pageheap_reserve_count" : 50,
"pageheap_total_reserve_bytes" : 109355008,
"spinlock_total_delay_ns" : 2480700,
"formattedString" : "------------------------------------------------\nMALLOC: 83678928 ( 79.8 MiB) Bytes in use by application\nMALLOC: + 14688256 ( 14.0 MiB) Bytes in page heap freelist\nMALLOC: + 2633048 ( 2.5 MiB) Bytes in central cache freelist\nMALLOC: + 4127248 ( 3.9 MiB) Bytes in transfer cache freelist\nMALLOC: + 4137416 ( 3.9 MiB) Bytes in thread cache freelists\nMALLOC: + 1388760 ( 1.3 MiB) Bytes in malloc metadata\nMALLOC: ------------\nMALLOC: = 110653656 ( 105.5 MiB) Actual memory used (physical + swap)\nMALLOC: + 90112 ( 0.1 MiB) Bytes released to OS (aka unmapped)\nMALLOC: ------------\nMALLOC: = 110743768 ( 105.6 MiB) Virtual address space used\nMALLOC:\nMALLOC: 1008 Spans in use\nMALLOC: 65 Thread heaps in use\nMALLOC: 8192 Tcmalloc page size\n------------------------------------------------\nCall ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).\nBytes released to the OS take up virtual address space but no physical memory.\n"
}
},
"wiredTiger" : {
"uri" : "statistics:",
"LSM" : {
"application work units currently queued" : 0,
"merge work units currently queued" : 0,
"rows merged in an LSM tree" : 0,
"sleep for LSM checkpoint throttle" : 0,
"sleep for LSM merge throttle" : 0,
"switch work units currently queued" : 0,
"tree maintenance operations discarded" : 0,
"tree maintenance operations executed" : 0,
"tree maintenance operations scheduled" : 0,
"tree queue hit maximum" : 0
},
"async" : {
"current work queue length" : 0,
"maximum work queue length" : 0,
"number of allocation state races" : 0,
"number of flush calls" : 0,
"number of operation slots viewed for allocation" : 0,
"number of times operation allocation failed" : 0,
"number of times worker found no work" : 0,
"total allocations" : 0,
"total compact calls" : 0,
"total insert calls" : 0,
"total remove calls" : 0,
"total search calls" : 0,
"total update calls" : 0
},
"block-manager" : {
"blocks pre-loaded" : 11,
"blocks read" : 87,
"blocks written" : 277,
"bytes read" : 376832,
"bytes written" : 2093056,
"bytes written for checkpoint" : 2093056,
"mapped blocks read" : 0,
"mapped bytes read" : 0
},
"cache" : {
"application threads page read from disk to cache count" : 10,
"application threads page read from disk to cache time (usecs)" : 3426,
"application threads page write from cache to disk count" : 0,
"application threads page write from cache to disk time (usecs)" : 0,
"bytes belonging to page images in the cache" : 31809,
"bytes currently in the cache" : 2174130,
"bytes not belonging to page images in the cache" : 2142321,
"bytes read into cache" : 29581,
"bytes written from cache" : 3683496,
"checkpoint blocked page eviction" : 0,
"eviction calls to get a page" : 0,
"eviction calls to get a page found queue empty" : 0,
"eviction calls to get a page found queue empty after locking" : 0,
"eviction currently operating in aggressive mode" : 0,
"eviction empty score" : 0,
"eviction server candidate queue empty when topping up" : 0,
"eviction server candidate queue not empty when topping up" : 0,
"eviction server evicting pages" : 0,
"eviction server slept, because we did not make progress with eviction" : 0,
"eviction server unable to reach eviction goal" : 0,
"eviction state" : 16,
"eviction walks abandoned" : 0,
"eviction worker thread active" : 0,
"eviction worker thread created" : 0,
"eviction worker thread evicting pages" : 0,
"eviction worker thread removed" : 0,
"eviction worker thread stable number" : 0,
"failed eviction of pages that exceeded the in-memory maximum" : 0,
"files with active eviction walks" : 0,
"files with new eviction walks started" : 0,
"force re-tuning of eviction workers once in a while" : 0,
"hazard pointer blocked page eviction" : 0,
"hazard pointer check calls" : 0,
"hazard pointer check entries walked" : 0,
"hazard pointer maximum array length" : 0,
"in-memory page passed criteria to be split" : 0,
"in-memory page splits" : 0,
"internal pages evicted" : 0,
"internal pages split during eviction" : 0,
"leaf pages split during eviction" : 0,
"lookaside table insert calls" : 0,
"lookaside table remove calls" : 0,
"maximum bytes configured" : 1073741824,
"maximum page size at eviction" : 0,
"modified pages evicted" : 0,
"modified pages evicted by application threads" : 0,
"overflow pages read into cache" : 0,
"overflow values cached in memory" : 0,
"page split during eviction deepened the tree" : 0,
"page written requiring lookaside records" : 0,
"pages currently held in the cache" : 37,
"pages evicted because they exceeded the in-memory maximum" : 0,
"pages evicted because they had chains of deleted items" : 0,
"pages evicted by application threads" : 0,
"pages queued for eviction" : 0,
"pages queued for urgent eviction" : 0,
"pages queued for urgent eviction during walk" : 0,
"pages read into cache" : 22,
"pages read into cache requiring lookaside entries" : 0,
"pages requested from the cache" : 102676,
"pages seen by eviction walk" : 0,
"pages selected for eviction unable to be evicted" : 0,
"pages walked for eviction" : 0,
"pages written from cache" : 149,
"pages written requiring in-memory restoration" : 0,
"percentage overhead" : 8,
"tracked bytes belonging to internal pages in the cache" : 18150,
"tracked bytes belonging to leaf pages in the cache" : 2155980,
"tracked dirty bytes in the cache" : 0,
"tracked dirty pages in the cache" : 0,
"unmodified pages evicted" : 0
},
"connection" : {
"auto adjusting condition resets" : 577,
"auto adjusting condition wait calls" : 16648,
"detected system time went backwards" : 0,
"files currently open" : 22,
"memory allocations" : 247466,
"memory frees" : 221925,
"memory re-allocations" : 50318,
"pthread mutex condition wait calls" : 43032,
"pthread mutex shared lock read-lock calls" : 68684,
"pthread mutex shared lock write-lock calls" : 3018,
"total fsync I/Os" : 438,
"total read I/Os" : 1379,
"total write I/Os" : 1058
},
"cursor" : {
"cursor create calls" : 215,
"cursor insert calls" : 56198,
"cursor next calls" : 37279,
"cursor prev calls" : 22,
"cursor remove calls" : 22,
"cursor reset calls" : 86536,
"cursor restarted searches" : 0,
"cursor search calls" : 48964,
"cursor search near calls" : 161,
"cursor update calls" : 0,
"truncate calls" : 0
},
"data-handle" : {
"connection data handles currently active" : 24,
"connection sweep candidate became referenced" : 0,
"connection sweep dhandles closed" : 5,
"connection sweep dhandles removed from hash list" : 39,
"connection sweep time-of-death sets" : 50,
"connection sweeps" : 265,
"session dhandles swept" : 11,
"session sweep attempts" : 66
},
"lock" : {
"checkpoint lock acquisitions" : 20,
"checkpoint lock application thread wait time (usecs)" : 0,
"checkpoint lock internal thread wait time (usecs)" : 235,
"handle-list lock eviction thread wait time (usecs)" : 115785,
"metadata lock acquisitions" : 20,
"metadata lock application thread wait time (usecs)" : 0,
"metadata lock internal thread wait time (usecs)" : 157,
"schema lock acquisitions" : 57,
"schema lock application thread wait time (usecs)" : 275,
"schema lock internal thread wait time (usecs)" : 188,
"table lock acquisitions" : 0,
"table lock application thread time waiting for the table lock (usecs)" : 0,
"table lock internal thread time waiting for the table lock (usecs)" : 0
},
"log" : {
"busy returns attempting to switch slots" : 0,
"consolidated slot closures" : 745,
"consolidated slot join active slot closed" : 0,
"consolidated slot join races" : 0,
"consolidated slot join transitions" : 745,
"consolidated slot joins" : 8144,
"consolidated slot transitions unable to find free slot" : 0,
"consolidated slot unbuffered writes" : 0,
"log bytes of payload data" : 1707764,
"log bytes written" : 2097920,
"log files manually zero-filled" : 0,
"log flush operations" : 26075,
"log force write operations" : 29288,
"log force write operations skipped" : 28585,
"log records compressed" : 40,
"log records not compressed" : 8038,
"log records too small to compress" : 66,
"log release advances write LSN" : 40,
"log scan operations" : 5,
"log scan records requiring two reads" : 0,
"log server thread advances write LSN" : 705,
"log server thread write LSN walk skipped" : 94408,
"log sync operations" : 305,
"log sync time duration (usecs)" : 775671,
"log sync_dir operations" : 1,
"log sync_dir time duration (usecs)" : 10,
"log write operations" : 8144,
"logging bytes consolidated" : 2097536,
"maximum log file size" : 104857600,
"number of pre-allocated log files to create" : 2,
"pre-allocated log files not ready and missed" : 1,
"pre-allocated log files prepared" : 2,
"pre-allocated log files used" : 0,
"records processed by log scan" : 10,
"total in-memory size of compressed records" : 67713,
"total log buffer size" : 33554432,
"total size of compressed records" : 28268,
"written slots coalesced" : 0,
"yields waiting for previous log file close" : 0
},
"reconciliation" : {
"fast-path pages deleted" : 0,
"page reconciliation calls" : 139,
"page reconciliation calls for eviction" : 0,
"pages deleted" : 0,
"split bytes currently awaiting free" : 0,
"split objects currently awaiting free" : 0
},
"session" : {
"open cursor count" : 62,
"open session count" : 20,
"table alter failed calls" : 0,
"table alter successful calls" : 0,
"table alter unchanged and skipped" : 0,
"table compact failed calls" : 0,
"table compact successful calls" : 0,
"table create failed calls" : 0,
"table create successful calls" : 13,
"table drop failed calls" : 5,
"table drop successful calls" : 6,
"table rebalance failed calls" : 0,
"table rebalance successful calls" : 0,
"table rename failed calls" : 0,
"table rename successful calls" : 0,
"table salvage failed calls" : 0,
"table salvage successful calls" : 0,
"table truncate failed calls" : 0,
"table truncate successful calls" : 0,
"table verify failed calls" : 0,
"table verify successful calls" : 0
},
"thread-state" : {
"active filesystem fsync calls" : 0,
"active filesystem read calls" : 0,
"active filesystem write calls" : 0
},
"thread-yield" : {
"application thread time evicting (usecs)" : 0,
"application thread time waiting for cache (usecs)" : 0,
"page acquire busy blocked" : 0,
"page acquire eviction blocked" : 0,
"page acquire locked blocked" : 0,
"page acquire read blocked" : 0,
"page acquire time sleeping (usecs)" : 0
},
"transaction" : {
"number of named snapshots created" : 0,
"number of named snapshots dropped" : 0,
"transaction begins" : 40036,
"transaction checkpoint currently running" : 0,
"transaction checkpoint generation" : 20,
"transaction checkpoint max time (msecs)" : 134,
"transaction checkpoint min time (msecs)" : 2,
"transaction checkpoint most recent time (msecs)" : 4,
"transaction checkpoint scrub dirty target" : 0,
"transaction checkpoint scrub time (msecs)" : 0,
"transaction checkpoint total time (msecs)" : 393,
"transaction checkpoints" : 20,
"transaction checkpoints skipped because database was clean" : 25,
"transaction failures due to cache overflow" : 0,
"transaction fsync calls for checkpoint after allocating the transaction ID" : 20,
"transaction fsync duration for checkpoint after allocating the transaction ID (usecs)" : 8,
"transaction range of IDs currently pinned" : 0,
"transaction range of IDs currently pinned by a checkpoint" : 0,
"transaction range of IDs currently pinned by named snapshots" : 0,
"transaction sync calls" : 0,
"transactions committed" : 10040,
"transactions rolled back" : 29996
},
"concurrentTransactions" : {
"write" : {
"out" : 0,
"available" : 128,
"totalTickets" : 128
},
"read" : {
"out" : 0,
"available" : 128,
"totalTickets" : 128
}
}
},
"writeBacksQueued" : false,
"mem" : {
"bits" : 64,
"resident" : 50,
"virtual" : 835,
"supported" : true,
"mapped" : 0,
"mappedWithJournal" : 0
},
"metrics" : {
"commands" : {
"_isSelf" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"applyOps" : {
"failed" : NumberLong(0),
"total" : NumberLong(2008)
},
"buildInfo" : {
"failed" : NumberLong(0),
"total" : NumberLong(7)
},
"count" : {
"failed" : NumberLong(0),
"total" : NumberLong(14)
},
"create" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"createIndexes" : {
"failed" : NumberLong(0),
"total" : NumberLong(8)
},
"drop" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
},
"find" : {
"failed" : NumberLong(0),
"total" : NumberLong(16)
},
"getLog" : {
"failed" : NumberLong(0),
"total" : NumberLong(7)
},
"getMore" : {
"failed" : NumberLong(0),
"total" : NumberLong(7193)
},
"getnonce" : {
"failed" : NumberLong(0),
"total" : NumberLong(24)
},
"insert" : {
"failed" : NumberLong(0),
"total" : NumberLong(2033)
},
"isMaster" : {
"failed" : NumberLong(0),
"total" : NumberLong(56)
},
"killCursors" : {
"failed" : NumberLong(0),
"total" : NumberLong(1)
},
"listCollections" : {
"failed" : NumberLong(0),
"total" : NumberLong(16)
},
"listDatabases" : {
"failed" : NumberLong(0),
"total" : NumberLong(3)
},
"listIndexes" : {
"failed" : NumberLong(0),
"total" : NumberLong(4)
},
"ping" : {
"failed" : NumberLong(0),
"total" : NumberLong(15)
},
"replSetGetRBID" : {
"failed" : NumberLong(0),
"total" : NumberLong(6)
},
"replSetGetStatus" : {
"failed" : NumberLong(0),
"total" : NumberLong(135)
},
"replSetHeartbeat" : {
"failed" : NumberLong(0),
"total" : NumberLong(2643)
},
"replSetUpdatePosition" : {
"failed" : NumberLong(0),
"total" : NumberLong(11734)
},
"serverStatus" : {
"failed" : NumberLong(0),
"total" : NumberLong(2)
},
"whatsmyuri" : {
"failed" : NumberLong(0),
"total" : NumberLong(7)
}
},
"cursor" : {
"timedOut" : NumberLong(0),
"open" : {
"noTimeout" : NumberLong(0),
"pinned" : NumberLong(2),
"total" : NumberLong(2)
}
},
"document" : {
"deleted" : NumberLong(0),
"inserted" : NumberLong(12010),
"returned" : NumberLong(24115),
"updated" : NumberLong(0)
},
"getLastError" : {
"wtime" : {
"num" : 30,
"totalMillis" : 223
},
"wtimeouts" : NumberLong(0)
},
"operation" : {
"fastmod" : NumberLong(0),
"idhack" : NumberLong(0),
"scanAndOrder" : NumberLong(0),
"writeConflicts" : NumberLong(4)
},
"queryExecutor" : {
"scanned" : NumberLong(0),
"scannedObjects" : NumberLong(26118)
},
"record" : {
"moves" : NumberLong(0)
},
"repl" : {
"executor" : {
"counters" : {
"eventCreated" : 6,
"eventWait" : 6,
"cancels" : 11741,
"waits" : 36925,
"scheduledNetCmd" : 6637,
"scheduledDBWork" : 1,
"scheduledXclWork" : 0,
"scheduledWorkAt" : 15785,
"scheduledWork" : 48659,
"schedulingFailures" : 0
},
"queues" : {
"networkInProgress" : 0,
"dbWorkInProgress" : 0,
"exclusiveInProgress" : 0,
"sleepers" : 4,
"ready" : 0,
"free" : 8
},
"unsignaledEvents" : 4,
"eventWaiters" : 0,
"shuttingDown" : false,
"networkInterface" : "NetworkInterfaceASIO inShutdown: 0"
},
"apply" : {
"attemptsToBecomeSecondary" : NumberLong(1),
"batches" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0)
},
"buffer" : {
"count" : NumberLong(0),
"maxSizeBytes" : 268435456,
"sizeBytes" : NumberLong(0)
},
"network" : {
"bytes" : NumberLong(0),
"getmores" : {
"num" : 0,
"totalMillis" : 0
},
"ops" : NumberLong(0),
"readersCreated" : NumberLong(11)
},
"preload" : {
"docs" : {
"num" : 0,
"totalMillis" : 0
},
"indexes" : {
"num" : 0,
"totalMillis" : 0
}
}
},
"storage" : {
"freelist" : {
"search" : {
"bucketExhausted" : NumberLong(0),
"requests" : NumberLong(0),
"scanned" : NumberLong(0)
}
}
},
"ttl" : {
"deletedDocuments" : NumberLong(0),
"passes" : NumberLong(44)
}
},
"ok" : 1
}
my_repl:PRIMARY>
#2.单节点也能用
[mongod@151 ~]$ mongo -uroot -proot123 admin
MongoDB shell version: 3.2.22
connecting to: admin
>
> db.serverStatus()
{
"host" : "151",
"advisoryHostFQDNs" : [ ],
"version" : "3.2.22",
"process" : "mongod",
"pid" : NumberLong(30019),
"uptime" : 9552,
"uptimeMillis" : NumberLong(9552588),
"uptimeEstimate" : 7923,
"localTime" : ISODate("2021-05-13T08:13:47.912Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 2,
"rollovers" : 0
},
"connections" : {
"current" : 1,
"available" : 818,
"totalCreated" : NumberLong(50)
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 79180232,
"page_faults" : 17
},
"globalLock" : {
"totalTime" : NumberLong("9552415000"),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 8,
"readers" : 0,
"writers" : 0
}
},
。。。
4. 命令行监控工具1-mongostat¶
#1.介绍
-实时数据库状态,读写、加锁、索引命中、缺页中断、读写等待队列等情况
-每秒刷新一次状态值,并能提供良好的可读性,通过这些参数可以观察到MongoDB系统整体性能情况
#2.用法
mongostat --port 28017
#3.参数说明
insert:每秒插入量
query: 每秒查询量
update:每秒更新量
delete:每秒删除量
conn: 当前连接数
qr|qw; 客户端查询排队长度(读|写)
最好为0,如果有堆积,数据库处理慢
time: 当前时间
#1.复制集
[mongod@151 ~]$ mongostat --port 28017
insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn set repl time
*0 *0 *0 *0 0 2|0 0.0 0.2 0 835M 50.0M 0|0 0|0 246b 22.6k 7 my_repl PRI 2021-05-13T16:12:14+08:00
*0 *0 *0 *0 2 4|0 0.0 0.2 0 835M 50.0M 0|0 0|0 1.96k 23.3k 7 my_repl PRI 2021-05-13T16:12:15+08:00
*0 *0 *0 *0 0 2|0 0.0 0.2 0 835M 50.0M 0|0 0|0 246b 22.6k 7 my_repl PRI 2021-05-13T16:12:16+08:00
*0 *0 *0 *0 0 2|0 0.0 0.2 0 835M 50.0M 0|0 0|0 246b 22.6k 7 my_repl PRI 2021-05-13T16:12:17+08:00
*0 *0 *0 *0 0 2|0 0.0 0.2 0 835M 50.0M 0|0 0|0 246b 22.6k 7 my_repl PRI 2021-05-13T16:12:18+08:00
*0 *0 *0 *0 0 2|0 0.0 0.2 0 835M 50.0M 0|0 0|0 246b 22.6k 7 my_repl PRI 2021-05-13T16:12:19+08:00
*0 *0 *0 *0 2 4|0 0.0 0.2 0 835M 50.0M 0|0 0|0 1.96k 23.3k 7 my_repl PRI 2021-05-13T16:12:20+08:00
#2.单节点
[mongod@151 ~]$ mongostat --port 27017
2021-05-13T16:12:53.652+0800 Failed: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 }
[mongod@151 ~]$
[mongod@151 ~]$
[mongod@151 ~]$
[mongod@151 ~]$ netstat -tunlp|grep 27017
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 30019/mongod
[mongod@151 ~]$
[mongod@151 ~]$ mongostat --port 27017
2021-05-13T16:13:12.709+0800 Failed: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 }
#报错
#加上用户名和验证库。好用。
[mongod@151 ~]$ mongostat --port 27017 -uroot -proot123
2021-05-13T16:17:19.543+0800 --authenticationDatabase is required when authenticating against a non $external database
[mongod@151 ~]$
[mongod@151 ~]$ mongostat --port 27017 -uroot -proot123 --authenticationDatabase admin
insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:22+08:00
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:23+08:00
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:24+08:00
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:25+08:00
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:26+08:00
*0 *0 *0 *0 0 1|0 0.0 1.5 0 468M 69.0M 0|0 0|0 79b 21.8k 1 2021-05-13T16:17:27+08:00
^C[mongod@151 ~]$
5. 命令行监控工具2-mongotop¶
#1.用法
mongotop --port 38022
#2.参数说明
ns:数据库命名空间,后者结合了数据库名称和集合
total:mongod在这个命名空间上花费的总时间
read: 在这个命名空间上mongod执行读操作花费的时间
write:在这个命名空间上mongod进行写操作花费的时间
#1.复制集,每秒一次数据
[mongod@151 ~]$ mongotop --port 28017
2021-05-13T16:14:56.466+0800 connected to: 127.0.0.1:28017
ns total read write 2021-05-13T16:14:57+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.me 0ms 0ms 0ms
local.oplog.rs 0ms 0ms 0ms
local.replset.election 0ms 0ms 0ms
local.replset.minvalid 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
test.t1 0ms 0ms 0ms
wo.ci 0ms 0ms 0ms
ns total read write 2021-05-13T16:14:58+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
local.me 0ms 0ms 0ms
local.oplog.rs 0ms 0ms 0ms
local.replset.election 0ms 0ms 0ms
local.replset.minvalid 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
test.t1 0ms 0ms 0ms
wo.ci 0ms 0ms 0ms
#2.单节点
[mongod@151 ~]$ mongotop --port 27017 -uroot -proot123 --authenticationDatabase admin
2021-05-13T16:16:04.075+0800 connected to: 127.0.0.1:27017
ns total read write 2021-05-13T16:16:05+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.users 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
app.a 0ms 0ms 0ms
app.b 0ms 0ms 0ms
app.log 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
oldboy.log 0ms 0ms 0ms
oldboy.log1 0ms 0ms 0ms
ns total read write 2021-05-13T16:16:06+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.users 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
app.a 0ms 0ms 0ms
app.b 0ms 0ms 0ms
app.log 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
oldboy.log 0ms 0ms 0ms
oldboy.log1 0ms 0ms 0ms
6. 其他监控命令¶
#1.db.currentOp()
1.查看数据库当前执行什么操作
2.用于查看长时间运行进程
3.通过(执行时长、操作、锁、等待锁时长)等条件过滤
4.如果发现一个操作太长,把数据库卡死的话,可以杀死这个命令
db.killOp(608605) #括号里跟opid
#2.db.setProfilingLevel()
1.设置server级别慢日志
0-不保存
1-保存慢查询日志
2-保存所有查询日志
#1.
[mongod@151 ~]$ mongo --port 28017
MongoDB shell version: 3.2.22
connecting to: 127.0.0.1:28017/test
my_repl:PRIMARY>
my_repl:PRIMARY> db.currentOp()
{
"inprog" : [
{
"desc" : "conn48",
"threadId" : "140612779759360",
"connectionId" : 48,
"client" : "127.0.0.1:42114",
"active" : true,
"opid" : 43519,
"secs_running" : 0,
"microsecs_running" : NumberLong(38),
"op" : "command",
"ns" : "admin.$cmd",
"query" : {
"currentOp" : 1
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
},
{
"desc" : "rsSync",
"threadId" : "140613137233664",
"active" : true,
"opid" : 93,
"op" : "none",
"ns" : "",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
},
{
"desc" : "ReplBatcher",
"threadId" : "140612802860800",
"active" : true,
"opid" : 43517,
"op" : "none",
"ns" : "",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
},
{
"desc" : "conn7",
"threadId" : "140612785022720",
"connectionId" : 7,
"client" : "192.168.178.151:42050",
"active" : true,
"opid" : 43505,
"secs_running" : 2,
"microsecs_running" : NumberLong(2346961),
"op" : "getmore",
"ns" : "local.oplog.rs",
"query" : {
"getMore" : NumberLong("13848933467"),
"collection" : "oplog.rs",
"maxTimeMS" : NumberLong(5000),
"term" : NumberLong(3),
"lastKnownCommittedOpTime" : {
"ts" : Timestamp(1620893240, 70),
"t" : NumberLong(3)
}
},
"planSummary" : "COLLSCAN",
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(2)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"r" : NumberLong(1)
}
}
}
},
{
"desc" : "conn10",
"threadId" : "140612782917376",
"connectionId" : 10,
"client" : "192.168.178.151:42074",
"active" : true,
"opid" : 43506,
"secs_running" : 2,
"microsecs_running" : NumberLong(2345715),
"op" : "getmore",
"ns" : "local.oplog.rs",
"query" : {
"getMore" : NumberLong("13919156199"),
"collection" : "oplog.rs",
"maxTimeMS" : NumberLong(5000),
"term" : NumberLong(3),
"lastKnownCommittedOpTime" : {
"ts" : Timestamp(1620893240, 70),
"t" : NumberLong(3)
}
},
"planSummary" : "COLLSCAN",
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(2)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"r" : NumberLong(1)
}
}
}
},
{
"desc" : "WT RecordStoreThread: local.oplog.rs",
"threadId" : "140613259032320",
"active" : true,
"opid" : 21,
"secs_running" : 3151,
"microsecs_running" : NumberLong("3151787864"),
"op" : "none",
"ns" : "local.oplog.rs",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
}
}
],
"ok" : 1
}
my_repl:PRIMARY>
#2.
my_repl:PRIMARY> db.setProfilingLevel()
{ "was" : 0, "slowms" : 100, "ok" : 1 }
my_repl:PRIMARY>
7. MongoDB集群性能优化方案¶
1.硬件(内存、SSD)
2.收缩数据
3.增加新的机器、新的副本集
4.集群分片键选择
5.chunk大小设置
6.预分片(预先分配存储空间)
8. chunk大小的选择¶
#1.适合业务的chunksize是最好的
#2.chunk的分裂和迁移,非常消耗io资源
#3.chunk分裂的时机:插入和更新,读数据不会分裂
#4.chunksize的选择
-小的chunksize:数据均衡时迁移速度快,数据分布更均匀。数据分裂频繁,路由接地但消耗更多资源。
-大的chunksize:数据分裂少。数据块移动集中消耗io资源
-通常100-200M
9. 执行计划¶
#1.作用:优化查询语句
#2.用法
use world
show tables
db.city.find().explain()
[mongod@151 ~]$ mongo --port 28017
MongoDB shell version: 3.2.22
connecting to: 127.0.0.1:28017/test
my_repl:PRIMARY>
my_repl:PRIMARY> show dbs
local 0.001GB
test 0.000GB
wo 0.000GB
my_repl:PRIMARY>
my_repl:PRIMARY> use wo
switched to db wo
my_repl:PRIMARY>
my_repl:PRIMARY> show tables
ci
ci1
ci2
my_repl:PRIMARY>
my_repl:PRIMARY> db.ci.find().explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "wo.ci",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [ ]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [ ]
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "151",
"port" : 28017,
"version" : "3.2.22",
"gitVersion" : "105acca0d443f9a47c1a5bd608fd7133840a58dd"
},
"ok" : 1
}
my_repl:PRIMARY>
最后更新:
2022-02-20 11:21:58