对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB 日志消息

在正常操作中,MongoDB 会维护一个运行事件日志,包括传入连接、运行的命令和遇到的问题等条目。日志消息有助于诊断问题、监控部署和调优性能。

您可以使用以下方法获取日志消息:

mongod / mongos instances output all log messages in structured JSON format. Log entries are written as a series of key-value pairs, where each key indicates a log message field type, such as "severity", and each corresponding value records the associated logging information for that field type, such as "informational". Previously, log entries were output as plaintext.

例子

以下是 MongoDB 日志文件中可能出现的 JSON 格式的示例日志消息:

{"t":{"$date":"2020-05-01T15:16:17.180+00:00"},"s":"I", "c":"NETWORK", "id":12345, "ctx":"listener", "svc": "R", "msg":"Listening on", "attr":{"address":"127.0.0.1"}}

JSON log entries can be pretty-printed for readability. Here is the same log entry pretty-printed:

{
"t": {
"$date": "2020-05-01T15:16:17.180+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 12345,
"ctx": "listener",
"svc": "R",
"msg": "Listening on",
"attr": {
"address": "127.0.0.1"
}
}

In this log entry, for example, the key s, representing severity, has a corresponding value of I, representing "Informational", and the key c, representing component, has a corresponding value of NETWORK, indicating that the "network" component was responsible for this particular message. The various field types are presented in detail in the Log Message Field Types section.

Structured logging with key-value pairs allows for efficient parsing by automated tools or log ingestion services, and makes programmatic search and analysis of log messages easier to perform. Examples of analyzing structured log messages can be found in the Parsing Structured Log Messages section.

注意

如果无法写入日志文件,mongod 会退出。为了确保 mongod 可以写入日志文件,请验证日志卷在磁盘上是否有空间并且日志已轮换。

所有日志输出均采用 JSON 格式,包括发送至以下目标的输出:

getLog 命令的输出也是 JSON 格式。

每个日志条目以自包含 JSON 对象的形式输出,该对象遵循宽松扩展 JSON v2.0 规范,具有以下布局和字段顺序:

{
"t": <Datetime>, // timestamp
"s": <String>, // severity
"c": <String>, // component
"id": <Integer>, // unique identifier
"ctx": <String>, // context
"svc": <String>, // service
"msg": <String>, // message body
"attr": <Object>, // additional attributes (optional)
"tags": <Array of strings>, // tags (optional)
"truncated": <Object>, // truncation info (if truncated)
"size": <Object> // original size of entry (if truncated)
}

字段说明:

字段名称
类型
说明

t

日期时间

Timestamp of the log message in ISO-8601 format. For an example, see Timestamp.

s

字符串

Short severity code of the log message. For an example, see Severity.

c

字符串

Full component string for the log message. For an example, see Components.

id

整型

Unique identifier for the log statement. For an example, see Filtering by Known Log ID.

ctx

字符串

产生日志语句的线程的名称。

svc

字符串

在其上下文中发出日志声明的服务的名称。 将为S (表示“分分片”)、 R “路由器”或- (表示“未知”或“无”)。

msg

字符串

记录从服务器或驱动程序传递的输出消息。如有必要,消息将根据 JSON 规范进行转义。

attr

对象

用于其他日志属性的一个或多个键值对。如果日志消息不包含任何其他属性,则省略 attr 对象。

Attribute values may be referenced by their key name in the
msg message body, depending on the message. When required,
attributes are escaped
according to the JSON specification.

tags

字符串数组

表示适用于日志语句的任何标签的字符串。例如:["startupWarnings"]。

truncated

对象

Information about the log message truncation, if applicable. Only included if the log entry contains at least one truncated attr attribute.

size

对象

Original size of a log entry if it has been truncated. Only included if the log entry contains at least one truncated attr attribute.

message 和 attributes 字段会根据 Relaxed Extended JSON v2.0 规范对控制字符进行转义规范:

所代表的字符
转义序列

引号 (")

\"

Backslash (\)

\\

退格 (0x08)

\b

进纸 (0x0C)

\f

换行符 (0x0A)

\n

回车 (0x0D)

\r

Horizontal tab (0x09)

\t

上面未列出的控制字符使用 \uXXXX 进行转义,其中 "XXXX" 是十六进制的 unicode 代码点。具有无效 UTF-8 编码的字节将替换为 \ufffd 表示的 unicode 替换字符。

An example of message escaping is provided in the examples section.

在版本7.3中进行了更改。

任何超过 maxLogSizeKB 定义的最大大小(默认值:10 KB)的属性都将被截断。截断的属性会省略超出配置限制的日志数据,但保留条目的 JSON 格式,以确保条目仍可解析。

示例,以下JSON对象表示command属性,该属性在$in字段中包含未截断的5000元素。

注意

为了便于阅读,对示例日志条目进行了重新格式化。

{
"command": {
"find": "mycoll",
"filter": {
"value1": {
"$in": [0, 1, 2, 3, ... 4999]
},
"value2": "foo"
},
"sort": { "value1": 1 },
"lsid":{"id":{"$uuid":"80a99e49-a850-467b-a26d-aeb2d8b9f42b"}},
"$db": "testdb"
}
}

在此示例中,$in 数组在第 376 个元素处被截断,因为如果包含后续元素,command 属性的大小将超过 maxLogSizeKB。command 属性的其余部分被省略。截断的日志条目类似于以下输出:

{
"t": { "$date": "2021-03-17T20:30:07.212+01:00" },
"s": "I",
"c": "COMMAND",
"id": 51803,
"ctx": "conn9",
"msg": "Slow query",
"attr": {
"command": {
"find": "mycoll",
"filter": {
"value1": {
"$in": [ 0, 1, ..., 376 ] // Values in array omitted for brevity
}
}
},
... // Other attr fields omitted for brevity
},
"truncated": {
"command": {
"truncated": {
"filter": {
"truncated": {
"value1": {
"truncated": {
"$in": {
"truncated": {
"377": {
"type": "double",
"size": 8
}
},
"omitted": 4623
}
}
}
},
"omitted": 1
}
},
"omitted": 3
}
},
"size": {
"command": 21692
}
}

包含一个或多个截断属性的日志条目包括嵌套的truncated对象,该对象为日志条目中的每个截断属性提供以下信息:

  • 被截断的属性

  • 触发截断的该属性的特定子对象(如果适用)

  • 截断字段的数据 type

  • 触发截断的元素的 size(以字节为单位)

  • 每个子对象下因截断而为omitted的元素数量

具有截断属性的日志条目还可能在条目末尾包含一个附加的 size 字段,该字段指示截断之前属性的原始大小,在本例中为 21692 或大约 22KB。仅当此最终 size 字段与 truncated 对象中的 size 字段不同时才会显示。

输出到文件或系统日志日志目的地时,会在严重性、上下文和 id 字段之后添加填充,从而提高使用固定宽度字体查看时的可读性。

以下 MongoDB 日志文件摘录演示了这种填充:

{"t":{"$date":"2020-05-18T20:18:12.724+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main", "svc": "R", "msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2020-05-18T20:18:12.734+00:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main", "svc": "R", "msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2020-05-18T20:18:12.734+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main", "svc": "R", "msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2020-05-18T20:18:12.814+00:00"},"s":"I", "c":"STORAGE", "id":4615611, "ctx":"initandlisten", "svc": "R", "msg":"MongoDB starting", "attr":{"pid":10111,"port":27001,"dbPath":"/var/lib/mongo","architecture":"64-bit","host":"centos8"}}
{"t":{"$date":"2020-05-18T20:18:12.814+00:00"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten", "svc": "R", "msg":"Build Info", "attr":{"buildInfo":{"version":"4.4.0","gitVersion":"328c35e4b883540675fb4b626c53a08f74e43cf0","openSSLVersion":"OpenSSL 1.1.1c FIPS 28 May 2019","modules":[],"allocator":"tcmalloc","environment":{"distmod":"rhel80","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2020-05-18T20:18:12.814+00:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten", "svc": "R", "msg":"Operating System", "attr":{"os":{"name":"CentOS Linux release 8.0.1905 (Core) ","version":"Kernel 4.18.0-80.11.2.el8_0.x86_64"}}}

在使用 MongoDB 结构化日志时,您可以使用第三方 jq 命令行实用程序轻松美观地打印日志条目,并提供强大的基于键的匹配和过滤功能。

jq 是一个开源 JSON 解析器,用于 Linux、Windows 和 macOS。

可以使用 jq 来美化日志条目的打印,如下所示:

  • 美观打印整个日志文件:

    cat mongod.log | jq
  • 美观打印最新的日志条目:

    cat mongod.log | tail -1 | jq

有关使用 MongoDB 结构化日志的更多示例,请参阅解析结构化日志消息部分。

MongoDB 日志消息可以输出到文件、系统日志或 stdout(标准输出)。

要配置日志输出目的地,可在配置文件或命令行中使用以下设置

配置文件:
命令行:

不指定文件或系统日志会将所有日志输出发送到标准输出。

有关日志记录设置和选项的完整列表,请参阅:

配置文件:
命令行:

注意

发送到 stderr(标准误)的错误消息,例如启动期间不使用文件或系统日志日志目标时发生的致命错误,或与配置错误的日志记录设置有关的消息,不受日志输出目标设置的影响,并且以纯文本格式打印到 stderr。

时间戳字段类型指示记录的事件发生的确切日期和时间。

{
"t": {
"$date": "2020-05-01T15:16:17.180+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 12345,
"ctx": "listener",
"svc": "R",
"msg": "Listening on",
"attr": {
"address": "127.0.0.1"
}
}

When logging to file or to syslog [1], the default format for the timestamp is iso8601-local. To modify the timestamp format, use the --timeStampFormat runtime option or the systemLog.timeStampFormat setting.

请参阅按日期范围过滤,查看过滤时间戳字段的解析示例。

注意

ctime 时间戳格式不再受支持。

[1] 如果记录到系统日志,syslog 守护进程会在记录消息时生成时间戳,而不是在 MongoDB 发布消息时生成时间戳。这可能会导致日志条目的时间戳具有误导性,尤其是当系统负载较重时。

严重性字段类型表示与记录的事件关联的严重性级别。

{
"t": {
"$date": "2020-05-01T15:16:17.180+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 12345,
"ctx": "listener",
"svc": "R",
"msg": "Listening on",
"attr": {
"address": "127.0.0.1"
}
}

严重性级别从“致命”(最严重)到“调试”(最不严重):

等级
说明

F

致命

E

错误

有关错误日志记录的更多信息,请参阅错误代码。

W

警告

I

Informational, for verbosity level 0

D1 - D5

Debug, for verbosity levels > 0

MongoDB indicates the specific debug verbosity level. For example, if verbosity level is 2, MongoDB indicates D2.

在以前的版本中,MongoDB 日志消息为所有调试详细级别指定 D。

You can specify the verbosity level of various components to determine the amount of Informational and Debug messages MongoDB outputs. Severity categories above these levels are always shown. [2] To set verbosity levels, see Configure Log Verbosity Levels.

组件字段类型表示记录的事件所属的类别,例如网络或命令。

{
"t": {
"$date": "2020-05-01T15:16:17.180+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 12345,
"ctx": "listener",
"svc": "R",
"msg": "Listening on",
"attr": {
"address": "127.0.0.1"
}
}

Each component is individually configurable through its own verbosity filter. The available components are as follows:

ACCESS

与访问控制(例如身份验证)相关的消息。要指定 ACCESS 组件的日志级别,请使用 systemLog.component.accessControl.verbosity 设置。

ASSERT

An assertion is triggered when an operation returns an error. The default verbosity level is 0. However, the verbosity setting must be at least 1 in order for operations that return errors to be included in the system logs. To specify the log level for ACCESS components, use the assert setting.

COMMAND

Messages related to database commands, such as count. To specify the log level for COMMAND components, use the systemLog.component.command.verbosity setting.

CONTROL

与控制活动(例如初始化)相关的消息。要指定 CONTROL 组件的日志级别,请使用 systemLog.component.control.verbosity 设置。

ELECTION

Messages related specifically to replica set elections. To specify the log level for ELECTION components, set the systemLog.component.replication.election.verbosity parameter.

REPL is the parent component of ELECTION. If systemLog.component.replication.election.verbosity is unset, MongoDB uses the REPL verbosity level for ELECTION components.

FTDC

与诊断数据收集机制相关的消息,例如服务器统计信息和状态消息。要指定 FTDC 组件的日志级别,请使用 systemLog.component.ftdc.verbosity 设置。

GEO

Messages related to the parsing of geospatial shapes, such as verifying the GeoJSON shapes. To specify the log level for GEO components, set the systemLog.component.geo.verbosity parameter.

INDEX

Messages related to indexing operations, such as creating indexes. To specify the log level for INDEX components, set the systemLog.component.index.verbosity parameter.

INITSYNC

Messages related to initial sync operation. To specify the log level for INITSYNC components, set the systemLog.component.replication.initialSync.verbosity parameter.

REPL is the parent component of INITSYNC. If systemLog.component.replication.initialSync.verbosity is unset, MongoDB uses the REPL verbosity level for INITSYNC components.

JOURNAL

与存储日志活动明确相关的消息。要指定 JOURNAL 组件的日志级别,请使用 systemLog.component.storage.journal.verbosity 设置。

STORAGE is the parent component of JOURNAL. If systemLog.component.storage.journal.verbosity is unset, MongoDB uses the STORAGE verbosity level for JOURNAL components.

NETWORK

Messages related to network activities, such as accepting connections. To specify the log level for NETWORK components, set the systemLog.component.network.verbosity parameter.

QUERY

Messages related to queries, including query planner activities. To specify the log level for QUERY components, set the systemLog.component.query.verbosity parameter.

QUERYSTATS

与$queryStats操作相关的消息。 要指定QUERYSTATS组件的日志级别,请设立systemLog.component.queryStats.verbosity参数。

RECOVERY

与存储恢复活动相关的消息。要指定 RECOVERY 组件的日志级别,请使用 systemLog.component.storage.recovery.verbosity 设置。

STORAGE is the parent component of RECOVERY. If systemLog.component.storage.recovery.verbosity is unset, MongoDB uses the STORAGE verbosity level for RECOVERY components.

REJECTED

8.0版本新增。

与被拒绝的查询操作相关的消息。

要指定 REJECTED 组件消息的日志级别,请设置 systemLog.component.query.rejected.verbosity 参数。

MongoDB 仅在详细程度设置为至少 2 时才记录 REJECTED 组件消息。

The parent component for REJECTED is QUERY.

REPL

Messages related to replica sets, such as initial sync, heartbeats, steady state replication, and rollback. [2] To specify the log level for REPL components, set the systemLog.component.replication.verbosity parameter.

REPL is the parent component of the ELECTION, INITSYNC, REPL_HB, and ROLLBACK components.

REPL_HB

Messages related specifically to replica set heartbeats. To specify the log level for REPL_HB components, set the systemLog.component.replication.heartbeats.verbosity parameter.

REPL is the parent component of REPL_HB. If systemLog.component.replication.heartbeats.verbosity is unset, MongoDB uses the REPL verbosity level for REPL_HB components.

ROLLBACK

Messages related to rollback operations. To specify the log level for ROLLBACK components, set the systemLog.component.replication.rollback.verbosity parameter.

REPL is the parent component of ROLLBACK. If systemLog.component.replication.rollback.verbosity is unset, MongoDB uses the REPL verbosity level for ROLLBACK components.

SHARDING

Messages related to sharding activities, such as the startup of the mongos. To specify the log level for SHARDING components, use the systemLog.component.sharding.verbosity setting.

SLOWPROG

8.3版本新增。

Messages related to slow in-progress queries. To specify the log level for SLOWPROG components, use the systemLog.component.command.slowInProgress.verbosity setting.

STORAGE

Messages related to storage activities, such as processes involved in the fsync command. To specify the log level for STORAGE components, use the systemLog.component.storage.verbosity setting.

STORAGE is the parent component of JOURNAL and RECOVERY.

TXN

Messages related to multi-document transactions. To specify the log level for TXN components, use the systemLog.component.transaction.verbosity setting.

WRITE

Messages related to write operations, such as update commands. To specify the log level for WRITE components, use the systemLog.component.write.verbosity setting.

WT

5.3版本新增。

Messages related to the WiredTiger storage engine. To specify the log level for WT components, use the systemLog.component.storage.wt.verbosity setting.

WTBACKUP

5.3版本新增。

Messages related to backup operations performed by the WiredTiger storage engine. To specify the log level for the WTBACKUP components, use the systemLog.component.storage.wt.wtBackup.verbosity setting.

WTCHKPT

5.3版本新增。

Messages related to checkpoint operations performed by the WiredTiger storage engine. To specify the log level for WTCHKPT components, use the systemLog.component.storage.wt.wtCheckpoint.verbosity setting.

WTCMPCT

5.3版本新增。

Messages related to compaction operations performed by the WiredTiger storage engine. To specify the log level for WTCMPCT components, use the systemLog.component.storage.wt.wtCompact.verbosity setting.

WTEVICT

5.3版本新增。

Messages related to eviction operations performed by the WiredTiger storage engine. To specify the log level for WTEVICT components, use the systemLog.component.storage.wt.wtEviction.verbosity setting.

WTHS

5.3版本新增。

Messages related to the history store of the WiredTiger storage engine. To specify the log level for WTHS components, use the systemLog.component.storage.wt.wtHS.verbosity setting.

WTRECOV

5.3版本新增。

Messages related to recovery operations performed by the WiredTiger storage engine. To specify the log level for WTRECOV components, use the systemLog.component.storage.wt.wtRecovery.verbosity setting.

WTRTS

5.3版本新增。

Messages related to rollback to stable (RTS) operations performed by the WiredTiger storage engine. To specify the log level for WTRTS components, use the systemLog.component.storage.wt.wtRTS.verbosity setting.

WTSLVG

5.3版本新增。

Messages related to salvage operations performed by the WiredTiger storage engine. To specify the log level for WTSLVG components, use the systemLog.component.storage.wt.wtSalvage.verbosity setting.

WTTS

5.3版本新增。

Messages related to timestamps used by the WiredTiger storage engine. To specify the log level for WTTS components, use the systemLog.component.storage.wt.wtTimestamp.verbosity setting.

WTTXN

5.3版本新增。

Messages related to transactions performed by the WiredTiger storage engine. To specify the log level for WTTXN components, use the systemLog.component.storage.wt.wtTransaction.verbosity setting.

WTVRFY

5.3版本新增。

Messages related to verification operations performed by the WiredTiger storage engine. To specify the log level for WTVRFY components, use the systemLog.component.storage.wt.wtVerify.verbosity setting.

WTWRTLOG

5.3版本新增。

Messages related to log write operations performed by the WiredTiger storage engine. To specify the log level for WTWRTLOG components, use the systemLog.component.storage.wt.wtWriteLog.verbosity setting.

-

与命名组件不相关的消息。在 systemLog.verbosity 设置中指定未命名组件的默认日志级别。systemLog.verbosity 设置是已命名和未命名组件的默认设置。

请参阅按组件过滤,了解按照组件字段过滤的日志解析示例。

MongoDB Drivers and client applications (including mongosh) can send identifying information at the time of connection to the server. After the connection is established, the client does not send the identifying information again unless the connection is dropped and reestablished.

该标识信息包含在日志条目的属性字段中。所包含的确切信息因客户端而异。

Below is a sample log message containing the client data document as transmitted from a mongosh connection. The client data is contained in the doc object in the attributes field:

{"t":{"$date":"2020-05-20T16:21:31.561+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn202", "svc": "R", "msg":"client metadata", "attr":{"remote":"127.0.0.1:37106","client":"conn202","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.0"},"os":{"type":"Linux","name":"CentOS Linux release 8.0.1905 (Core) ","architecture":"x86_64","version":"Kernel 4.18.0-80.11.2.el8_0.x86_64"}}}}

当副本集从节点发起与主节点的连接时,从节点会发送类似的数据。包含此启动连接的示例日志消息可能如下所示。客户端数据包含在属性字段的 doc 对象中:

{"t":{"$date":"2020-05-20T16:33:40.595+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn214", "svc": "R", "msg":"client metadata", "attr":{"remote":"127.0.0.1:37176","client":"conn214","doc":{"driver":{"name":"NetworkInterfaceTL","version":"4.4.0"},"os":{"type":"Linux","name":"CentOS Linux release 8.0.1905 (Core) ","architecture":"x86_64","version":"Kernel 4.18.0-80.11.2.el8_0.x86_64"}}}}

See the examples section for a pretty-printed example showing client data.

有关客户端信息和必填字段的完整说明,请参阅MongoDB 握手规范。

您可以指定日志记录详细程度以增加或减少 MongoDB 输出的日志消息量。可以同时调整所有组件的详细程度,也可以单独调整特定命名组件的详细程度。

Verbosity affects log entries in the severity categories Informational and Debug only. Severity categories above these levels are always shown.

You might set verbosity levels to a high value to show detailed logging for debugging or development, or to a low value to minimize writes to the log on a vetted production deployment. [2]

要查看当前的详细级别,请使用 db.getLogComponents() 方法:

db.getLogComponents()

输出可能如下所示:

{
"verbosity" : 0,
"accessControl" : {
"verbosity" : -1
},
"command" : {
"verbosity" : -1
},
...
"storage" : {
"verbosity" : -1,
"recovery" : {
"verbosity" : -1
},
"journal" : {
"verbosity" : -1
}
},
...

The initial verbosity entry is the parent verbosity level for all components, while the individual named components that follow, such as accessControl, indicate the specific verbosity level for that component, overriding the global verbosity level for that particular component if set.

A value of -1, indicates that the component inherits the verbosity level of their parent, if they have one (as with recovery above, inheriting from storage), or the global verbosity level if they do not (as with command). Inheritance relationships for verbosity levels are indicated in the components section.

You can configure the verbosity level using: the systemLog.verbosity and systemLog.component.<name>.verbosity settings, the logComponentVerbosity parameter, or the db.setLogLevel() method. [2]

To configure the default log level for all components, use the systemLog.verbosity setting. To configure the level of specific components, use the systemLog.component.<name>.verbosity settings.

例如,以下配置将 systemLog.verbosity 设置为 1,将 systemLog.component.query.verbosity 设置为 2,将 systemLog.component.storage.verbosity 设置为 2,将 systemLog.component.storage.journal.verbosity 设置为 1:

systemLog:
verbosity: 1
component:
query:
verbosity: 2
storage:
verbosity: 2
journal:
verbosity: 1

您将在配置文件或命令行中为 mongod 或 mongos 实例设置这些值。

所有未在配置中显式指定的组件的详细级别均为 -1,如果有父组件,则表示继承父组件的详细级别;如果没有,则表示继承全局详细级别 (systemLog.verbosity)。

要设置 logComponentVerbosity 参数,请传递一个包含要更改的详细级别设置的文档。

例如,以下将 default verbosity level 设置为 1,将 query 设置为 2,将 storage 设置为 2,将 storage.journal 设置为 1:

db.adminCommand( {
setParameter: 1,
logComponentVerbosity: {
verbosity: 1,
query: {
verbosity: 2
},
storage: {
verbosity: 2,
journal: {
verbosity: 1
}
}
}
} )

You would set these values from mongosh.

使用 db.setLogLevel() 方法更新单个组件的日志级别。对于组件,您可以指定 0 到 5 的详细级别,也可以指定 -1 以继承父组件的详细级别。例如,以下内容将 systemLog.component.query.verbosity 设置为其父级详细级别(即 默认详细级别):

db.setLogLevel(-1, "query")

You would set this value from mongosh.

[2](1, 2, 3, 4)

现在,副本集的从节点会记录应用时间超过慢操作阈值的 oplog 条目。这些慢 oplog 消息:

  • 在diagnostic log中针对从节点记录。

  • Are logged under the REPL component with the text applied op: <oplog entry> took <num>ms.

  • 不依赖日志级别(系统级别或组件级别)

  • 不依赖于分析级别。

  • 受 slowOpSampleRate 影响。

分析器不会捕获慢 oplog 条目。

Client operations (such as queries) appear in the log if their duration exceeds the slow operation threshold or when the log verbosity level is at least 1. [2] These log entries include the full command object associated with the operation.

用于读/写操作的分析器条目和诊断日志消息(即 mongod/mongos 日志消息)包括:

  • planCacheShapeHash 帮助识别具有相同计划缓存查询结构的慢查询。

    从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

  • planCacheKey 更深入地了解慢查询的查询计划缓存。

重要

单个操作可能会记录多个条目。例如,如果批量写入操作中的多个写入超过慢操作阈值,则每个缓慢写入都会被单独记录。

下表列出了慢查询日志记录的更改。

MongoDB 版本
更改

6.1

Slow operation log messages include cache refresh time fields.

6.2

慢操作日志消息包含一个 queryFramework 字段,它可用于指示哪个查询引擎执行了该查询:

  • queryFramework: "classic" 表示经典引擎完成该查询。

  • queryFramework: "sbe" 表示基于槽的查询执行引擎执行该查询。

6.3

慢操作日志消息和数据库分析器条目包含 cpuNanos 字段,而它指定了查询操作所花费的总 CPU 时间(以纳秒为单位)。cpuNanos 字段仅在 Linux 系统上可用。

7.0(和6.0.13,5.0.24)

慢查询日志消息中的 totalOplogSlotDurationMicros 显示了写操作获取提交时间戳以提交存储引擎写入内容与实际提交之间的时间。mongod 支持并行写入。但是,它可按任意顺序提交附带提交时间戳的写入操作。

例如,考虑以下带有提交时间戳的写入操作:

  • writeA with Timestamp1

  • writeB with Timestamp2

  • writeC with Timestamp3

假设 WriteB 首先在 Timestamp2 提交。复制暂停到 writeA 提交,因为需要 writeA 的带 Timestamp1 的 oplog 条目才能将 oplog 复制到副本集从节点。

8.0

慢查询输出包括一个 queues 文档,其中包含有关此操作的队列的信息。queues 字段中的每个队列均包含一个 totalTimeQueuedMicros 字段,其中包含此操作在相应队列中花费的总累积时间(以微秒为单位)。

可用时,查询结构的 queryShapeHash 字段也会包含在慢查询日志中。

如果具有特定查询结构的命令被拒绝,MongoDB 会记录一条日志,说明该查询命令已被拒绝。该消息包含查询命名空间 queryShapeHash 和带有被拒绝查询的命令。仅当日志详细程度级别设置为 2 或更高时,MongoDB 才会记录该消息。

8.1

如果查询执行将临时文件写入磁盘,则慢查询日志消息会包含新指标。这些指标以导致查询超过内存限制的查询执行阶段为前缀。示例,sortSpills 表示查询执行的排序阶段将临时文件写入磁盘的次数。

  • <executionPart>Spills 表示相应查询执行阶段将临时文件写入磁盘的次数。

  • <executionPart>SpilledBytes 表示将临时文件写入磁盘所释放的内存大小(以字节为单位)。

  • <executionPart>SpilledDataStorageSize 表示用于临时文件的磁盘空间大小(以字节为单位)。

  • <executionPart>SpilledRecords 表示写入磁盘临时文件的记录数。

有关将临时文件写入磁盘的更多信息,请参阅 allowDiskUse()。

8.3

慢查询日志条目有一个可选的 originalQueryShapeHash字段,其中包含源自 mongos 的以下操作的查询结构:

在 mongos 将操作发送到 mongod 后,mongos 操作的原始查询结构可能会有所不同。

MongoDB 8.3 还引入了进行中慢速查询日志条目。查询在执行期间触发慢速日志。这些日志包含的信息少于标准慢查询日志。如果查询的持续时间超过operationProfiling.slowOpInProgressThresholdMs,MongoDB最多记录一次查询。

当操作指定查询时间限制时,慢查询日志包含 deadline字段。deadline 表示操作必须完成的时间,等于操作开始的时间加上查询超时值。

9.0

当查询结构通过 setQuerySettings 或 setParameter">MongoDB Atlas 设置了查询旋钮时,慢查询日志条目将包含 queryKnobs 文档。文档显示每个配置项的有效值以及该值的来源,其形状与 explain 输出相同。有关详情,请参阅 查询控制键。

For a pretty-printed example of a slow operation log entry, see Log Message Examples.

版本 5.0 中的新增功能。

从 MongoDB 5.0 开始,可以使用 remoteOpWaitMillis 日志字段来获取分片结果的等待时间(以毫秒为单位)。

remoteOpWaitMillis 仅记录:

要确定合并操作或分片问题是否导致查询缓慢,请比较日志中的 workingMillis 和 remoteOpWaitMillis 时间字段。workingMillis 是查询完成所需的总时间。具体而言:

  • 如果 workingMillis 略长于 remoteOpWaitMillis,则等待分片响应花费的时间最长。例如,17 中的 workingMillis 和 15 中的 remoteOpWaitMillis。

  • 如果 workingMillis 明显长于 remoteOpWaitMillis,则执行合并花费的时间最长。例如,100 中的 workingMillis 和 15 中的 remoteOpWaitMillis。

使用 Queryable Encryption 时,慢查询日志中会省略针对已加密集合的增删改查操作。有关详细信息,请参阅 Queryable Encryption 日志校订。

仅适用于MongoDB Atlas和MongoDB Enterprise

使用 redactClientLogData 运行的 mongod 或 mongos 会在记录之前编辑与校订给定日志事件附带的任何消息,只留下与该事件相关的元数据、源文件或行号。redactClientLogData 以诊断细节为代价,防止潜在的敏感信息进入系统日志。

For example, the following operation inserts a document into a mongod running without log redaction. The mongod has the log verbosity level set to 1:

db.clients.insertOne( { "name" : "Joe", "PII" : "Sensitive Information" } )

此操作会产生以下日志事件:

{
"t": { "$date": "2024-07-19T15:36:55.024-07:00" },
"s": "I",
"c": "COMMAND",
...
"attr": {
"type": "command",
...
"appName": "mongosh 2.2.10",
"command": {
"insert": "clients",
"documents": [
{
"name": "Joe",
"PII": "Sensitive Information",
"_id": { "$oid": "669aea8792c7fd822d3e1d8c" }
}
],
"ordered": true,
...
}
...
}
}

当 mongod 结合 redactClientLogData 运行并执行相同的插入操作时,会产生以下日志事件:

{
"t": { "$date": "2024-07-19T15:36:55.024-07:00" },
"s": "I",
"c": "COMMAND",
...
"attr": {
"type": "command",
...
"appName": "mongosh 2.2.10",
"command": {
"insert": "###",
"documents": [
{
"name": "###",
"PII": "###",
"_id": "###"
}
],
"ordered": "###",
...
}
...
}
}

将 redactClientLogData 与静态加密和 TLS/SSL(传输加密)结合使用,以帮助遵守监管要求。

日志解析是以编程方式搜索和分析日志文件的行为,通常自动进行。随着引入结构化日志记录,日志解析变得更简单、更强大。例如:

  • 日志消息字段以键值对的形式显示。日志解析器可以按照感兴趣的特定键查询,以有效地筛选结果。

  • 日志消息始终包含相同的消息结构。日志解析器能可靠地从任何日志消息中提取信息,无需针对信息丢失或格式不同的情况进行编码。

以下示例展示使用 MongoDB JSON 日志输出时的常见日志解析工作流程。

在使用 MongoDB 结构化日志时,您可以使用第三方 jq 命令行实用程序轻松美观地打印日志条目,并提供强大的基于键的匹配和过滤功能。

jq 是一个开源 JSON 解析器,用于 Linux、Windows 和 macOS。

这些示例使用 jq 简化日志解析。

以下示例按频率排序显示给定日志文件中的前 10 个唯一消息值:

jq -r ".msg" /var/log/mongodb/mongod.log | sort | uniq -c | sort -rn | head -10

远程客户端连接显示在属性对象中 "remote" 键下的日志中。以下内容对日志文件过程中的所有唯一连接进行计数,并按出现次数降序排列这些连接:

jq -r '.attr.remote' /var/log/mongodb/mongod.log | grep -v 'null' | sort | uniq -c | sort -r

请注意,该命令将来自同一 IP 地址但通过不同端口的连接视为不同的连接。您可以将输出限制为仅考虑 IP 地址,并进行以下更改:

jq -r '.attr.remote' /var/log/mongodb/mongod.log | grep -v 'null' | awk -F':' '{print $1}' | sort | uniq -c | sort -r

The following example counts all remote MongoDB driver connections, and presents each driver type and version in descending order by number of occurrences:

jq -cr '.attr.doc.driver' /var/log/mongodb/mongod.log | grep -v null | sort | uniq -c | sort -rn

The following example analyzes the reported client data of remote MongoDB driver connections and client applications, including mongosh, and prints a total for each unique operating system type that connected, sorted by frequency:

jq -r '.attr.doc.os.type' /var/log/mongodb/mongod.log | grep -v null | sort | uniq -c | sort -rn

此日志字段中报告的字符串 "Darwin" 代表 macOS 客户端。

With slow operation logging enabled, the following returns only the slow operations that took above 2000 milliseconds:, for further analysis:

jq 'select(.attr.workingMillis>=2000)' /var/log/mongodb/mongod.log

查阅 jq 文档,了解本示例所示 jq 过滤器的更多信息。

Log components (the third field in the JSON log output format) indicate the general category a given log message falls under. Filtering by component is often a great starting place when parsing log messages for relevant events.

以下示例仅打印组件类型 REPL 的日志消息:

jq 'select(.c=="REPL")' /var/log/mongodb/mongod.log

以下示例打印除组件类型 REPL 之外的所有日志消息:

jq 'select(.c!="REPL")' /var/log/mongodb/mongod.log

The following example print log messages of component type REPL or STORAGE:

jq 'select( .c as $c | ["REPL", "STORAGE"] | index($c) )' /var/log/mongodb/mongod.log

查阅 jq 文档,了解本示例所示 jq 过滤器的更多信息。

Log IDs (the fifth field in the JSON log output format) map to specific log events, and can be relied upon to remain stable over successive MongoDB releases.

例如,以下两个日志事件显示客户端连接和之后的断开连接:

{"t":{"$date":"2020-06-01T13:06:59.027-0500"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener", "svc": "R", "msg":"connection accepted from {session_remote} #{session_id} ({connectionCount}{word} now open)", "attr":{"session_remote":"127.0.0.1:61298", "session_id":164,"connectionCount":11,"word":" connections"}}
{"t":{"$date":"2020-06-01T13:07:03.490-0500"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn157", "svc": "R", "msg":"end connection {remote} ({connectionCount}{word} now open)", "attr":{"remote":"127.0.0.1:61298","connectionCount":10,"word":" connections"}}

这两个条目的日志 ID 分别是 22943 和 22944。然后,您可以使用以下 jq 语法筛选日志输出以仅显示这些日志 ID,从而有效地仅显示客户端连接活动:

jq 'select( .id as $id | [22943, 22944] | index($id) )' /var/log/mongodb/mongod.log

查阅 jq 文档,了解本示例所示 jq 过滤器的更多信息。

可以通过筛选时间戳字段来进一步细化日志输出,将返回的日志条目限制为特定日期范围。例如,以下命令返回 2020 年 4 月 15 日发生的所有日志条目:

jq 'select(.t["$date"] >= "2020-04-15T00:00:00.000" and .t["$date"] <= "2020-04-15T23:59:59.999")' /var/log/mongodb/mongod.log

此语法包含完整时间戳(包括毫秒但不包括时差)。

按日期范围筛选可以与上述任何示例结合,例如创建每周报告或年度摘要。以下语法扩展了之前的“监控连接”示例,将结果限制为 2020 年 5 月:

jq 'select(.t["$date"] >= "2020-05-01T00:00:00.000" and .t["$date"] <= "2020-05-31T23:59:59.999" and .attr.remote)' /var/log/mongodb/mongod.log

查阅 jq 文档,了解本示例所示 jq 过滤器的更多信息。

日志摄取服务是一种第三方产品,摄取和汇总日志文件(通常来自分布式系统集群),并在中央位置对这些数据进行持续分析。

The JSON log format allows for more flexibility when working with log ingestion and analysis services. Whereas plaintext logs generally require some manner of transformation before being eligible for use with these products, JSON files can often be consumed out of the box, depending on the service. Further, JSON-formatted logs offer more control when performing filtering for these services, as the key-value structure offers the ability to specifically import only the fields of interest, while omitting the rest.

有关更多信息,请参阅所选第三方日志摄取服务的文档。

以下示例以 JSON 输出格式显示日志消息。

为方便起见,这些日志信息以美观打印格式呈现。

此示例显示启动警告:

{
"t": {
"$date": "2020-05-20T19:17:06.188+00:00"
},
"s": "W",
"c": "CONTROL",
"id": 22120,
"ctx": "initandlisten",
"svc": "R",
"msg": "Access control is not enabled for the database. Read and write access to data and configuration is unrestricted",
"tags": [
"startupWarnings"
]
}

This example shows a client connection that includes client data:

{
"t": {
"$date": "2020-05-20T19:18:40.604+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 51800,
"ctx": "conn281",
"svc": "R",
"msg": "client metadata",
"attr": {
"remote": "192.168.14.15:37666",
"client": "conn281",
"doc": {
"application": {
"name": "MongoDB Shell"
},
"driver": {
"name": "MongoDB Internal Client",
"version": "4.4.0"
},
"os": {
"type": "Linux",
"name": "CentOS Linux release 8.0.1905 (Core) ",
"architecture": "x86_64",
"version": "Kernel 4.18.0-80.11.2.el8_0.x86_64"
}
}
}
}

从 MongoDB 8.0 开始,慢速操作的记录基于 MongoDB 在该操作上花费的时间,而不是操作的总延迟。

您可以使用慢速操作日志中的指标来确定操作在其生命周期中花费的时间,这有助于确定可能的性能改进。

在以下示例日志消息中:

  • 这些指标显示了执行查询时等待资源所花费的时间:

    • queues.execution.totalTimeQueuedMicros

    • timeAcquiringMicros

  • workingMillis

    MongoDB在操作上花费的时间量。

  • durationMillis

    操作的总延迟。

  • inUseTrackedMemBytes

    8.3版本新增。

    当前查询操作使用的跟踪内存的字节数。

  • peakTrackedMemBytes

    8.3版本新增。

    当前查询操作使用的跟踪内存的最大字节数。

{
"t":{
"$date":"2024-06-01T13:24:10.034+00:00"
},
"s":"I",
"c":"COMMAND",
"id":51803,
"ctx":"conn3",
"msg":"Slow query",
"attr":{
"type":"command",
"isFromUserConnection":true,
"ns":"db.coll",
"collectionType":"normal",
"appName":"MongoDB Shell",
"command":{
"find":"coll",
"filter":{
"b":-1
},
"sort":{
"splitPoint":1
},
"readConcern":{ },
"$db":"db"
},
"planSummary":"COLLSCAN",
"planningTimeMicros":87,
"keysExamined":0,
"docsExamined":20889,
"hasSortStage":true,
"nBatches":1,
"cursorExhausted":true,
"numYields":164,
"nreturned":99,
"inUseTrackedMemBytes":368,
"peakTrackedMemBytes":795,
"planCacheShapeHash":"9C05019A",
"planCacheKey":"C41063D6",
"queryFramework":"classic",
"reslen":96,
"locks":{
"ReplicationStateTransition":{
"acquireCount":{
"w":3
}
},
"Global":{
"acquireCount":{
"r":327,
"w":1
}
},
"Database":{
"acquireCount":{
"r":1
},
"acquireWaitCount":{
"r":1
},
"timeAcquiringMicros":{
"r":2814
}
},
"Collection":{
"acquireCount":{
"w":1
}
}
},
"flowControl":{
"acquireCount":1,
"acquireWaitCount":1,
"timeAcquiringMicros":8387
},
"readConcern":{
"level":"local",
"provenance":"implicitDefault"
},
"storage":{ },
"cpuNanos":20987385,
"remote":"127.0.0.1:47150",
"protocol":"op_msg",
"queues":{
"ingress":{
"admissions":7,
"totalTimeQueuedMicros":0
},
"execution":{
"admissions":328,
"totalTimeQueuedMicros":2109
}
},
"workingMillis":89,
"durationMillis":101
}
}

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

This example demonstrates character escaping, as shown in the setName field of the attribute object:

{
"t": {
"$date": "2020-05-20T19:11:09.268+00:00"
},
"s": "I",
"c": "REPL",
"id": 21752,
"ctx": "ReplCoord-0",
"svc": "R",
"msg": "Scheduling remote command request",
"attr": {
"context": "vote request",
"request": "RemoteCommand 229 -- target:localhost:27003 db:admin cmd:{ replSetRequestVotes: 1, setName: \"my-replica-name\", dryRun: true, term: 3, candidateIndex: 0, configVersion: 2, configTerm: 3, lastAppliedOpTime: { ts: Timestamp(1589915409, 1), t: 3 } }"
}
}

Starting in MongoDB 5.0, log messages for slow queries on views include a resolvedViews field that contains the view details:

"resolvedViews": [ {
"viewNamespace": <String>, // namespace and view name
"dependencyChain": <Array of strings>, // view name and collection
"resolvedPipeline": <Array of documents> // aggregation pipeline for view
} ]

以下示例使用 test 数据库并创建名为 myView 的视图,该视图按 firstName 字段对 myCollection 中的文档排序:

use test
db.createView( "myView", "myCollection", [ { $sort: { "firstName" : 1 } } ] )

Assume a slow query is run on myView. The following example log message contains a resolvedViews field for myView:

{
"t": {
"$date": "2021-09-30T17:53:54.646+00:00"
},
"s": "I",
"c": "COMMAND",
"id": 51803,
"ctx": "conn249",
"svc": "R",
"msg": "Slow query",
"attr": {
"type": "command",
"ns": "test.myView",
"appName": "MongoDB Shell",
"command": {
"find": "myView",
"filter": {},
"lsid": {
"id": { "$uuid": "ad176471-60e5-4e82-b977-156a9970d30f" }
},
"$db": "test"
},
"planSummary":"COLLSCAN",
"resolvedViews": [ {
"viewNamespace": "test.myView",
"dependencyChain": [ "myView", "myCollection" ],
"resolvedPipeline": [ { "$sort": { "firstName": 1 } } ]
} ],
"keysExamined": 0,
"docsExamined": 1,
"hasSortStage": true,
"cursorExhausted": true,
"numYields": 0,
"nreturned": 1,
"planCacheShapeHash": "3344645B",
"planCacheKey": "1D3DE690",
"queryFramework": "classic"
"reslen": 134,
"locks": { "ParallelBatchWriterMode": { "acquireCount": { "r": 1 } },
"ReplicationStateTransition": { "acquireCount": { "w": 1 } },
"Global": { "acquireCount": { "r": 4 } },
"Database": { "acquireCount": {"r": 1 } },
"Collection": { "acquireCount": { "r": 1 } },
"Mutex": { "acquireCount": { "r": 4 } } },
"storage": {},
"remote": "127.0.0.1:34868",
"protocol": "op_msg",
"workingMillis": 0,
"durationMillis": 0
}
}
}

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

Starting in MongoDB 5.0, log messages for slow queries include a system.profile.authorization section. These metrics help determine if a request is delayed because of contention for the user authorization cache.

"authorization": {
"startedUserCacheAcquisitionAttempts": 1,
"completedUserCacheAcquisitionAttempts": 1,
"userCacheWaitTimeMicros": 508
},

从 MongoDB 6.3 开始,如果发生以下情况,日志中就会添加一条信息:发送操作响应的时间超过 slowms 阈值选项。

该消息称为会话工作流日志消息,包含在数据库会话中执行操作的各种时间。

会话工作流日志消息示例:

{
"t": {
"$date": "2022-12-14T17:22:44.233+00:00"
},
"s": "I",
"c": "EXECUTOR",
"id": 6983000,
"ctx": "conn1",
"svc": "R",
"msg": "Slow network response send time",
"attr": {
"elapsed": {
"totalMillis": 109,
"activeMillis": 30,
"receiveWorkMillis": 2,
"processWorkMillis": 10,
"sendResponseMillis": 22,
"yieldMillis": 15,
"finalizeMillis": 30
}
}
}

时间以毫秒为单位。

如果 sendResponseMillis 超过 slowms 阈值选项,则会向日志添加一条会话工作流消息。

字段
说明

totalMillis

会话中执行操作的总时间,包括等待接收信息的时间。

activeMillis

从接收消息到完成与该消息关联的操作之间的时间。这个时间包括发送响应和执行任何清理。

receivedWorkMillis

通过网络接收操作信息的时间。

processWorkMillis

处理操作和创建响应的时间。

sendResponseMillis

发送响应的时间。

yieldMillis

释放工作线程和再次使用该线程之间的时间。

finalize

结束和关闭会话工作流的时间。

从 MongoDB 6.3 开始,如果一个操作从获取服务器连接到通过网络向服务器发送字节的等待时间超过 1 毫秒,则日志中就会添加一条消息。

默认情况下,消息在 "I" 信息级别记录,最多每秒一次,以避免日志消息过多。如果必须获取每条日志消息,请将日志级别更改为调试。

如果操作等待时间超过 1 毫秒,并且上一秒内将消息记录在信息级别,则将在调试级别记录下一条消息。否则,将在信息级别记录下一条消息。

示例日志消息:

{
"t": {
"$date":"2023-01-31T15:22:29.473+00:00"
},
"s": "I",
"c": "NETWORK",
"id": 6496702,
"ctx": "ReplicaSetMonitor-TaskExecutor",
"svc": "R",
"msg": "Acquired connection for remote operation and completed writing to wire",
"attr": {
"durationMicros": 1683
}
}

下表描述 attr 中的 durationMicros 字段。

字段
说明

durationMicros

操作在获取服务器连接和写入字节以通过网络发送到服务器之间等待的总时间(以微秒为单位)。

注意

缓存刷新日志字段特定于分片集群,并且仅出现在 mongos 路由器生成的日志中。它们在未分片的副本集或独立部署中不可用。

Starting in MongoDB 6.1, log messages for slow queries include the following cache refresh time fields:

字段
说明

catalogCacheDatabaseLookupDurationMillis

从数据库数据库的缓存中检索元数据元数据的时间(以毫秒为单位)。

catalogCacheCollectionLookupDurationMillis

从集合元数据的缓存中检索元数据的时间(以毫秒为单位)。

databaseVersionRefreshDurationMillis

为 movePrimary、createDatabase 和 dropDatabase 等操作刷新数据库元数据的时间(以毫秒为单位)。

shardVersionRefreshMillis

指定刷新缓存分片版本操作缓存的频率的时间(以毫秒为单位)。

Starting in MongoDB 7.0, log messages for slow queries also include the catalogCacheIndexLookupDurationMillis field that indicates the time that the operation spent fetching information from the index cache. This release also renames the shardVersionRefreshMillis field to placementVersionRefreshDurationMillis.

placementVersionRefreshDurationMillis 是为以下操作刷新缓存的时间:

以下示例包括:

  • catalogCacheDatabaseLookupDurationMillis

  • catalogCacheCollectionLookupDurationMillis

  • catalogCacheIndexLookupDurationMillis

{
"t": {
"$date": "2023-03-17T09:47:55.929+00:00"
},
"s": "I",
"c": "COMMAND",
"id": 51803,
"ctx": "conn14",
"svc": "R",
"msg": "Slow query",
"attr": {
"type": "command",
"ns": "db.coll",
"appName": "MongoDB Shell",
"command": {
"insert": "coll",
"ordered": true,
"lsid": {
"id": {
"$uuid": "5d50b19c-8559-420a-a122-8834e012274a"
}
},
"$clusterTime": {
"clusterTime": {
"$timestamp": {
"t": 1679046398,
"i": 8
}
},
"signature": {
"hash": {
"$binary": {
"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"subType": "0"
}
},
"keyId": 0
}
},
"$db": "db"
},
"catalogCacheDatabaseLookupDurationMillis": 19,
"catalogCacheCollectionLookupDurationMillis": 68,
"catalogCacheIndexLookupDurationMillis": 16026,
"nShards": 1,
"ninserted": 1,
"numYields": 232,
"reslen": 96,
"readConcern": {
"level": "local",
"provenance": "implicitDefault",
},
"cpuNanos": 29640339,
"remote": "127.0.0.1:48510",
"protocol": "op_msg",
"remoteOpWaitMillis": 4078,
"workingMillis": 20334,
"durationMillis": 20334
}
}

从 MongoDB 9.0 开始,如果查询操作超过了单次操作内存限制且无法溢出到磁盘,MongoDB 将终止该操作并返回 ExceededMemoryLimit 错误。MongoDB 还会写入以下日志消息:

{
"t": {
"$date": "2026-09-02T15:16:17.180+00:00"
},
"s": "E",
"c": "QUERY",
"id": 12932700,
"ctx": "conn3",
"svc": "R",
"msg": "Query exceeded the memory limit",
"attr": {
"error": "expression evaluation needs too much memory. Needs: 1140850688 bytes. Local memory limit: 1073741824 bytes. Global memory used: 1140850688 bytes. Global memory limit: 1073741824 bytes."
}
}

error 属性是一个字符串,其中包含内存超额的详细信息。详细信息包括超出限制的操作、正在使用的内存量以及适用的内存限制。MongoDB 还会将此 string 返回给 ExceededMemoryLimit 错误中的客户端。

仅当操作超过单次操作内存限制且无法溢出到磁盘时,MongoDB 才会写入此日志消息。如果一个可以溢出到磁盘的阶段超过了限制,并且 allowDiskUse 为 false,MongoDB 将以 QueryExceededMemoryLimitNoDiskUseAllowed 错误结束操作,并且不会写入此日志消息。

在 Linux 系统中,消息遵循 Linux 配置文件 /etc/systemd/journald.conf 中定义的规则。默认情况下,日志消息突发量限制为 30 秒内 1000 条消息。要查看更多消息,请提高 /etc/systemd/journald.conf 中的 RateLimitBurst参数。

您可以使用 MongoDB Atlas 下载压缩文件,其中包含数据库部署中选定主机名或进程的日志。 要了解更多信息,请参阅查看和下载 MongoDB 日志。