在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上的運行狀態。選擇適合你需求的方法進行監控。