暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Prometheus+Grafana 监控 MySQL

DevOps实战 2021-12-21
323

作者简介

马听,多年 DBA 实战经验,对 MySQL、 Redis、ClickHouse 等数据库有一定了解,专栏《一线数据库工程师带你深入理解 MySQL》、《Redis 运维实战》作者。


这一节内容来聊聊通过 Prometheus 获取 MySQL 的监控数据,并通过 Grafana 展示的过程。首先来看整体架构图:

1 架构图

如上图,通过 mysql_exporter 获取 MySQL 的监控数据,通过 node_exporter 获得 Linux 服务器的监控数据。将获得的监控数据传到 Prometheus 中,最终通过 Grafana 展示出来,效果如下图:


2 实验环境介绍

实验环境大致如下:
  • 被监控的 MySQL 机器:192.168.150.123(MySQL 版本:8.0.22);

  • Prometheus 服务器:192.168.150.253(Prometheus 版本:2.25.2);

  • Grafana 服务器:192.168.21.126(Grafana 版本:7.4.5);

  • 服务器版本均为:CentOS 7.4;

  • 防火墙、Selinux 均关闭。


3 部署 Prometheus

https://prometheus.io/download/ 中下载对应的版本。
解压 Prometheus 安装包
‍‍‍‍‍‍‍‍
tar zxvf prometheus-2.25.2.linux-amd64.tar.gz -C /opt
复制

创建软链接
ln -s /opt/prometheus-2.25.2.linux-amd64/ /opt/prometheus
复制

修改配置文件 /opt/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).


# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093


# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"


# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'


# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.


static_configs:
- targets: ['192.168.150.253:9090']
复制
这里只对 scrape_configs(被监控机器的配置) 中的 static_configs 进行了修改,targets 中的内容改成了 prometheus 所在机器的 IP 加端口。也就是暂时配置只监控本机。



启动 Prometheus
nohup /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &
复制
访问 Prometheus 页面:http://192.168.150.253:9090/
可以看到如下界面:

点击 Status --> Targets,可以看到被监控的目标机器,如下:
点击对应的 Endpoint,则可获得监控数据,如下图:
到这里,Prometheus 已经部署完成。

4 部署 node_exporter 组件

node_exporter 是 Prometheus 用户获取 Linux 指标的插件。Github 地址:https://github.com/prometheus/node_exporter。这里聊聊它的部署。
首先在 https://prometheus.io/download/ 中找到对应的 node_exporter 版本。
将 node_exporter 传到需要监控的 MySQL 机器上,进行解压:

复制
tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /opt/
复制


创建软链接:

ln -s /opt/node_exporter-1.1.2.linux-amd64/ /opt/node_exporter
复制
启动
nohup /opt/node_exporter/node_exporter &
复制

浏览器输入:http://192.168.150.123:9100/metrics,如果有类似如下数据,则表示 node_exporter 部署完成。


5 部署 mysqld_exporter 组件

mysqld_exporter 是 Prometheus 的 MySQL 指标导出插件。Github 地址:https://github.com/prometheus/mysqld_exporter。这里来聊聊它的部署。
首先在 https://github.com/prometheus/mysqld_exporter/releases 中找到对应的 mysqld_exporter 版本。
将 mysqld_exporter 传到需要监控的 MySQL 机器上,进行解压:
tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt
复制

创建软链接
ln -s /opt/mysqld_exporter-0.12.1.linux-amd64/ /opt/mysqld_exporter
复制

在 MySQL 上创建监控用户
create user 'exporter'@'localhost'  IDENTIFIED BY 'eXpIHB666QWE!';


GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'exporter'@'localhost';
复制

新建一个配置文件:
vim /opt/mysqld_exporter/mysqld_exporter.cnf
复制
配置 MySQL 监控用户信息:
[client]
user=exporter
password=eXpIHB666QWE!
复制
启动 mysqld_exporter


nohup /opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/mysqld_exporter.cnf &
复制

浏览器输入:http://192.168.150.123:9104/metrics,可获得 MySQL 监控数据,如下图(部分数据):


6 配置 Prometheus 获取监控数据

在 prometheus 的机器上,修改 prometheus 的配置文件 /opt/prometheus/prometheus.yml,增加 node_exporter 和 mysqld_exporter 的配置:


# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).


# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093


# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"


# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'


# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.


static_configs:
- targets: ['192.168.150.253:9090']
- job_name: 'mysql-123'
static_configs:
- targets: ['192.168.150.123:9104']
- job_name: 'node-123'
static_configs:
- targets: ['192.168.150.123:9100']
复制
重启 prometheus
pkill prometheus
nohup /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &
复制

访问 Prometheus 页面:http://192.168.150.253:9090/,点击 Status --> Targets,可以看到新增加的被监控节点。

回到主界面,搜索 MySQL 相关参数,比如:innodb_buffer_pool_size:

选择对应的参数,则可看到监控图,如下:


7 部署 Grafana

Grafana 的 Github 地址:https://github.com/grafana/grafana
可以在 https://grafana.com/grafana/download 中选择适合自己操作系统的包。然后下载对应的包,如下:
wget https://dl.grafana.com/oss/release/grafana-7.4.5-1.x86_64.rpm
yum install grafana-7.4.5-1.x86_64.rpm -y
复制

启动 Grafana
systemctl start grafana-server.service
复制

登录 Grafana  WEB 界面:http://192.168.21.126:3000/,显示如下:

用户名密码都是 admin。登录后,会让我们修改密码,则按提示操作即可,当然也可以点击跳过。

这里就可以看到 Grafana 的主界面了:


8 为 Grafana 配置 Prometheus 数据源

按下图进入数据源添加界面:
点击 “Add data source”

如下图,选择“Prometheus”数据源:
如下图,增加 Prometheus 的 URL 即可:
点击最下方的 Save & Test,显示“Data source is working”,则表示数据源配置正常:


9 Grafana 展示 Linux 的监控数据

按照下图的方式进入模板导入界面:
在 “Import via grafana.com” 下方输入:11074
点击 Load 会出现如下界面:
将 Name 改成你希望定义的名字,在 VictoriaMetrics 位置选择之前创建的 Prometheus 数据源,如下图:
点击 “Import”,会自动跳转到如下界面:
到这里,完成了 Grafana 展示 Prometheus 中 Linux 操作系统的监控数据。

10 Grafana 展示 MySQL 的监控数据

按照下图的方式进入模板导入界面:
在 “Import via grafana.com” 下方输入 7362:
或者在 https://grafana.com/grafana/dashboards/7362 页面下载 JSON 模板,然后点击 “Upload JSON file” 导入,然后会显示如下信息(目前还有其他一些模板,比如 https://github.com/percona/grafana-dashboards,有兴趣的可以尝试一下):
在 prometheus 选项选择之前创建的 Prometheus 数据源,点击 “Import”,会自动跳转到如下界面:
到这里,完成了 Grafana 展示 Prometheus 中 MySQL 的监控数据。

文章转载自DevOps实战,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论