4

【Prometheus+Grafana系列】监控MySQL服务 - immaxfang

 1 year ago
source link: https://www.cnblogs.com/immaxfang/p/16631839.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前面的一篇文章已经介绍了 docker-compose 搭建 Prometheus + Grafana 服务。当时实现了监控服务器指标数据,是通过 node_exporter。Prometheus 还可用来监控很多服务,比如常见的 MySQL。本文就介绍如何通过 mysqld_exporter 来监控 MySQL 指标。

下载安装包

cd /optwget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gztar xvf mysqld_exporter-0.14.0.linux-amd64.tar.gzmv mysqld_exporter-0.14.0.linux-amd64 mysqld_exportermv /opt/mysqld_exporter /usr/local/

创建监控账号并授权

在需要监控的mysql上创建账号并授权。

# 创建用户CREATE USER 'prometheus'@'%' IDENTIFIED BY 'prometheus';# 分配权限GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'%';

添加数据库监控账号配置

vim /usr/local/mysqld_exporter/.my.cnf
添加如下内容

[client]user=prometheuspassword=prometheusport=3306

启动exporter客户端

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

先手动启动exporter看一下日志,若有错误根据输出调整即可。手动运行没问题后,则进行下一步将其添加到系统服务中。

添加到系统服务

vim /etc/systemd/system/mysqld_exporter.service
添加如下内容

[Unit]Description=mysqld_exporterAfter=network.target[Service]ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnfRestart=on-failure[Install]WantedBy=multi-user.target

加载并重启服务

# 加载配置systemctl daemon-reload# 启动服务systemctl restart mysqld_exporter.service# 查看服务状态systemctl status mysqld_exporter.service# 配置开机启动systemctl enable mysqld_exporter.service

查看收集数据

访问exporter服务地址,查看数据收集情况。

curl http://192.168.2.192:9104/metrics

修改 prometheus 配置文件,添加新节点

修改 prometheus 下的配置,prometheus.yml,添加如下内容。

scrape_configs: # 添加job - job_name: 'mysql-192' static_configs: # 配置监控端,即上面我们启动的 mysqld_exporter 服务 - targets: ['192.168.2.192:9104'] labels: instance: mysql

重启 prometheus 服务

上一步修改了 prometheus.yml,需要重启下 prometheus 服务。

cd /var/workspace/docker-prometheusdocker-compose stop prometheusdocker-compose up -d --build prometheus

查看 prometheus 中服务添加情况

查看 targets 是否添加成功

查看 mysql 监控信息

mysql_global_status_aborted_clients

在 grafana 中添加 data sources

添加 prometheus 服务地址,此处由于服务是基于 docker-compose 构建的,没有填写ip,直接填写服务名即可。

在 grafana 中导入 mysql 监控

输入官方模版 id,7362,点击 load。然后按照下图选择确认即可。

上一步导入成功后,会自动跳转到监控面板页面,如下图。默认的格式已经非常丰富可以直接使用了,也可以根据自己需求调整位置和配置。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK