文章目录
**Prometheus 监控 MySQL **
1. 目标
- 实时监控 MySQL 关键性能指标(如连接数、查询吞吐量、复制状态等)。
- 设置告警规则,及时发现数据库异常。
- 通过 Grafana 可视化监控数据。
2. 环境准备
2.1 所需组件
组件 | 作用 | 安装位置 |
---|---|---|
Prometheus Server | 指标采集与存储 | 监控服务器 |
mysqld_exporter | 暴露 MySQL 指标 | MySQL 服务器 |
Grafana | 数据可视化 | 监控服务器 |
Alertmanager | 告警通知管理 | 监控服务器 |
2.2 权限要求
- MySQL 用户需具备
PROCESS
,REPLICATION CLIENT
,SELECT
权限:CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'YourStrongPassword'; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; FLUSH PRIVILEGES;
3. 部署 mysqld_exporter
3.1 下载与安装
# 下载最新版 (替换版本号)
VERSION="0.15.1"
wget https://github.com/prometheus/mysqld_exporter/releases/download/v${VERSION}/mysqld_exporter-${VERSION}.linux-amd64.tar.gz
tar xvf mysqld_exporter-${VERSION}.linux-amd64.tar.gz
sudo mv mysqld_exporter-${VERSION}.linux-amd64/mysqld_exporter /usr/local/bin/
3.2 创建配置文件
创建 .my.cnf
文件存储数据库连接信息:
sudo tee /etc/.mysqld_exporter.cnf <<EOF
[client]
user=exporter
password=YourStrongPassword
host=localhost
port=3306
EOF
sudo chmod 600 /etc/.mysqld_exporter.cnf # 限制权限
3.3 创建 Systemd 服务
/etc/systemd/system/mysqld_exporter.service
:
[Unit]
Description=MySQL Prometheus Exporter
After=network.target
[Service]
User=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter \
--config.my-cnf=/etc/.mysqld_exporter.cnf \
--collect.global_status \
--collect.info_schema.innodb_metrics \
--collect.slave_status \
--collect.info_schema.processlist
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start mysqld_exporter
sudo systemctl enable mysqld_exporter
3.4 验证 Exporter
访问指标接口:
curl http://localhost:9104/metrics
应输出包含 mysql_
前缀的指标。
4. 配置 Prometheus
4.1 添加 Job 到 prometheus.yml
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['mysql-server-ip:9104'] # Exporter 地址
relabel_configs:
- source_labels: [__address__]
target_label: instance
replacement: 'mysql-primary' # 实例标识
4.2 重载 Prometheus
curl -X POST http://localhost:9090/-/reload # 或重启服务
5. 核心监控指标说明
指标名称 | 含义 | 告警建议 |
---|---|---|
mysql_global_status_threads_connected |
当前连接数 | > 80% max_connections |
mysql_global_status_threads_running |
活跃连接数 | 持续 > 100 |
mysql_global_status_slow_queries |
慢查询计数 | 短时间内突增 |
mysql_global_variables_max_connections |
最大连接数 | 规划容量参考 |
mysql_slave_status_slave_io_running |
主从 IO 线程状态 | ≠ 1 (异常) |
mysql_info_schema_innodb_row_lock_time_avg |
平均行锁等待时间 | > 500ms |
6. 告警规则配置
6.1 创建告警规则文件
/etc/prometheus/rules/mysql_alerts.yml
:
groups:
- name: MySQL-Alerts
rules:
- alert: MySQLHighConnections
expr: mysql_global_status_threads_connected / mysql_global_variables_max_connections * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "MySQL 高连接数 ({{ $value }}%)"
description: "实例 {{ $labels.instance }} 连接数超过 80% 限制"
- alert: MySQLReplicationFailure
expr: mysql_slave_status_slave_io_running != 1 or mysql_slave_status_slave_sql_running != 1
for: 1m
labels:
severity: critical
annotations:
summary: "MySQL 复制中断 ({{ $labels.instance }})"
6.2 在 prometheus.yml
中加载规则
rule_files:
- "/etc/prometheus/rules/mysql_alerts.yml"
7. Grafana 仪表盘配置
- 导入官方 Dashboard:
- ID
7362
(MySQL Overview)
- ID
- 配置 Prometheus 为数据源。
8. 维护与优化
8.1 定期检查
- 验证 Exporter 状态:
systemctl status mysqld_exporter
- 检查指标收集延迟:
prometheus_target_interval_length_seconds
- 审核 MySQL 用户权限(每年至少一次)。
8.2 安全加固
- 使用 TLS 加密 Exporter 通信:
mysqld_exporter --web.config.file=/path/to/web-config.yml
web-config.yml
示例:tls_server_config: cert_file: server.crt key_file: server.key
8.3 性能调整
- 限制采集的指标(减少负载):
--no-collect.info_schema.tables # 禁用表统计
- 调整 Prometheus 抓取间隔(默认 15s):
scrape_interval: 30s # prometheus.yml
9. 故障排查
9.1 Exporter 无数据
- 检查 MySQL 用户权限。
- 测试连接:
mysql --defaults-file=/etc/.mysqld_exporter.cnf -e "SHOW STATUS"
- 查看 Exporter 日志:
journalctl -u mysqld_exporter -f
9.2 Prometheus 未抓取
- 访问
http://prom-server:9090/targets
检查 Target 状态。 - 验证网络连通性:
telnet mysql-server-ip 9104
10. 附录
- mysqld_exporter 官方文档:
https://github.com/prometheus/mysqld_exporter - MySQL 监控关键指标指南:
https://prometheus.io/docs/guides/mysqld-exporter/
按照此文档,可实现 MySQL 的全面监控与告警,确保数据库稳定性。部署后需通过压力测试验证监控有效性,并根据业务特点调整告警阈值。