Appearance
MongoDB 监控
常用服务器状态命令
在 mongosh 或驱动中执行:
javascript
db.serverStatus()可查看连接数、内存、opcounters、锁、副本集状态等。
当前操作
javascript
db.currentOp()查看正在执行的操作,便于排查慢查询或阻塞。
数据库与集合统计
javascript
db.stats() // 当前库的存储、索引、集合数等
db.users.stats() // 集合 users 的文档数、大小、索引等慢查询
在 mongod 配置中开启慢查询日志:
yaml
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100慢查询会记录到 system.profile 集合,可通过 db.system.profile.find() 查看,或使用 MongoDB Atlas / MongoDB Compass 的 Profiler 功能。
监控工具
- MongoDB Atlas:云服务自带监控与告警。
- MongoDB Compass:图形化查看性能与索引。
- mongostat / mongotop:命令行实时统计(QPS、连接、读写时间等)。
- Prometheus + mongodb_exporter:指标采集与告警。
下一节介绍 MongoDB Java。