在Linux上监控MongoDB运行状态,可以使用以下几种方法:
mongostat
mongostat
是一个轻量级的命令行工具,用于实时监控MongoDB的性能指标。
mongostat --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase admin
例如:
mongostat --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
mongotop
mongotop
是一个实时监控MongoDB数据库操作的工具,类似于Linux的top
命令。
mongotop --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase admin
例如:
mongotop --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
MongoDB Compass是MongoDB官方提供的图形化界面工具,可以用来监控和管理MongoDB实例。
Prometheus和Grafana是流行的监控和可视化工具组合。
prometheus.yml
文件,添加MongoDB的抓取配置:scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['<hostname>:<port>']
例如:
scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['localhost:27017']
db.collection.stats()
你可以在MongoDB shell中使用db.collection.stats()
命令来获取特定集合的统计信息。
mongo --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase admin
例如:
mongo --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
然后在MongoDB shell中执行:
db.yourCollection.stats()
db.serverStatus()
你也可以在MongoDB shell中使用db.serverStatus()
命令来获取服务器的全局状态信息。
db.serverStatus()
通过这些方法,你可以全面监控MongoDB在Linux上的运行状态。选择适合你需求的方法进行监控。