mongodb中serverStatus()返回参数理解

本文详细解析MongoDB的serverStatus命令,包括其返回的参数含义,如内存使用、网络通信、数据库操作计数等,帮助理解MongoDB实例的运行状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是个人的理解,由于自身能力有限,也许会存在误解,欢迎留言指正,本人一定虚心请教,谢谢

serverStatus的输出取决于MongoDB的版本,操作系统,存储引擎,和节点类型(mongosmongod、副本集成员)。

如果要显示某个属性就设置值为1,否则设置为0

比如下面的命令就不会显示replmetricslocks的信息
db.runCommand( { serverStatus: 1, repl: 0, metrics: 0, locks: 0 } )
下面的命令就在输出中包括rangeDeleterrepl的所有信息
db.runCommand( { serverStatus: 1, rangeDeleter: 1, repl: 1 } )
mongos> db.serverStatus()db.serverStatus()
{
        "host" : "gs2:10",#系统的主机名
        "version" : "3.4.7",#mongodb版本
        "process" : "mongos",#当前mongodb的进程,可能的是:mongos或者mongod.
        "pid" : NumberLong(17124),#进程ID
        "uptime" : 1365907,#当前mongodb进程已经活跃的秒数
        "uptimeMillis" : NumberLong(1365907544),#当前mongodb进程已经活跃的毫秒数
        "uptimeEstimate" : NumberLong(1365907),#mongodb内部系统时间计算的正常工作时间,以秒为单位
        "localTime" : ISODate("2017-10-25T07:28:39.604Z"),#系统的当前时间
    #asserts返回的是一个文档,报告自MongoDB进程开始后所产生的assertions数量。而assert errors通常是不常见的,
    # 如果有非零值,你应该检查日志文件来查看更多信息。
        "asserts" : {
                "regular" : 0,#mongodb进程启动后产生的常规ssertions数量,查看日志文件以获取更过信息
                "warning" : 0,#mongodb进程启动后产生的警告数量
                "msg" : 0,#消息数目
                "user" : 90612,
                "rollovers" : 0
        },
    #connections返回的是文档连接状态。使用这些值可以评估当前负载和服务器的容量需求。
        "connections" : {
                "current" : 13,#从客户到数据库服务器的连接,包括当前会话
                "available" : 819187,#可用的连接数
                "totalCreated" : 884#创建过的所有连接,包括已经关闭过了的
        },
    #extra_info返回的是系统的其他信息
        "extra_info" : {
                "note" : "fields vary by platform",
                "page_faults" : 185#错误页面的总数
        },
        "network" : {#网络
                "bytesIn" : NumberLong("2905159959"),#数据库网络通信所收到的字节数
                "bytesOut" : NumberLong("3836423563"),#数据库网络通信所发送的字节数
                "physicalBytesIn" : NumberLong("2905159959"),
                "physicalBytesOut" : NumberLong("3702692307"),
                "numRequests" : NumberLong(2350394)#服务器收到的不同的请求数目
        },
        "opcounters" : {#上一次启动后的数据库操作情况
                "insert" : 14846519,# mongod 开始后的插入次数
                "query" : 170,#查询次数
                "update" : 0,#更新次数
                "delete" : 0,#删除次数
                "getmore" : 159,#getmore的次数
                "command" : 1174856#发布到数据库的总命令数
        },
        "sharding" : {
                "configsvrConnectionString" : "cfgSet/11.18.9.12:111,1.18.9.15:110,17.10.9.14:11",
                "lastSeenConfigServerOpTime" : {
                        "ts" : Timestamp(1508916518, 1),
                        "t" : NumberLong(3)
                }
        },
        "tcmalloc" : {
                "generic" : {
                        "current_allocated_bytes" : 2933168,
                        "heap_size" : 90456064
                },
                "tcmalloc" : {
                        "pageheap_free_bytes" : 77418496,
                        "pageheap_unmapped_bytes" : 344064,
                        "max_total_thread_cache_bytes" : 1024458752,
                        "current_total_thread_cache_bytes" : 2205720,
                        "total_free_bytes" : 9760336,
                        "central_cache_free_bytes" : 2272152,
                        "transfer_cache_free_bytes" : 5282464,
                        "thread_cache_free_bytes" : 2205720,
                        "aggressive_memory_decommit" : 0,
                        "formattedString" : "-----------
                                            MALLOC:        2933168 (    2.8 MiB) Bytes in use by application
                                            MALLOC: +     77418496 (   73.8 MiB) Bytes in page heap freelist
                                            MALLOC: +      2272152 (    2.2 MiB) Bytes in central cache freelist
                                            MALLOC: +      5282464 (    5.0 MiB) Bytes in transfer cache freelist
                                            MALLOC: +      2205720 (    2.1 MiB) Bytes in thread cache freelists
                                            MALLOC: +      1335488 (    1.3 MiB) Bytes in malloc metadata
                                            MALLOC:   ------------
                                            MALLOC: =     91447488 (   87.2 MiB) Actual memory used (physical + swap)
                                            MALLOC: +       344064 (    0.3 MiB) Bytes released to OS (aka unmapped)
                                            MALLOC:   ------------"
                                            MALLOC: =     91791552 (   87.5 MiB) Virtual address space used
                                            MALLOC:"
                                            MALLOC:           1398              Spans in use
                                            MALLOC:             34              Thread heaps in use
                                            MALLOC:           4096              Tcmalloc page size
                                            ------------------------------------------------
                                            Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()).
                                            Bytes released to the OS take up virtual address space but no physical memory."
                }
        },
        "mem" : {#mongod系统架构和当前内存使用情况
                "bits" : 64,#MongoDB的位数:6432
                "resident" : 91,#大致等于当前进程所用的RAM的值,单位MB,在专用的数据库中这个值接近系统内存
                "virtual" : 351,#mongod使用了的虚拟内存,单位MB                "supported" : true#指示底层系统是否支持扩展内存信息。False系统不支持扩展内存信息,则其他mem values不得访问数据库服务器
        },
        "metrics" : {#返回正在运行的mongod实例的状态等统计信息
                "cursor" : {
                        "timedOut" : NumberLong(1),
                        "open" : {
                                "multiTarget" : NumberLong(0),
                                "singleTarget" : NumberLong(0),
                                "pinned" : NumberLong(0),
                                "total" : NumberLong(0)
                        }
                },
                "commands" : {#数据库执行的命令
                        "buildInfo" : {
                                "failed" : NumberLong(0),#命令执行失败的次数
                                "total" : NumberLong(565)#mongod执行命令的总次数
                        },
                        "count" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(2)
                        },
                        "find" : {
                                "failed" : NumberLong(1),
                                "total" : NumberLong(170)
                        },
                        "getLastError" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(556)
                        },
                        "getLog" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(3)
                        },
                        "getMore" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(159)
                        },
                        "insert" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(304434)
                        },
                        "isMaster" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(867124)
                        },
                        "listCollections" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(2)
                        },
                        "listDatabases" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(5)
                        },
                        "replSetGetStatus" : {
                                "failed" : NumberLong(32),
                                "total" : NumberLong(32)
                        },
                        "saslContinue" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(1414)
                        },
                        "saslStart" : {
                                "failed" : NumberLong(4),
                                "total" : NumberLong(714)
                        },
                        "serverStatus" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(2)
                        },
                        "whatsmyuri" : {
                                "failed" : NumberLong(0),
                                "total" : NumberLong(3)
                        }
                }
        },
        "ok" : 1
}
参考地址:https://docs.mongodb.com/manual/reference/command/serverStatus/
TASK [mongodb : required gpg key url | mongodb] ***************************************************************************************************************************************************************************************** task path: /workspaces/instances/roles/mongodb/tasks/required-gpg-key.yml:13 Tuesday 18 March 2025 10:27:44 +0000 (0:00:00.017) 0:00:21.480 ********* Using module file /opt/pipx/venvs/ansible-core/lib/python3.11/site-packages/ansible/modules/get_url.py Pipelining is enabled. <hoya-cex-host-vpc-pritunl-2> ESTABLISH SSH CONNECTION FOR USER: ops <hoya-cex-host-vpc-pritunl-2> SSH: EXEC /workspaces/instances/scripts/gcp-ssh-wrapper.sh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=22 -o &#39;IdentityFile="/workspaces/instances/instances/hoya-cex-host-vpc-pritunl-2/.terraform/.ssh/id_rsa"&#39; -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o &#39;User="ops"&#39; -o ConnectTimeout=10 -o ControlMaster=auto -o ControlPersist=300s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --project=hoya-cex-host-vpc --zone=asia-east2-c --user=ops --ssh-key-file=/workspaces/instances/instances/hoya-cex-host-vpc-pritunl-2/.terraform/.ssh/id_rsa -o &#39;ControlPath="/home/vscode/.ansible/cp/%h-%r"&#39; hoya-cex-host-vpc-pritunl-2 &#39;/bin/sh -c &#39;"&#39;"&#39;sudo -H -S -n -u root /bin/sh -c &#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39;echo BECOME-SUCCESS-ozbzltumssytcgiorrwusdiptwpgmvmm ; /usr/bin/python3.12&#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39;"&#39; && sleep 0&#39;"&#39;"&#39;&#39; Escalation succeeded <hoya-cex-host-vpc-pritunl-2> (0, b&#39;\n{"url": "https://www.mongodb.org/static/pgp/server-8.0.asc", "dest": "/usr/share/keyrings/mongodb-server-8.0.gpg", "changed": false, "msg": "HTTP Error 304: Not Modified", "status_code": 304, "elapsed": 1, "uid": 0, "gid": 0, "owner": "root", "group": "root", "mode": "0644", "state": "file", "size": 1676, "invocation": {"module_args": {"url": "https://www.mongodb.org/static
03-21
<think>好的,我现在要解决用户提到的在Ansible中安装MongoDB GPG密钥时遇到的HTTP 304错误。首先,我需要了解HTTP 304状态码的含义。HTTP 304表示“未修改”,通常发生在客户端发送了一个条件性请求(比如使用If-Modified-Since或If-None-Match头),而服务器发现资源自该时间后没有修改过,因此返回304而是重新下载资源。这种情况在使用缓存机制时很常见。 接下来,我需要检查用户使用的Ansible任务。用户可能使用了get_url或apt_key模块来下载GPG密钥。例如,类似这样的代码: ```yaml - name: Add MongoDB GPG key apt_key: url: https://www.mongodb.org/static/pgp/server-6.0.asc state: present ``` 或者: ```yaml - name: Download MongoDB GPG key get_url: url: https://www.mongodb.org/static/pgp/server-6.0.asc dest: /tmp/mongodb.gpg ``` 当Ansible任务重复执行时,如果本地已经存在该文件,并且服务器端的资源未修改,get_url模块可能会返回304错误。这时候任务可能会失败,因为模块期望的是200 OK响应,而304会被视为错误。过,我需要确认Ansible的版本,因为同版本的模块处理方式可能同。例如,较新版本的get_url模块可能默认会忽略304状态码,或者有参数可以控制这一行为。 解决方案可能有几种: 1. **使用force参数**:在get_url模块中设置force=no,这样如果文件已存在,就会重新下载。过,这可能导致问题,如果用户希望强制更新密钥的话。 2. **忽略HTTP 304错误**:通过设置status_code参数,将304视为成功状态。例如: ```yaml - name: Download MongoDB GPG key get_url: url: https://www.mongodb.org/static/pgp/server-6.0.asc dest: /tmp/mongodb.gpg status_code: 200,304 ``` 这样,无论是200还是304,任务都会成功。 3. **直接使用apt_key模块的密钥内容**:避免下载文件,直接将密钥的内容通过keyserver添加。例如: ```yaml - name: Add MongoDB GPG key via keyserver apt_key: keyserver: keyserver.ubuntu.com id: 20691EEC35216C63CAF66CE1656408E390CFB1F5 state: present ``` 这种方法可能更可靠,因为通过keyserver获取密钥可能更稳定。 4. **使用shell命令手动添加密钥**:虽然推荐在Ansible中使用shell模块,但在某些情况下可以作为临时解决方案: ```yaml - name: Add MongoDB GPG key via command shell: curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb.gpg ``` 过需要注意,这种方法可能缺乏幂等性,需要添加creates参数来确保会重复执行。 另外,需要确认用户使用的Ansible版本,以及模块的文档。例如,apt_key模块在较新的版本中可能已经处理了这种情况。如果用户的任务中已经存在文件,可能需要清理缓存或旧文件,然后重试。 最后,用户提到的引用[^1]中的内容可能与主机变量相关,但和这个问题关联大,可能是个错误引用。需要提醒用户检查引用来源的正确性。</think>针对MongoDB GPG密钥安装过程中出现的HTTP 304错误,以下是分步解决方案: ### 1. 理解错误原因 HTTP 304状态码表示请求的资源未被修改,通常由于本地缓存导致Ansible认为"无需重复下载"。该错误常见于以下场景: - 使用`get_url`模块时文件已存在且未被修改 - `apt_key`模块尝试重复添加相同密钥 - 服务器端GPG密钥未更新[^1] ### 2. 推荐解决方案 #### 方法一:强制更新密钥 ```yaml - name: 强制更新MongoDB GPG密钥 get_url: url: https://www.mongodb.org/static/pgp/server-6.0.asc dest: /tmp/mongodb.gpg force: yes # 强制覆盖现有文件 status_code: 200,304 # 同时接受200和304状态码 register: download_result - name: 添加密钥到APT apt_key: data: "{{ lookup(&#39;file&#39;, &#39;/tmp/mongodb.gpg&#39;) }}" state: present ``` #### 方法二:使用密钥服务器直接添加 ```yaml - name: 通过密钥服务器添加 apt_key: keyserver: keyserver.ubuntu.com id: 20691EEC35216C63CAF66CE1656408E390CFB1F5 state: present ``` ### 3. 验证配置 ```yaml - name: 验证密钥存在 shell: apt-key list | grep MongoDB register: key_check failed_when: "MongoDB not in key_check.stdout" ``` ### 4. 清理缓存(可选) ```yaml - name: 清理APT缓存 apt: update_cache: yes cache_valid_time: 3600 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值