插件窝 干货文章 使用 Prometheus MySQL Exporter 监控 MySQL 和 MariaDB Droplet

使用 Prometheus MySQL Exporter 监控 MySQL 和 MariaDB Droplet

Exporter MySQL collect 收集器 705    来源:    2025-03-28

使用 Prometheus MySQL Exporter 监控 MySQL/MariaDB Droplet

概述

Prometheus MySQL Exporter 是一个用于从 MySQL/MariaDB 服务器收集指标并暴露给 Prometheus 的工具。以下是完整的部署和配置指南。

安装步骤

1. 安装 MySQL Exporter

下载并安装二进制文件

# 创建专用用户
sudo useradd -rs /bin/false mysql_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-0.15.1.linux-amd64.tar.gz
sudo mv mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter /usr/local/bin/
sudo chown mysql_exporter:mysql_exporter /usr/local/bin/mysqld_exporter

或使用 Docker

docker run -d \
  --name mysql_exporter \
  -p 9104:9104 \
  -e DATA_SOURCE_NAME="exporter:password@(mysql-host:3306)/" \
  prom/mysqld-exporter

2. 配置 MySQL 监控用户

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your-password' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;

3. 创建配置文件

创建 /etc/.my.cnf 文件:

[client]
user=exporter
password=your-password

设置权限:

sudo chown mysql_exporter:mysql_exporter /etc/.my.cnf
sudo chmod 600 /etc/.my.cnf

4. 创建 Systemd 服务

创建 /etc/systemd/system/mysql_exporter.service

[Unit]
Description=Prometheus MySQL Exporter
After=network.target

[Service]
User=mysql_exporter
Group=mysql_exporter
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter \
  --config.my-cnf=/etc/.my.cnf \
  --collect.global_status \
  --collect.info_schema.innodb_metrics \
  --collect.auto_increment.columns \
  --collect.info_schema.processlist \
  --collect.binlog_size \
  --collect.info_schema.tablestats \
  --collect.global_variables \
  --collect.info_schema.query_response_time \
  --collect.info_schema.userstats \
  --collect.info_schema.tables \
  --collect.perf_schema.tablelocks \
  --collect.perf_schema.file_events \
  --collect.perf_schema.eventswaits \
  --collect.perf_schema.indexiowaits \
  --collect.perf_schema.tableiowaits \
  --collect.slave_status \
  --web.listen-address=0.0.0.0:9104

Restart=always

[Install]
WantedBy=multi-user.target

启动服务:

sudo systemctl daemon-reload
sudo systemctl enable mysql_exporter
sudo systemctl start mysql_exporter

5. 配置 Prometheus

prometheus.yml 中添加:

scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['mysql-host:9104']
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        regex: '([^:]+)(:[0-9]+)?'
        replacement: '${1}'

重启 Prometheus 服务。

6. 验证

访问 http://mysql-host:9104/metrics 查看指标。

访问 Prometheus UI 查询 mysql_up 指标确认状态。

常用监控指标

  • mysql_up: 服务是否可达
  • mysql_global_status_connections: 总连接数
  • mysql_global_status_threads_connected: 当前连接数
  • mysql_global_status_questions: 查询总数
  • mysql_global_status_slow_queries: 慢查询数
  • mysql_global_variables_max_connections: 最大连接数
  • mysql_info_schema_table_size: 表大小
  • mysql_slave_status_*: 复制状态指标

安全注意事项

  1. 限制 MySQL Exporter 用户的权限
  2. 使用防火墙限制对 9104 端口的访问
  3. 考虑使用 TLS 加密 exporter 端点
  4. 使用强密码并定期更换

故障排除

  1. 连接问题:

    • 检查 .my.cnf 文件权限
    • 验证 MySQL 用户权限
    • 检查 MySQL 是否监听正确接口
  2. 指标缺失:

    • 检查 exporter 启动参数是否包含所需收集器
    • 查看 exporter 日志获取错误信息
  3. 性能问题:

    • 减少收集频率
    • 禁用不需要的收集器
    • 增加 max_user_connections 限制

高级配置

自定义收集器

通过 --collect[]--no-collect[] 参数启用/禁用特定收集器。

过滤数据库

使用 --collect[] 参数过滤特定数据库或表:

--collect.info_schema.tables.databases="db1,db2"

TLS 配置

--web.config.file=/path/to/web-config.yml

其中 web-config.yml 包含 TLS 和认证配置。

通过以上步骤,您可以全面监控 MySQL/MariaDB Droplet 的性能和健康状况。