2

给您的 MongoDB 定期做个体检:MongoDB 诊断

 1 year ago
source link: https://www.51cto.com/article/751674.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

给您的 MongoDB 定期做个体检:MongoDB 诊断-51CTO.COM

随着 MongoDB 的不断运行且数据量日益增多,就会不免产生一些问题。当问题出现时,该怎么办呢?我们会从以下几个方面进行探讨。

当有个长期运行的 MongoDB 进程时,我们可能想自己写个脚本来定期轮转日志文件。其实不用,MongoDB 已经提供了 logrotate 命令来完成这个操作,可以直接从 shell 里启动使用:



use admin


rs0:PRIMARY> db.runCommand({logRotate: 1})
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1679966896, 1),
        "signature" : {
            "hash" : BinData(0,"50vWhFp/59fAKwBbUQUDxApF/+g="),
            "keyId" : NumberLong("7173500260914823169")
        }
    },
    "operationTime" : Timestamp(1679966896, 1)
}



需要注意:logRotate 是区分大小写的。

或者可以向 Mongo 进程发送 SIGUSR1 信号:



$ kill -SIGUSR1 <mongo_pid>



MongoDB 包含了许多命令可以报告内部状态,这些命令背后都是监控 MongoDB。下面是一些常见命令的参考命令,希望对大家有所帮助:

01全局服务器信息统计

该命令输出的信息非常详细,当 MongoDB 出现问题时,是一个不错的诊断命令。



