Loading

物理部署mysqld_exporter

1. 创建监控用户(MySQL 中)

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

如需远程访问,将 'localhost' 改成 % 或指定主机 IP。

2. 下载 MySQL Exporter

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-arm64.tar.gz
tar -zxvf mysqld_exporter-0.15.0.linux-arm64.tar.gz
cd mysqld_exporter-0.15.0.linux-arm64
cp mysqld_exporter /usr/local/bin/

3. 配置数据库连接

可通过环境变量或配置文件,推荐写 .my.cnf 文件或使用 DATA_SOURCE_NAME:

  • 配置文件

创建 ~/.my.cnf 文件:

[client]
user=exporter
password=exporter_password
host=127.0.0.1
port=3306

然后运行时添加 --config.my-cnf 参数:

4. 创建 Systemd 服务

$ /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
After=network.target

[Service]
User=root
ExecStart=/usr/local/bin/mysqld_exporter --web.listen-address=0.0.0.0:9104  --config.my-cnf=/etc/.my.cnf
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

5. 启动 MySQL Exporter

systemctl daemon-reload
systemctl restart mysqld_exporter
systemctl status mysqld_exporter -l
posted @ 2025-07-17 14:44  亚里士多智  阅读(25)  评论(0)    收藏  举报