Prometheus 监控 MySQL:mysqld_exporter 部署与关键指标监控

1. mysqld_exporter 部署

步骤:

  1. 下载二进制文件
    Prometheus 官网 下载最新版 mysqld_exporter

    wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
    tar -xvf mysqld_exporter-*.tar.gz
    cd mysqld_exporter-*
    

  2. 创建 MySQL 监控专用用户
    在 MySQL 中执行:

    CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'YourSecurePassword';
    GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
    FLUSH PRIVILEGES;
    

  3. 配置数据源
    创建配置文件 .my.cnf

    [client]
    user=exporter
    password=YourSecurePassword
    

  4. 启动 exporter

    ./mysqld_exporter --config.my-cnf=.my.cnf
    

    默认端口 9104,验证访问:curl http://localhost:9104/metrics


2. Prometheus 配置

prometheus.yml 中添加抓取任务:

scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['mysql_host:9104']  # mysqld_exporter 地址
    metrics_path: /metrics

重启 Prometheus:systemctl restart prometheus


3. 关键指标监控配置
(1) 慢查询监控
  • MySQL 配置
    启用慢查询日志(修改 my.cnf):

    [mysqld]
    slow_query_log = ON
    long_query_time = 2     # 超过 2 秒视为慢查询
    log_output = FILE
    

  • PromQL 查询
    监控慢查询速率:

    rate(mysql_global_status_slow_queries[5m])
    

(2) 连接数监控
  • 关键指标

    • 当前连接数:mysql_global_status_threads_connected
    • 最大连接数:mysql_global_variables_max_connections
  • PromQL 查询
    连接数使用率:

    mysql_global_status_threads_connected / mysql_global_variables_max_connections * 100
    

(3) 缓存命中率监控
  • 计算公式
    InnoDB 缓冲池命中率: $$ \text{命中率} = \left(1 - \frac{\text{innodb_buffer_pool_reads}}{\text{innodb_buffer_pool_read_requests}}\right) \times 100% $$

  • PromQL 查询

    (1 - 
      rate(mysql_global_status_innodb_buffer_pool_reads[5m]) / 
      rate(mysql_global_status_innodb_buffer_pool_read_requests[5m])
    ) * 100
    


4. Grafana 仪表板配置
  1. 导入仪表板
    使用官方模板 MySQL Overview(ID: 7362)。

  2. 核心面板示例

    监控项 PromQL 查询
    慢查询趋势 rate(mysql_global_status_slow_queries[5m])
    连接数水位 mysql_global_status_threads_connected
    缓存命中率 (1 - rate(innodb_buffer_pool_reads)/rate(innodb_buffer_pool_read_requests)) * 100

5. 告警规则示例

在 Prometheus 的 rules.yml 中添加:

groups:
- name: mysql_alerts
  rules:
  - alert: MySQLHighConnections
    expr: mysql_global_status_threads_connected / mysql_global_variables_max_connections > 0.8
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "MySQL 连接数过高 ({{ $value }}%)"

  - alert: MySQLLowCacheHit
    expr: (1 - rate(mysql_global_status_innodb_buffer_pool_reads[5m]) / rate(mysql_global_status_innodb_buffer_pool_read_requests[5m])) * 100 < 90
    for: 10m
    annotations:
      summary: "InnoDB 缓冲池命中率低 ({{ $value }}%)"


验证与优化
  • 验证指标:访问 http://prometheus_server:9090/targets 确认 mysql 任务状态为 UP
  • 性能优化
    • 调整 long_query_time 降低日志开销
    • 增加 max_connections 避免连接耗尽
    • 扩大 innodb_buffer_pool_size 提升缓存命中率

通过以上步骤,即可实现对 MySQL 慢查询、连接数及缓存命中率的实时监控与告警。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