rs0:PRIMARY> db.serverStatus()
{
    "host" : "mongo03.tyun.cn",
    "version" : "4.4.15",
    "process" : "mongod",
    "pid" : NumberLong(14092),
    "uptime" : 18727504,
    "uptimeMillis" : NumberLong("18727504137"),
    "uptimeEstimate" : NumberLong(18727504),
    "localTime" : ISODate("2023-03-28T01:30:35.805Z"),
    "asserts" : {
        "regular" : 0,
        "warning" : 0,
        "msg" : 0,
        "user" : 1380727,
        "rollovers" : 0
    },
    "connections" : {
        "current" : 17,
        "available" : 51183,
        "totalCreated" : 3253,
        "active" : 7,
        "exhaustIsMaster" : 3,
        "exhaustHello" : 2,
        "awaitingTopologyChanges" : 176
    },
    "defaultRWConcern" : {
        "localUpdateWallClockTime" : ISODate("2022-08-23T07:25:32.995Z")
    },


        "tcmalloc" : {
            "pageheap_free_bytes" : 94162944,
            "pageheap_unmapped_bytes" : 349478912,
            "max_total_thread_cache_bytes" : 1024458752,
            "current_total_thread_cache_bytes" : 238198560,
            "total_free_bytes" : 637257904,
            "central_cache_free_bytes" : 397502736,
            "transfer_cache_free_bytes" : 1556608,
            "thread_cache_free_bytes" : 238198560,
            "aggressive_memory_decommit" : 0,
            "pageheap_committed_bytes" : NumberLong("3653197824"),
            "pageheap_scavenge_count" : 13297522,
            "pageheap_commit_count" : 14804116,
            "pageheap_total_commit_bytes" : NumberLong("60304102580224"),
            "pageheap_decommit_count" : 13297522,
            "pageheap_total_decommit_bytes" : NumberLong("60300449382400"),
            "pageheap_reserve_count" : 459,
            "pageheap_total_reserve_bytes" : NumberLong("4002676736"),
            "spinlock_total_delay_ns" : NumberLong("111430768421"),
            "release_rate" : 1,
            "formattedString" : "------------------------------------------------\nMALLOC:     2921777552 ( 2786.4 MiB) Bytes in use by application\nMALLOC: +     94162944 (   89.8 MiB) Bytes in page heap freelist\nMALLOC: +    397502736 (  379.1 MiB) Bytes in central cache freelist\nMALLOC: +      1556608 (    1.5 MiB) Bytes in transfer cache freelist\nMALLOC: +    238197984 (  227.2 MiB) Bytes in thread cache freelists\nMALLOC: +     24510464 (   23.4 MiB) Bytes in malloc metadata\nMALLOC:   ------------\nMALLOC: =   3677708288 ( 3507.3 MiB) Actual memory used (physical + swap)\nMALLOC: +    349478912 (  333.3 MiB) Bytes released to OS (aka unmapped)\nMALLOC:   ------------\nMALLOC: =   4027187200 ( 3840.6 MiB) Virtual address space used\nMALLOC:\nMALLOC:         265177              Spans in use\nMALLOC:             72              Thread heaps in use\nMALLOC:           4096              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"
        }
    },
    "trafficRecording" : {
        "running" : false
    },
    "transactions" : {
        "retriedCommandsCount" : NumberLong(0),
        "retriedStatementsCount" : NumberLong(0),
        "transactionsCollectionWriteCount" : NumberLong(0),
        "currentActive" : NumberLong(0),
        "currentInactive" : NumberLong(0),
        "currentOpen" : NumberLong(0),
        "totalAborted" : NumberLong(0),
        "totalCommitted" : NumberLong(0),
        "totalStarted" : NumberLong(0),
        "totalPrepared" : NumberLong(0),
        "totalPreparedThenCommitted" : NumberLong(0),
        "totalPreparedThenAborted" : NumberLong(0),
        "currentPrepared" : NumberLong(0)
    },
    "transportSecurity" : {
        "1.0" : NumberLong(0),
        "1.1" : NumberLong(0),
        "1.2" : NumberLong(0),
        "1.3" : NumberLong(0),
        "unknown" : NumberLong(0)
    },
    "twoPhaseCommitCoordinator" : {
        "totalCreated" : NumberLong(0),
        "totalStartedTwoPhaseCommit" : NumberLong(0),
        "totalAbortedTwoPhaseCommit" : NumberLong(0),
        "totalCommittedTwoPhaseCommit" : NumberLong(0),
        "currentInSteps" : {
            "writingParticipantList" : NumberLong(0),
            "waitingForVotes" : NumberLong(0),
            "writingDecision" : NumberLong(0),
            "waitingForDecisionAcks" : NumberLong(0),
            "deletingCoordinatorDoc" : NumberLong(0)
        }
    },
    "wiredTiger" : {
        "uri" : "statistics:",
        "block-manager" : {
            "blocks pre-loaded" : 23,
            "blocks read" : 4268362,
            "blocks written" : 39421996,
            "bytes read" : 48401854464,
            "bytes read via memory map API" : 0,
            "bytes read via system call API" : 0,
            "bytes written" : 418967826432,
            "bytes written for checkpoint" : 305570754560,
            "bytes written via memory map API" : 0,
            "bytes written via system call API" : 0,
            "mapped blocks read" : 0,
            "mapped bytes read" : 0,
            "number of times the file was remapped because it changed size via fallocate or truncate" : 0,
            "number of times the region was remapped via write" : 0
        },


        "capacity" : {
            "background fsync file handles considered" : 0,
            "background fsync file handles synced" : 0,
            "background fsync time (msecs)" : 0,
            "bytes read" : 39614959616,
            "bytes written for checkpoint" : 239185165378,
            "bytes written for eviction" : 97795992160,
            "bytes written for log" : 157233860864,
            "bytes written total" : 494215018402,
            "threshold to call fsync" : 0,
            "time waiting due to total capacity (usecs)" : 0,
            "time waiting during checkpoint (usecs)" : 0,
            "time waiting during eviction (usecs)" : 0,
            "time waiting during logging (usecs)" : 0,
            "time waiting during read (usecs)" : 0
        },
        "checkpoint-cleanup" : {
            "pages added for eviction" : 81667,
            "pages removed" : 16946,
            "pages skipped during tree walk" : 3064011324,
            "pages visited" : 5381579176
        },
        "connection" : {
            "auto adjusting condition resets" : 6326104,
            "auto adjusting condition wait calls" : 121447994,
            "auto adjusting condition wait raced to update timeout and skipped updating" : 61,
            "detected system time went backwards" : 0,
            "files currently open" : 71,
            "hash bucket array size for data handles" : 512,
            "hash bucket array size general" : 512,
            "memory allocations" : 14030754494,
            "memory frees" : 14027269174,
            "memory re-allocations" : 161883962,
            "pthread mutex condition wait calls" : 314479507,
            "pthread mutex shared lock read-lock calls" : 12181240198,
            "pthread mutex shared lock write-lock calls" : 743154980,
            "total fsync I/Os" : 275579938,
            "total read I/Os" : 4582332,
            "total write I/Os" : 313739357
        },


    "mem" : {
        "bits" : 64,
        "resident" : 3423,
        "virtual" : 5564,
        "supported" : true
    },
    "metrics" : {
        "aggStageCounters" : {
            "$_internalInhibitOptimization" : NumberLong(0),
            "$_internalSplitPipeline" : NumberLong(0),
            "$addFields" : NumberLong(1),
            "$bucket" : NumberLong(0),
            "$bucketAuto" : NumberLong(0),
            "$changeStream" : NumberLong(0),
            "$collStats" : NumberLong(0),
            "$count" : NumberLong(0),
            "$currentOp" : NumberLong(0),
            "$documents" : NumberLong(0),
            "$facet" : NumberLong(0),
            "$geoNear" : NumberLong(0),
            "$graphLookup" : NumberLong(0),
            "$group" : NumberLong(0),
            "$indexStats" : NumberLong(0),
            "$limit" : NumberLong(0),
            "$listLocalSessions" : NumberLong(0),
            "$listSessions" : NumberLong(0),
            "$lookup" : NumberLong(0),
            "$match" : NumberLong(1),
            "$merge" : NumberLong(0),
            "$mergeCursors" : NumberLong(0),
            "$out" : NumberLong(0),
            "$planCacheStats" : NumberLong(0),
            "$project" : NumberLong(2),
            "$queue" : NumberLong(0),
            "$redact" : NumberLong(0),
            "$replaceRoot" : NumberLong(0),
            "$replaceWith" : NumberLong(0),
            "$sample" : NumberLong(0),
            "$set" : NumberLong(46126),
            "$skip" : NumberLong(0),
            "$sort" : NumberLong(1),
            "$sortByCount" : NumberLong(0),
            "$unionWith" : NumberLong(0),
            "$unset" : NumberLong(0),
            "$unwind" : NumberLong(0)
        },
        "commands" : {
            "<UNKNOWN>" : NumberLong(2),
            "_isSelf" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(13)
            },
            "buildInfo" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(690365)
            },
            "count" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(6)
            },
            "delete" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(48)
            },
            "endSessions" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(134)
            },
            "find" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(2142077)
            },
            "getCmdLineOpts" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1380235)
            },
            "getDiagnosticData" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(2070345)
            },
            "getFreeMonitoringStatus" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(5)
            },
            "getLastError" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(101)
            },
            "getLog" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(5)
            },
            "getMore" : {
                "failed" : NumberLong(1),
                "total" : NumberLong(465344562)
            },
            "getnonce" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(2)
            },
            "hello" : {
                "failed" : NumberLong(268),
                "total" : NumberLong(1384888)
            },
            "insert" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1)
            },
            "isMaster" : {
                "failed" : NumberLong(2),
                "total" : NumberLong(7731521)
            },
            "killCursors" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(5)
            },
            "listCollections" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(10)
            },
            "listDatabases" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(57515)
            },
            "listIndexes" : {
                "failed" : NumberLong(3),
                "total" : NumberLong(124851)
            },
            "logRotate" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1)
            },
            "ping" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(4436308)
            },
            "replSetGetConfig" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(2070345)
            },
            "replSetGetRBID" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(6)
            },
            "replSetGetStatus" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(690128)
            },
            "replSetHeartbeat" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(17000850)
            },
            "replSetRequestVotes" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(4)
            },
            "replSetUpdatePosition" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(390211482)
            },
            "rolesInfo" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1)
            },
            "saslContinue" : {
                "failed" : NumberLong(6),
                "total" : NumberLong(62466)
            },
            "saslStart" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(57875)
            },
            "serverStatus" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1)
            },
            "update" : {
                "arrayFilters" : NumberLong(0),
                "failed" : NumberLong(0),
                "pipeline" : NumberLong(23063),
                "total" : NumberLong(511271)
            },
            "usersInfo" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(1)
            },
            "whatsmyuri" : {
                "failed" : NumberLong(0),
                "total" : NumberLong(5)
            }
        },
        "cursor" : {
            "timedOut" : NumberLong(0),
            "open" : {
                "noTimeout" : NumberLong(0),
                "pinned" : NumberLong(1),
                "total" : NumberLong(1)
            }
        },
        "document" : {
            "deleted" : NumberLong(0),
            "inserted" : NumberLong(1),
            "returned" : NumberLong(322743825),
            "updated" : NumberLong(522796)
        },
        "getLastError" : {
            "wtime" : {
                "num" : 11577,
                "totalMillis" : 37265
            },
            "wtimeouts" : NumberLong(1),
            "default" : {
                "unsatisfiable" : NumberLong(0),
                "wtimeouts" : NumberLong(0)
            }
        },
        "operation" : {
            "scanAndOrder" : NumberLong(15),
            "writeConflicts" : NumberLong(9825)
        },


        "query" : {
            "planCacheTotalSizeEstimateBytes" : NumberLong(0),
            "updateOneOpStyleBroadcastWithExactIDCount" : NumberLong(0),
            "multiPlanner" : {
                "classicCount" : NumberLong(0),
                "classicMicros" : NumberLong(0),
                "classicWorks" : NumberLong(0),
                "histograms" : {
                    "classicMicros" : [
                        {
                            "lowerBound" : NumberLong(0),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(1024),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(4096),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(16384),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(65536),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(262144),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(1048576),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(4194304),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(16777216),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(67108864),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(268435456),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(1073741824),
                            "count" : NumberLong(0)
                        }
                    ],
                    "classicNumPlans" : [
                        {
                            "lowerBound" : NumberLong(0),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(2),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(4),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(8),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(16),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(32),
                            "count" : NumberLong(0)
                        }
                    ],
                    "classicWorks" : [
                        {
                            "lowerBound" : NumberLong(0),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(128),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(256),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(512),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(1024),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(2048),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(4096),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(8192),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(16384),
                            "count" : NumberLong(0)
                        },
                        {
                            "lowerBound" : NumberLong(32768),
                            "count" : NumberLong(0)
                        }
                    ]
                }
            }
        },
        "queryExecutor" : {
            "scanned" : NumberLong(1070392),
            "scannedObjects" : NumberLong(323266658),
            "collectionScans" : {
                "nonTailable" : NumberLong(1386454),
                "total" : NumberLong(1386461)
            }
        },
        "record" : {
            "moves" : NumberLong(0)
        },
        "repl" : {
            "executor" : {
                "pool" : {
                    "inProgressCount" : 0
                },
                "queues" : {
                    "networkInProgress" : 0,
                    "sleepers" : 3
                },
                "unsignaledEvents" : 0,
                "shuttingDown" : false,
                "networkInterface" : "DEPRECATED: getDiagnosticString is deprecated in NetworkInterfaceTL"
            },
            "apply" : {
                "attemptsToBecomeSecondary" : NumberLong(19),
                "batchSize" : NumberLong(318318346),
                "batches" : {
                    "num" : 278600198,
                    "totalMillis" : 5795319
                },
                "ops" : NumberLong(318318943)
            },
            "buffer" : {
                "count" : NumberLong(0),
                "maxSizeBytes" : NumberLong(268435456),
                "sizeBytes" : NumberLong(0)
            },
            "initialSync" : {
                "completed" : NumberLong(0),
                "failedAttempts" : NumberLong(0),
                "failures" : NumberLong(0)
            },
            "network" : {
                "bytes" : NumberLong("57955381887"),
                "getmores" : {
                    "num" : 364468339,
                    "totalMillis" : 15110995960,
                    "numEmptyBatches" : NumberLong(67943454)
                },
                "notPrimaryLegacyUnacknowledgedWrites" : NumberLong(0),
                "notPrimaryUnacknowledgedWrites" : NumberLong(0),
                "oplogGetMoresProcessed" : {
                    "num" : 465344548,
                    "totalMillis" : 200161
                },
                "ops" : NumberLong(318318349),
                "readersCreated" : NumberLong(5),
                "replSetUpdatePosition" : {
                    "num" : NumberLong(455260555)
                }
            },
            "stateTransition" : {
                "lastStateTransition" : "stepUp",
                "userOperationsKilled" : NumberLong(0),
                "userOperationsRunning" : NumberLong(5)
            },
            "syncSource" : {
                "numSelections" : NumberLong(28),
                "numTimesChoseDifferent" : NumberLong(3),
                "numTimesChoseSame" : NumberLong(0),
                "numTimesCouldNotFind" : NumberLong(25)
            }
        },
        "ttl" : {
            "deletedDocuments" : NumberLong(9),
            "passes" : NumberLong(312121)
        }
    },
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1679967033, 3),
        "signature" : {
            "hash" : BinData(0,"7p9I6mnYmAqWdGWHZuVRCfEIJtU="),
            "keyId" : NumberLong("7173500260914823169")
        }
    },
    "operationTime" : Timestamp(1679967033, 3)
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK