Prometheus监控系统(6)使用mysqld_exporter监控MySQL

Tanglu Prometheus 2019-07-24 12485 0

mysqld_exporter是Prometheus用于监控MySQL指标的一个导出器,支持对MySQL 5.5以上进行监控。下面是mysqld_exporter的安装与配置:

1、登录MySQL,创建一个账号用于Prometheus监控获取数据

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';


2、在Prometheus官网下载并解压mysqld_exporter,在启动mysqld_exporter前需要配置好数据库信息,有2种实现方式:一个是将数据库信息写入到环境变量中,从而让exporter能读取到MySQL的信息;另一种是写入到一个配置文件中,在运行mysqld_exporter时指定该配置文件。这里推荐使用配置文件的形式,便于查阅

vi /usr/local/mysqld_exporter/my.cnf
[client]
host=192.168.94.10  #mysql服务地址
port=3306
user=exporter  #这里的用户就是第一步设置的用户
password=123456  #


3、启动mysqld_exporter,默认监听在9104端口

/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/my.cnf


4、在Prometheus增加相关监控配置,然后热加载服务

scrape_configs:
  - job_name: 'prometheus-server'
    static_configs:
    - targets: ['192.168.94.10:9090']
  - job_name: 'mysql'
    static_configs:
    - targets: ['192.168.94.10:9104']                                  

kill -HUP `pidof prometheus`



5、为mysqld_exporter创建一个启动脚本,以便使用systemctl来管理mysqld_exporter

[Unit]
Description=mysqld_exporter
[Service]
Type=simple
Restart=on-failure
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/my.cnf
[Install]
WantedBy=multi-user.target


systemctl daemon-reload
systemctl start mysqld_exporter.service


评论