在CentOS上监控MongoDB的运行状态,可以使用以下几种方法:
mongostatmongostat是一个简单的命令行工具,用于监控MongoDB的性能指标。
mongostat --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase admin
例如:
mongostat --host localhost --port 27017 --username admin --password yourpassword --authenticationDatabase admin
mongotopmongotop是一个实时监控MongoDB数据库性能的工具,显示每个数据库和集合的读写操作。
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是流行的监控和可视化工具组合。
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar xvfz prometheus-2.30.3.linux-amd64.tar.gz
cd prometheus-2.30.3.linux-amd64
prometheus.yml文件,添加MongoDB的监控配置:scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['<hostname>:<port>']
./prometheus --config.file=prometheus.yml
wget https://dl.grafana.com/oss/release/grafana-8.2.0.linux-amd64.tar.gz
tar xvfz grafana-8.2.0.linux-amd64.tar.gz
cd grafana-8.2.0
./bin/grafana-server
在浏览器中访问http://<hostname>:3000,使用默认用户名和密码(admin/admin)登录。
添加Prometheus数据源:
http://localhost:9090),点击“Save & Test”。添加监控面板:
mongodb_server_status_currentOp。systemd服务监控如果你使用systemd管理服务,可以创建一个自定义的服务来监控MongoDB的状态。
/usr/local/bin/monitor_mongodb.sh:#!/bin/bash
while true; do
echo "Checking MongoDB status..."
systemctl status mongod
sleep 10
done
chmod +x /usr/local/bin/monitor_mongodb.sh
systemd服务文件/etc/systemd/system/monitor_mongodb.service:[Unit]
Description=Monitor MongoDB
After=mongod.service
[Service]
ExecStart=/usr/local/bin/monitor_mongodb.sh
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start monitor_mongodb
systemctl enable monitor_mongodb
通过以上方法,你可以在CentOS上有效地监控MongoDB的运行状态。