Prometheus MySQL Exporter 是一个用于从 MySQL/MariaDB 服务器收集指标并暴露给 Prometheus 的工具。以下是完整的部署和配置指南。
# 创建专用用户
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 run -d \
--name mysql_exporter \
-p 9104:9104 \
-e DATA_SOURCE_NAME="exporter:password@(mysql-host:3306)/" \
prom/mysqld-exporter
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your-password' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
创建 /etc/.my.cnf
文件:
[client]
user=exporter
password=your-password
设置权限:
sudo chown mysql_exporter:mysql_exporter /etc/.my.cnf
sudo chmod 600 /etc/.my.cnf
创建 /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
在 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 服务。
访问 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_*
: 复制状态指标连接问题:
.my.cnf
文件权限指标缺失:
性能问题:
max_user_connections
限制通过 --collect[]
和 --no-collect[]
参数启用/禁用特定收集器。
使用 --collect[]
参数过滤特定数据库或表:
--collect.info_schema.tables.databases="db1,db2"
--web.config.file=/path/to/web-config.yml
其中 web-config.yml
包含 TLS 和认证配置。
通过以上步骤,您可以全面监控 MySQL/MariaDB Droplet 的性能和健康状况。