prometheus_grafana_db_monitoring
使用 Prometheus + Grafana 监控 PostgreSQL、MySQL 和 MongoDB
1. 概述
在现代微服务架构中,数据库是核心组件,性能瓶颈会直接影响系统稳定性和业务连续性。本方案介绍如何通过 Prometheus + Grafana 搭建统一的 PostgreSQL、MySQL、MongoDB 监控平台,实现指标采集、可视化展示及报警。
2. 架构设计
2.1 组件说明
- Postgres Exporter:采集 PostgreSQL 性能指标
- MySQL Exporter:采集 MySQL 指标
- MongoDB Exporter:采集 MongoDB 运行状态
- Prometheus:时序数据库,存储所有指标数据
- Grafana:可视化工具,展示实时监控面板
2.2 架构图

2.3 端口使用和下载地址
| 服务 | 端口 | 下载地址 |
|---|---|---|
| mongodb_exporter | 9216 | 下载链接 |
| mysql_exporter | 9104 | 下载链接 |
| postgresql_exporter | 9187 | 下载链接 |
| Prometheus | 9090 | 下载链接 |
| Grafana | 3000 | 下载链接 |
3. 环境准备
Linux 系统
Prometheus
Grafana 10+
已安装的 PostgreSQL、MySQL、MongoDB 数据库
Exporter 工具:
- mongodb_exporter
- mysqld_exporter
- postgres_exporter
4. 部署步骤
4.1 部署 PostgreSQL Exporter
1. 创建监控用户
-- 创建专用监控用户
CREATE USER exporter WITH PASSWORD '123456';
-- 设置默认搜索路径
ALTER USER exporter SET SEARCH_PATH TO pg_catalog;
-- 赋予监控权限
GRANT CONNECT ON DATABASE postgres TO exporter;
GRANT pg_exporter TO exporter;
2. 测试启动
export DATA_SOURCE_NAME=postgresql://exporter:[email protected]:5432/postgres?sslmode=disable
/usr/local/postgres_exporter --web.listen-address=:9187
无报错信息后可使用 Ctrl+C 中断。
3. 创建 Systemd 服务
/usr/lib/systemd/system/postgres_exporter.service
[Unit]
Description=PostgreSQL Exporter
After=network-online.target
[Service]
Type=simple
User=root
Group=root
Environment=DATA_SOURCE_NAME=postgresql://exporter:[email protected]:5432/postgres?sslmode=disable
ExecStart=/usr/local/postgres_exporter --web.listen-address=:9187
ExecReload=/bin/kill -HUP
KillMode=process
TimeoutStopSec=20s
Restart=always
[Install]
WantedBy=default.target
4. 启动服务
systemctl daemon-reload
systemctl start postgres_exporter.service
systemctl enable postgres_exporter.service
4.2 部署 MySQL Exporter
1. 创建监控用户
CREATE USER 'exporter'@'%' IDENTIFIED BY '123456';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
FLUSH PRIVILEGES;
2. 配置文件 /etc/prom/my.cnf
[client]
user=exporter
password=123456
host=192.168.75.40
port=3306
3. 测试启动
/usr/local/mysqld_exporter --web.listen-address=:9104 --config.my-cnf="/etc/prom/my.cnf"
4. Systemd 服务配置
/usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
[Service]
ExecStart=/usr/local/mysqld_exporter \
--web.listen-address=:9104 \
--collect.info_schema.processlist \
--collect.info_schema.innodb_tablespaces \
--collect.info_schema.innodb_metrics \
--collect.perf_schema.tableiowaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tablelocks \
--collect.engine_innodb_status \
--collect.perf_schema.file_events \
--collect.binlog_size \
--collect.info_schema.clientstats \
--collect.perf_schema.eventswaits \
--config.my-cnf=/etc/prom/my.cnf
[Install]
WantedBy=multi-user.target
5. 启动服务
systemctl daemon-reload
systemctl start mysqld_exporter.service
systemctl enable mysqld_exporter.service
4.3 部署 MongoDB Exporter
1. 创建监控用户
use admin
db.createUser({
user: "exporter",
pwd: "123321",
roles: [
{ role: "clusterexporter", db: "admin" },
{ role: "read", db: "local" }
]
})
2. 测试启动
/usr/local/mongodb_exporter \
--mongodb.uri=mongodb://exporter:[email protected]:27017 \
--web.listen-address=":9216" \
--collector.dbstats \
--collector.topmetrics \
--discovering-mode \
--compatible-mode
3. Systemd 服务配置
/usr/lib/systemd/system/mongodb_exporter.service
[Unit]
Description=MongoDB Exporter
After=network-online.target
[Service]
User=root
Restart=on-failure
ExecStart=/usr/local/mongodb_exporter \
--web.listen-address=":9216" \
--mongodb.uri=mongodb://exporter:[email protected]:27017 \
--collector.dbstats \
--collector.topmetrics \
--discovering-mode \
--compatible-mode
[Install]
WantedBy=multi-user.target
4. 启动服务
systemctl daemon-reload
systemctl start mongodb_exporter
systemctl enable mongodb_exporter
4.4 部署 Prometheus
1. 编辑配置 /etc/prom/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'node_db'
file_sd_configs:
- files:
- "/etc/prom/node_db.yaml"
refresh_interval: 30s
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
2. 编辑 /etc/prom/node_db.yaml
- targets: ["127.0.0.1:9104"]
labels:
app: "mysql"
- targets: ["127.0.0.1:9216"]
labels:
app: "mongodb"
- targets: ["127.0.0.1:9187"]
labels:
app: "pgsql"
3. Systemd 服务配置
/usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Service
Documentation=https://prometheus.io
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/prometheus \
--web.listen-address=0.0.0.0:9090 \
--config.file=/etc/prom/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
4. 启动服务
systemctl daemon-reload
systemctl start prometheus.service
systemctl enable prometheus.service
访问:
4.5 部署 Grafana
1. 下载并安装
sudo apt-get install -y adduser libfontconfig1 musl
wget https://dl.grafana.com/grafana-enterprise/release/12.1.1/grafana-enterprise_12.1.1_16903967602_linux_amd64.deb
sudo dpkg -i grafana-enterprise_12.1.1_16903967602_linux_amd64.deb
systemctl enable grafana-server
systemctl start grafana-server
2. 默认登录信息
- 账号:
admin - 密码:
admin
3. 修改端口(可选)
编辑 /etc/grafana/grafana.ini
http_port = 3000
重启服务:
systemctl restart grafana-server
5. Grafana 可视化
访问 Grafana 控制台
添加 Prometheus 数据源
导入官方 Dashboard:
MongoDB:
- MongoDB =
14997 - Opstree/Mongodb Dashboard =
16490
- MongoDB =
MySQL:
- MySQL Exporter Quickstart and Dashboard =
14057
- MySQL Exporter Quickstart and Dashboard =
PostgreSQL:
- PostgreSQL Database with kube-prometheus-stack =
13115
- PostgreSQL Database with kube-prometheus-stack =
即可查看各类数据库的核心监控指标
6. 监控效果
- 数据库核心性能指标一览无余
- 出现连接数飙升、慢查询或磁盘延迟时可及时报警
- 利用 Grafana 报表进行周期性分析,优化数据库参数配置
7. 总结
通过 Prometheus + Grafana 构建统一的数据库监控平台,可以:
- 降低多数据库环境下的监控成本
- 提升运维问题定位效率
赏
支付宝打赏
微信打赏
支付宝打赏
微信打赏
赞赏是不耍流氓的鼓励