1.下载Mysqld_exporter相关包:
官网地址: https://github.com/prometheus/mysqld_exporter
→ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
2.解压
→ tar -zxvf mysqld_exporter-0.13.0.linux-amd64.tar.gz
→ mv mysqld_exporter-0.13.0.linux-amd64 mysqld_exporter
3.创建my.cnf文件
→ cd mysqld_exporter
→ vim my.cnf
注意:这个文件中需要写入能够访问数据库的用户名和密码
[client]
user=root
password=
4.创建服务系统文件
→ vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/mysql_export/mysqld_exporter/mysqld_exporter --config.my-cnf /usr/local/mysql_export/mysqld_exporter/my.cnf --collect.heartbeat
Restart=on-failure
[Install]
WantedBy=multi-user.target
5.启动服务,设置开机自启
→ systemctl daemon-reload
→ systemctl start mysqld_exporter.service
→ systemctl enable mysqld_exporter.service #设置开机自启
→ systemctl status mysqld_exporter.service #查看状态
→ systemctl restart mysqld_exporter.service
6.测试metrics是否生效
查看是否存在mysql_up指标
curl http://localhost:9104/metrics
7.在Prometheus的配置文件prometheus.yml中配置mysql的metries路径
注意:mysqld_exporter默认端口是9104
- job_name: 'mysql'
static_configs:
- targets: ['172.**.**.**:9104']
8.热加载Promethues的配置
systemctl reload prometheus.service
9.创建Prometheus报警规则
vim mysql_instance_down.yml
groups:
- name: mysql_instance_down
rules:
- alert: Mysql instance Down
expr: mysql_up{job="mysql"} == 0
for: 20s
labels:
user: mysql
severity: error
annotations:
summary: "mysql Instance {{ $labels.instance }} down"
description: "mysql {{ $labels.instance }} has been down for more than 20s."