MySQL Server Exporter:Prometheus 监控 MySQL/MariaDB 指南

MySQL Server Exporter:Prometheus 监控 MySQL/MariaDB 指南

原文 :https://github.com/prometheus/mysqld_exporter

说明:安装方式和配置参数以官方文档为准。源码细节请参考 GitHub 仓库。


项目简介

Prometheus 官方维护的 MySQL Server Exporter,用来采集 MySQL 和 MariaDB 的指标。

支持的数据库版本:

  • MySQL >= 5.6
  • MariaDB >= 10.3

前置:创建数据库监控用户

MySQL 里创建一个专用账号,权限越小越好:

sql 复制代码
CREATE USER 'exporter'@'localhost' IDENTIFIED BY '你的密码' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

MAX_USER_CONNECTIONS 3 限制连接数,防止 exporter 频繁采集把数据库连接池打满。PROCESS 权限用来查 SHOW PROCESSLIST,REPLICATION CLIENT 用来查主从状态。


配置连接信息

配置连接的方式有这么几种:

方式一:.my.cnf 文件(推荐)

创建 .my.cnf 文件,放到 exporter 同目录或指定路径:

ini 复制代码
[client]
host = 127.0.0.1
port = 3306
user = exporter
password = 你的密码

方式二:命令行参数

bash 复制代码
--mysqld.address="127.0.0.1:3306"
--mysqld.username="exporter"

密码通过环境变量传入(避免在命令行明文显示):

bash 复制代码
export MYSQLD_EXPORTER_PASSWORD="你的密码"
./mysqld_exporter

方式三:多目标配置

多个 MySQL 实例共用一个 exporter 时,用 .my.cnf 的多节配置:

ini 复制代码
[client]
user = exporter
password = pwd1

[client.server1]
user = exporter
password = pwd2

[client.server2]
user = exporter
password = pwd3

安装方式

二进制文件

bash 复制代码
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-0.15.1.linux-amd64.tar.gz
cd mysqld_exporter-0.15.1.linux-amd64
# 把 .my.cnf 放到当前目录
./mysqld_exporter

默认监听 9104 端口。

Docker 运行

bash 复制代码
docker run -d \
  -p 9104:9104 \
  -v /path/to/.my.cnf:/.my.cnf \
  prom/mysqld-exporter

单目标 vs 多目标模式

单目标

exporter 直接连本地或指定的 MySQL 实例,在 /metrics 暴露指标。适合每台 MySQL 服务器部署一个 exporter 的场景。

多目标

一个 exporter 代理多个 MySQL 实例,通过 /probe 端点按需采集。配置方式:

yaml 复制代码
# prometheus.yml
scrape_configs:
  - job_name: 'mysql'
    metrics_path: /probe
    params:
      auth_module: [client.server1]
    static_configs:
      - targets:
        - db1:3306
        - db2:3306
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: exporter服务器IP:9104

支持的收集器

默认启用的收集器:

收集器 说明
collect.global_status MySQL 全局状态(SHOW GLOBAL STATUS)
collect.global_variables MySQL 全局变量(SHOW GLOBAL VARIABLES)
collect.slave_status 主从复制状态

可通过参数开启更多收集器:

bash 复制代码
# 启用自增列监控
--collect.auto_increment.columns

# 启用表结构信息采集
--collect.info_schema.tables

# 启用 InnoDB 指标(默认就开了一部分)
--collect.innodb_metrics

# 启用用户统计
--collect.info_schema.userstats

# 启用 performance_schema
--collect.perf_schema.eventsstatements

收集的核心指标

指标 含义
mysql_up MySQL 是否在线(1=正常,0=宕机)
mysql_global_status_connections 累计连接数
mysql_global_status_threads_connected 当前连接数
mysql_global_variables_max_connections 最大连接数配置值
mysql_global_status_innodb_buffer_pool_pages_data InnoDB 缓冲池数据页数
mysql_slave_status_seconds_behind_master 主从复制延迟(秒)
mysql_global_status_uptime MySQL 运行时长

SSL/TLS 连接

如果 MySQL 启用了 SSL,在 .my.cnf 里加:

ini 复制代码
ssl-ca=/path/to/ca.pem
ssl-key=/path/to/client-key.pem
ssl-cert=/path/to/client-cert.pem

常用 PromQL

promql 复制代码
# MySQL 是否在线
mysql_up

# 连接使用率
mysql_global_status_threads_connected / mysql_global_variables_max_connections * 100

# 复制延迟
mysql_slave_status_seconds_behind_master

# InnoDB 缓存命中率
(mysql_global_status_innodb_buffer_pool_read_requests - mysql_global_status_innodb_buffer_pool_reads) / mysql_global_status_innodb_buffer_pool_read_requests * 100

# QPS(每秒查询数)
rate(mysql_global_status_questions[5m])
相关推荐
bksczm1 小时前
MySQL之存储引擎
数据库·mysql
计算机毕设定制辅导-无忧学长1 小时前
《基于微信小程序健康习惯激励系统设计与实现》
java·spring boot·mysql·微信小程序·uniapp·健康习惯激励系统
Decay__2 小时前
15_MySQL 数据同步与迁移方案 —— 复制 / DataX / Canal / 异构同步(对标 Oracle GoldenGate)
运维·数据库·sql·mysql·架构
雪芽蓝域zzs3 小时前
第 2 章:整合 MySQL + MyBatis,搭建数据库基础、实体类、Mapper、测试查询
数据库·mysql·mybatis
余槐i4 小时前
Spring Boot 3.2 偶发500无日志:线程池复用导致请求上下文丢失
spring boot·redis·后端·mysql·性能优化
孤狼GPT4 小时前
ChatGPT、Codex数据库实战:MyBatis-Plus第一页查询正常,翻到第10000页为什么越来越慢?
mysql·chatgpt·mybatis-plus·codex·深分页
Decay__4 小时前
17_MySQL 监控体系构建 —— mysqld_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·mysql·架构
Franciz小测测5 小时前
MySQL 增量备份实践:文件都在,为什么仍可能恢复失败?
数据库·mysql·binlog
幽灵诶16 小时前
Mysql(基于测试的学习)
数据库·mysql
宇宙无敌暴龙战士66617 小时前
MySQL 8 库表操作学习笔记:SQL 基础、库表管理、约束与用户授权全流程
数据库·sql·mysql·学习笔记·后端开发