Loading

使用 mysqld_exporter 监控 MySQL 数据库

背景

最近在做 slurm 相关的应用部署,需要压测一下是否会对 MariaDB 造成较大影响,需要对数据库进行监控。

效果展示

image

安装方法

1. 创建监控用户

# 登录数据库,请修改密码为你自己的密码
CREATE USER 'exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT REPLICATION CLIENT, PROCESS, SLAVE MONITOR ON *.* TO 'exporter'@'localhost' identified by '123456';
GRANT SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;

2. 下载并配置 mysqld_exporter 二进制

  1. 下载二进制
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.19.0/mysqld_exporter-0.19.0.linux-amd64.tar.gz
tar -C /opt -xvzf mysqld_exporter-0.19.0.linux-amd64.tar.gz
mv /opt/mysqld_exporter-0.19.0.linux-amd64 /opt/mysqld_exporter
  1. 配置守护进程
sudo tee /etc/systemd/system/mysqld_exporter.service << 'EOF'
[Unit]
Description=Mysqld Exporter
Documentation=https://github.com/prometheus/mysqld_exporter
After=network-online.target

[Service]
User=root
EnvironmentFile=/etc/default/mysqld_exporter
ExecStart=/opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/.my.cnf --collect.info_schema.innodb_metrics --collect.info_schema.tables --collect.info_schema.processlist --collect.info_schema.tables.databases=* --mysqld.username=exporter
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
  1. 配置 my.cnf
# sudo tee /opt/mysqld_exporter/.my.cnf << 'EOF'
[client]
user=exporter
password=123456
EOF

3. 启动服务

systemctl daemon-reload
systemctl enable --now mysqld_exporter

参考资料

  1. https://wiki.eryajf.net/pages/2499.html
  2. https://github.com/prometheus/mysqld_exporter
  3. https://nsrc.org/workshops/2021/sanog37/nmm/netmgmt/en/prometheus/ex-node-exporter.htm
posted @ 2026-07-14 13:35  codesucks  阅读(12)  评论(0)    收藏  举报