目錄
- 內容概述
- 一、創建MongoDB監控專用用戶
- 二、安裝MongoDB Exporter
- 三、啟動Exporter服務
- 四、配置Systemd服務
- 五、服務管理命令
- 六、Prometheus集成配置
- 七、Grafana看板
內容概述
本教程詳細演示了如何在Linux系統中部署MongoDB Exporter以監控MongoDB數據庫,并將其集成到Prometheus監控體系。教程涵蓋以下核心步驟:
- 創建具備監控權限的MongoDB專用用戶
- 安裝配置MongoDB Exporter二進制包
- 創建Systemd服務實現守護進程管理
- 配置Prometheus抓取Exporter指標
- 基礎服務管理命令演示
一、創建MongoDB監控專用用戶
mongo -u admin -p 123456 --authenticationDatabase admin
- 切換至admin數據庫
use admin
- 創建監控用戶(包含集群監控和本地庫讀權限)
db.createUser({user: "mongodb_expo",pwd: "SecurePassword",roles: [{ role: "clusterMonitor", db: "admin" },{ role: "read", db: "local" }]
});
- 驗證用戶創建
db.getUser("mongodb_expo")
- 退出MongoDB
ctrl+z
二、安裝MongoDB Exporter
# 創建安裝目錄
mkdir -p /usr/local/mongodb_exporter# 解壓安裝包(需提前下載對應版本)
tar -zxvf mongodb_exporter-0.20.5.linux-amd64.tar.gz -C /usr/local/mongodb_exporter --strip-components=1
三、啟動Exporter服務
# 基礎啟動命令
/usr/local/mongodb_exporter/mongodb_exporter \--mongodb.uri='mongodb://mongodb_expo:SecurePassword@192.168.15.131:27017/admin' \--web.listen-address=:9216 \--compatible-mode# 查看幫助參數
/usr/local/mongodb_exporter/mongodb_exporter --help
四、配置Systemd服務
vim /usr/lib/systemd/system/mongodb_exporter.service
[Unit]
Description=MongoDB Exporter
After=network.target[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/mongodb_exporter/mongodb_exporter \--mongodb.uri=mongodb://mongodb_expo:SecurePassword@192.168.15.131:27017/admin \--web.listen-address=:9216 \--compatible-mode
Restart=always
RestartSec=3[Install]
WantedBy=multi-user.target
五、服務管理命令
# 重載systemd配置
systemctl daemon-reload# 設置開機自啟
systemctl enable mongodb_exporter# 服務控制
systemctl stop mongodb_exporter
systemctl restart mongodb_exporter
systemctl status mongodb_exporter
六、Prometheus集成配置
vim /usr/local/prometheus/prometheus.yml
scrape_configs:- job_name: 'mongodb'static_configs: - targets: ['192.168.15.131:9216']
# 應用配置變更(注意:原命令中的postgres_exporter應為筆誤)
systemctl restart mongodb_exporter