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


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
tar zxvf prometheus-2.25.2.linux-amd64.tar.gz -C opt
复制
ln -s /opt/prometheus-2.25.2.linux-amd64/ /opt/prometheus
复制
# 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']
复制
nohup opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &
复制



4 部署 node_exporter 组件

复制
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 &
复制

5 部署 mysqld_exporter 组件
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
复制
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
复制
[client]
user=exporter
password=eXpIHB666QWE!
复制
nohup opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/mysqld_exporter.cnf &
复制

6 配置 Prometheus 获取监控数据
# 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']
复制
pkill prometheus
nohup opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &
复制



7 部署 Grafana
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
复制
systemctl start grafana-server.service
复制

用户名密码都是 admin。登录后,会让我们修改密码,则按提示操作即可,当然也可以点击跳过。
这里就可以看到 Grafana 的主界面了:

8 为 Grafana 配置 Prometheus 数据源




9 Grafana 展示 Linux 的监控数据





10 Grafana 展示 MySQL 的监控数据




11 告警配置
对 Prometheus 中获取的数据进行告警配置,目前有很多方式,比如:
通过 Grafana 配置邮件告警
通过开源的运维告警中心消息转发系统:PrometheusAlert,Github 地址:https://github.com/pandorasongs/PrometheusAlert。
等等

systemctl start grafana-server.service
复制


12 告警测试





A 对 A 类(A 是 Query 中定义的)指标; 5m,now 表示从 5 分钟前到现在; IS ABOVE 5 定义阈值的类型和阈值。





文章转载自悦专栏,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
墨天轮个人数说知识点合集
JiekeXu
440次阅读
2025-04-01 15:56:03
MySQL数据库当前和历史事务分析
听见风的声音
425次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
387次阅读
2025-03-28 16:28:31
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
355次阅读
2025-04-25 15:30:58
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
354次阅读
2025-04-15 23:49:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
353次阅读
2025-04-17 17:02:24
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
329次阅读
2025-04-15 14:48:05
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
316次阅读
2025-04-25 18:53:11
云和恩墨杨明翰:安全生产系列之MySQL高危操作
墨天轮编辑部
307次阅读
2025-03-27 16:45:26
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
294次阅读
2025-04-07 12:14:29