@Prometheus 监控-MySQL (Mysqld Exporter)

文章目录

    • [**Prometheus 监控 MySQL **](#**Prometheus 监控 MySQL **)
      • [**1. 目标**](#1. 目标)
      • [**2. 环境准备**](#2. 环境准备)
        • [**2.1 所需组件**](#2.1 所需组件)
        • [**2.2 权限要求**](#2.2 权限要求)
      • [**3. 部署 mysqld_exporter**](#3. 部署 mysqld_exporter)
        • [**3.1 下载与安装**](#3.1 下载与安装)
        • [**3.2 创建配置文件**](#3.2 创建配置文件)
        • [**3.3 创建 Systemd 服务**](#3.3 创建 Systemd 服务)
        • [**3.4 验证 Exporter**](#3.4 验证 Exporter)
      • [**4. 配置 Prometheus**](#4. 配置 Prometheus)
        • [**4.1 添加 Job 到 `prometheus.yml`**](#4.1 添加 Job 到 prometheus.yml)
        • [**4.2 重载 Prometheus**](#4.2 重载 Prometheus)
      • [**5. 核心监控指标说明**](#5. 核心监控指标说明)
      • [**6. 告警规则配置**](#6. 告警规则配置)
        • [**6.1 创建告警规则文件**](#6.1 创建告警规则文件)
        • [**6.2 在 `prometheus.yml` 中加载规则**](#6.2 在 prometheus.yml 中加载规则)
      • [**7. Grafana 仪表盘配置**](#7. Grafana 仪表盘配置)
      • [**8. 维护与优化**](#8. 维护与优化)
        • [**8.1 定期检查**](#8.1 定期检查)
        • [**8.2 安全加固**](#8.2 安全加固)
        • [**8.3 性能调整**](#8.3 性能调整)
      • [**9. 故障排查**](#9. 故障排查)
        • [**9.1 Exporter 无数据**](#9.1 Exporter 无数据)
        • [**9.2 Prometheus 未抓取**](#9.2 Prometheus 未抓取)
      • [**10. 附录**](#10. 附录)

**Prometheus 监控 MySQL **

1. 目标

  • 实时监控 MySQL 关键性能指标(如连接数、查询吞吐量、复制状态等)。
  • 设置告警规则,及时发现数据库异常。
  • 通过 Grafana 可视化监控数据。

2. 环境准备

2.1 所需组件
组件 作用 安装位置
Prometheus Server 指标采集与存储 监控服务器
mysqld_exporter 暴露 MySQL 指标 MySQL 服务器
Grafana 数据可视化 监控服务器
Alertmanager 告警通知管理 监控服务器
2.2 权限要求
  • MySQL 用户需具备 PROCESS, REPLICATION CLIENT, SELECT 权限:

    sql 复制代码
    CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'YourStrongPassword';
    GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
    FLUSH PRIVILEGES;

3. 部署 mysqld_exporter

3.1 下载与安装
bash 复制代码
# 下载最新版 (替换版本号)
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 文件存储数据库连接信息:

bash 复制代码
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

ini 复制代码
[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
bash 复制代码
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
yaml 复制代码
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
bash 复制代码
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

yaml 复制代码
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 中加载规则
yaml 复制代码
rule_files:
  - "/etc/prometheus/rules/mysql_alerts.yml"

7. Grafana 仪表盘配置

  1. 导入官方 Dashboard:
  2. 配置 Prometheus 为数据源。

8. 维护与优化

8.1 定期检查
  • 验证 Exporter 状态:systemctl status mysqld_exporter
  • 检查指标收集延迟:prometheus_target_interval_length_seconds
  • 审核 MySQL 用户权限(每年至少一次)。
8.2 安全加固
  • 使用 TLS 加密 Exporter 通信:

    bash 复制代码
    mysqld_exporter --web.config.file=/path/to/web-config.yml

    web-config.yml 示例:

    yaml 复制代码
    tls_server_config:
      cert_file: server.crt
      key_file: server.key
8.3 性能调整
  • 限制采集的指标(减少负载):

    bash 复制代码
    --no-collect.info_schema.tables  # 禁用表统计
  • 调整 Prometheus 抓取间隔(默认 15s):

    yaml 复制代码
    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. 附录


按照此文档,可实现 MySQL 的全面监控与告警,确保数据库稳定性。部署后需通过压力测试验证监控有效性,并根据业务特点调整告警阈值。

相关推荐
南風_入弦4 分钟前
优化09-表连接
数据库·oracle
Snk0xHeart1 小时前
极客大挑战 2019 EasySQL 1(万能账号密码,SQL注入,HackBar)
数据库·sql·网络安全
····懂···1 小时前
数据库OCP专业认证培训
数据库·oracle·ocp
学习中的码虫2 小时前
数据库-MySQL
数据库
天天摸鱼的java工程师2 小时前
高考放榜夜,系统别崩!聊聊查分系统怎么设计,三张表足以?
java·后端·mysql
Karry的巡洋舰2 小时前
【数据库】安全性
数据库·oracle
软件测试小仙女3 小时前
鸿蒙APP测试实战:从HDC命令到专项测试
大数据·软件测试·数据库·人工智能·测试工具·华为·harmonyos
exe4523 小时前
jdbc查询mysql数据库时,出现id顺序错误的情况
数据库·mysql
John Song3 小时前
macOS 上使用 Homebrew 安装redis-cli
数据库·redis·macos
数据知道3 小时前
Mac电脑上本地安装 redis并配置开启自启完整流程
数据库·redis·macos