还在手动配 mysql_exporter?
别再手动一步步敲了,太容易错、效率还低。下面给你一套**一键部署 + 多实例管理 + 自动发现**的成熟方案,直接能用。
---
## 一、手动配有多痛苦(你肯定遇到过)
- 每台机器:下载 → 写 `.my.cnf` → 写 systemd → 改 Prometheus → 重启 → 验证,**30 分钟/台**
- 密码一改,所有机器都要改,**改错一个就崩**
- 多实例要开多个端口、多个配置,**管理混乱**
- 权限、`performance_schema`、collector 开关,**漏一个就没指标**
---
## 二、最佳实践:用脚本一键部署(1 分钟/台)
### 1)一键脚本(可直接用)
```bash
#!/bin/bash
set -euo pipefail
VERSION="0.16.0"
LISTEN_ADDR="0.0.0.0:9104"
MYSQL_HOST="$1"
MYSQL_USER="$2"
MYSQL_PASS="$3"
if [ $# -ne 3 ]; then
echo "用法: $0 <mysql-host> <user> <pass>"
exit 1
fi
# 1. 安装二进制
wget https://github.com/prometheus/mysqld_exporter/releases/download/v${VERSION}/mysqld_exporter-${VERSION}.linux-amd64.tar.gz
tar zxf mysqld_exporter-${VERSION}.linux-amd64.tar.gz
cp mysqld_exporter-${VERSION}.linux-amd64/mysqld_exporter /usr/local/bin/
rm -rf mysqld_exporter-*
# 2. 配置 .my.cnf
mkdir -p /etc/mysqld_exporter
cat > /etc/mysqld_exporter/.my.cnf <<EOF
[client]
user=${MYSQL_USER}
password=${MYSQL_PASS}
host=${MYSQL_HOST}
port=3306
EOF
chmod 600 /etc/mysqld_exporter/.my.cnf
# 3. systemd 服务
cat > /etc/systemd/system/mysqld_exporter.service <<EOF
[Unit]
Description=MySQL Exporter
After=network.target
[Service]
ExecStart=/usr/local/bin/mysqld_exporter \
--config.my-cnf=/etc/mysqld_exporter/.my.cnf \
--web.listen-address=${LISTEN_ADDR} \
--collect.global_status \
--collect.info_schema.innodb_metrics \
--collect.slave_status \
--collect.info_schema.processlist
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysqld_exporter
# 4. 验证
sleep 3
curl -s http://${LISTEN_ADDR}/metrics | head -20
echo "✅ 部署完成"
```
### 2)使用方式
```bash
./deploy.sh 127.0.0.1 exporter 'YourPass123'
```
### 3)MySQL 提前建好监控账号(最小权限)
```sql
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'YourPass123' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
```
---
## 三、多实例不要多进程:用 **/probe 多目标模式**
官方支持**一个 exporter 监控多台 MySQL**,不用每个实例开一个进程。
### 1)`.my.cnf` 多账号
```ini
[client]
user=exporter
password=YourPass123
[client.slave1]
host=192.168.1.10
user=exporter
password=YourPass123
[client.slave2]
host=192.168.1.11
user=exporter
password=YourPass123
```
### 2)启动一个 exporter
```bash
mysqld_exporter --config.my-cnf=.my.cnf --web.listen-address=:9104
```
### 3)Prometheus 配置(动态探测)
```yaml
scrape_configs:
- job_name: 'mysql-probe'
metrics_path: /probe
static_configs:
- targets:
- 192.168.1.10:3306
- 192.168.1.11:3306
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9104 # exporter 地址
```
访问:
```
http://exporter:9104/probe?target=192.168.1.10:3306&auth_module=client.slave1
```
---
## 四、更高级:用 Ansible 全集群自动化
适合 10+ 台机器:
- 写一个 playbook,统一下发二进制、配置、systemd
- 密码用 Ansible Vault 加密
- Prometheus 配置自动生成 + 重载
一句话:**一次编写,到处部署,永不手抖**。
---
## 五、避坑 3 点
1. **必须开 performance_schema**(否则很多指标为空)
```ini
[mysqld]
performance_schema = ON
```
2. **`.my.cnf` 权限必须 600**,否则 exporter 报警
3. **不要用 root 当监控用户**,遵循最小权限原则
---
结论:
- 单机:用上面脚本 **1 分钟搞定**
- 多实例:用 **/probe 模式**,一个 exporter 管所有
- 大规模:上 **Ansible**,全集群自动化





