prometheus 监控mysql数据库

2022年5月31日

mysql_exporter
# 下载 mysql_exporter 的程序包,并解压到对应目录

cd /opt/
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz
ls mysqld_exporter-0.14.0.linux-amd64/
LICENSE mysqld_exporter NOTICE
mv mysqld_exporter-0.14.0.linux-amd64/ mysqld_exporter_prometheus


# 想要获取的监控数据,需要授权程序能够连接到 mysql 即需要创建连接 mysql 的用户
# 创建用户时建议为用户设置最大连接限制,以避免在重负载下监控刮擦而使服务器过载

 

MYSQL8

CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'127.0.0.1';
FLUSH PRIVILEGES;


MariaDB-1:10.4

GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'127.0.0.1' identified by "123456" ;


# 通过创建 .my.cnf 文件,指定 mysql 的连接信息

vim /opt/mysqld_exporter_prometheus/.my.cnf

[client]
user=mysqld_exporter
password=123456
host=127.0.0.1


# 注意这里的host要与创建用户时指定的host相同,这里使用 127.0.0.1 而不是 localhost
# 这是因为如果 mysql 设置了参数 skip_name_resolve = 1 的情况下是不会解析 localhost 的
# 这就会导致 mysqld_exporter 无法连接 mysql
# 而报错 Error 1045: Access denied for user 'mysqld_exporter'@'::1' (using password: YES)

# 创建 systemd 管理配置文件

Ubuntu

cat /usr/lib/systemd/system/mysqld_exporter.service

Debain

cat /etc/systemd/system/mysqld_exporter.service

 

[Unit]
Description=mysql_exporter
Documentation=https://github.com/prometheus/mysqld_exporter
After=network.target
 
[Service]
Type=simple
ExecStart=/opt/mysqld_exporter_prometheus/mysqld_exporter \
--config.my-cnf=/opt/mysqld_exporter_prometheus/.my.cnf \
--web.listen-address=:9104 \
--web.telemetry-path=/metrics
Restart=on-failure
 
[Install]
WantedBy=multi-user.target


# 启动,并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start mysqld_exporter &&
  3. systemctl enable mysqld_exporter

# 编辑prometheus配置文件,加入如下配置

vi /usr/local/prometheus/prometheus.yml

- job_name: 'mysql_0.245'
scrape_interval: 30s
static_configs:
- targets: ['192.168.0.245:9104']


# 重新加载 prometheus 使其配置生效

systemctl reload prometheus

Grafana 模板导入
https://grafana.com/grafana/dashboards/7362

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注