Prometheus+Grafana普罗米修斯搭建+监控MySQL

news/2025/2/1 17:56:44/

Prometheus+Grafana普罗米修斯搭建+监控MySQL


一,Prometheus

1.什么是Prometheus?

PrometheusCloud Native Computing Foundation 的一个监控系统项目, 集采集、监控、报警等特点于一体。

Prometheus主要受启发于GoogleBrogmon监控系统, 从2012年开始由前Google工程师在Soundcloud以开源软件的形式进行研发,2017年底发布了基于全新存储层的2.0版本,当前最新版本是2.44.0版本。

2.Prometheus架构

请添加图片描述

3.prometheus具有那些特点?

  • 多维数据模型(由指标名称、键/值组合的时间序列);
  • 提供了一种强大而灵活的查询语言promsql;
  • 没有对分布式存储的依赖,单个服务器节点是自主的;
  • 主要支持时间序列集合的HTTP拉模模型,同时也提供PushGateway来满足;
  • 服务发现;
  • 自带UI,支持丰富多种图形和仪表板,还能与其他;
  • 支持分层和水平联合;

二,Prometheus搭建

IP角色
192.168.2.4prometheus服务器端
192.168.2.3node_exporter客户端

1.二进制安装Prometheus

[root@server ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.44.0/prometheus-2.44.0.linux-amd64.tar.gz
[root@server ~]# tar zxf prometheus-2.44.0.linux-amd64.tar.gz
[root@server ~]# mv prometheus-2.44.0.linux-amd64 /usr/local/prometheus
1.查看版本号
[root@server ~]# cd /usr/local/prometheus/
[root@server prometheus]# ./prometheus --version
prometheus, version 2.44.0 (branch: HEAD, revision: 1ac5131f698ebc60f13fe2727f89b115a41f6558)build user:       root@739e8181c5dbbuild date:       20230514-06:18:11go version:       go1.20.4platform:         linux/amd64tags:             netgo,builtinassets,stringlabels
2.查看帮助文档
[root@server prometheus]# ./prometheus --help

2.prometheus.yml配置解释

# my global config
global:# 默认情况下,每15s拉取一次目标采样点数据。scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.# 每15秒评估一次规则。默认值为每1分钟。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:# job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上- job_name: 'prometheus'# 覆盖global的采样点,拉取时间间隔5sscrape_interval: 5sstatic_configs:- targets: ['localhost:9090']

3.升级为系统服务

# 启动服务
cd /usr/lib/systemd/system
vi prometheus.service[Unit]Description=https://prometheus.io[Service]Restart=on-failureExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090[Install]                      WantedBy=multi-user.target#保存退出

其他选项解释:

常用选项解释:
# 指定配置文件
--config.file="prometheus.yml"
# 默认指定监听地址端口,可修改端口
--web.listen-address="0.0.0.0:9090" 
# 最大连接数
--web.max-connections=512
# tsdb数据存储的目录,默认当前data/
--storage.tsdb.path="data/"
# premetheus 存储数据的时间,默认保存15天
--storage.tsdb.retention=15d 
# 通过命令热加载无需重启 curl -XPOST 192.168.2.45:9090/-/reload
--web.enable-lifecycle
# 可以启用 TLS 或 身份验证 的配置文件的路径
--web.config.file=""启动选项了解:./prometheus --help

4.刷新system文件,启动

systemctl daemon-reload
systemctl start prometheus

5.访问测试

IP:9090
请添加图片描述

三,客户端node_exporter搭建

1.监控目的Linux安装node_exporter

[root@server ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
[root@server ~]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@server ~]# cd /usr/local/
[root@server ~]# mv node_exporter-1.1.2.linux-amd64/ node_exporter

2.添加为系统服务

[root@server ~]# vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target [Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure[Install]
WantedBy=multi-user.target# 启动node_exporter
systemctl daemon-reload
systemctl start node_exporter

3.prometheus服务器端添加监控项

[root@server prometheus]# cat 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:
scrape_configs:- job_name: 'prometheus'static_configs:- targets: ['192.168.2.4:9090']- job_name: 'linux'static_configs:- targets: ['192.168.2.4:9100','192.168.2.3:9100'] # 多个用,分开# 添加上面三行

4.重启系统普罗米修斯

[root@server ~]#  systemctl restart prometheus.service

5.监控界面

请添加图片描述

四,监控MySQL

1.安装mysqld-exporter

[root@VM_2-44 ~]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@VM_2-44 ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
[root@VM_2-44 /usr/local]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
[root@VM_2-44 /usr/local/mysqld_exporter]# vi .my.cnf
[client]
host=192.168.2.3
user=root
password=123456
port=3306

2.启动mysqld-exporter服务

[root@VM_2-44 /usr/local/mysqld_exporter]# ./mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
[root@VM_2-44 /usr/local/mysqld_exporter]# ps -ef |grep exporter
root       3447   3398  0 01:31 pts/1    00:00:02 ./node_exporter
root       4647   3398  0 02:13 pts/1    00:00:00 ./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
root       4654   3398  0 02:13 pts/1    00:00:00 grep --color=auto exporter
[root@VM_2-44 /usr/local/mysqld_exporter]# ss -lntp |grep 4647
LISTEN     0      128         :::9104                    :::*                   users:(("mysqld_exporter",pid=4647,fd=3))
[root@VM_2-44 /usr/local/mysqld_exporter]# # 启动后会监听9104端口

3.普罗米修斯配置文件添加监控项

[root@VM_2-45 /usr/local/prometheus]# vi prometheus.yml - job_name: 'mysql'static_configs:- targets: ['192.168.2.3:9104']

4.重启普罗米修斯

[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus.service

5.查看状态

请添加图片描述

五,grafana展示prometheus数据

1.在prometheus安装grafana

wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/Packages/grafana-7.4.3-1.x86_64.rpm[root@VM_2-45 ~]# yum install initscripts fontconfig
[root@VM_2-45 ~]# yum install -y grafana-7.4.3-1.x86_64.rpm
[root@VM_2-45 ~]# systemctl start grafana-server.service 

2.访问grafana

启动后访问地址:ip:3000
初始用户名和密码都是admin
image-20230607175213818

3.添加Prometheus数据源

Configuration -> Data Sources ->add data source -> Prometheus

请添加图片描述

​ 添加prometheus服务器

请添加图片描述

4.添加dashboard Linux基础数据展示

请添加图片描述

​ 导入模板8919

请添加图片描述

​ 选择数据源

请添加图片描述

5.查看dashboard

Dashboards ->Manage

请添加图片描述

六,grafana展示MySQL信息

1.设置数据源

Configuration -> Data Sources ->add data source -> MySQL

请添加图片描述

2.数据库上授权用户

请添加图片描述

3.导入下载的dashboard,数据源现在刚刚创建的

https://pan.baidu.com/s/1GBzogDLsYS3IvwH4WbdPLw 提取码:ef6e

请添加图片描述

4.查看效果

请添加图片描述


http://www.ppmy.cn/news/295815.html

相关文章

MyBatis操作数据库实现

说明:MyBatis是作用于三层架构开发,数据访问层(Data Access Object)的框架,用于访问数据库,对数据进行操作。 一、环境搭建 首先,创建一个SpringBoot模块,然后把MyBatis的环境搭建…

【全栈开发】基于Spring BootVueAndroid扫码授权登录

文章目录 一、引言二、设计1、移动端(Android)(1)库(2)依赖(3)使用 2、前端(Vue)(1)库(2)使用 3、后端&#x…

购买一套WMS仓储管理系统要多少钱

随着电商行业的快速发展,仓储物流行业也逐渐成为了人们关注的焦点。WMS仓储管理系统作为物流管理领域的重要工具,在提高仓库管理效率、降低运营成本方面具有重要作用。那么,购买一套WMS仓储管理系统要多少钱呢? 首先,我…

Ansible判断执行失败

比如我们要安装java,但如果已经安装了,就没必要再执行了,怎么实现呢? 如下图,把shell执行结果输出到变量 res - name: Check javashell: source /etc/profile && java -versionregister: resignore_errors: …

什么是AI业务流程质检,如何用它做好销售和服务过程监督

近几年,随着语音转写、语义理解和机器学习等技术的成熟,越来越多的企业开始部署基于AI技术的智能质检系统,来帮助坐席、销售和服务团队提高沟通质量管理能力,同时提升沟通中的客户体验。 不过,不论是最初的人工质检&a…

Spring框架-面试题核心概念

目录 1.Spring框架的作用是什么? 2. 什么是DI? 3.什么是AOP? 4.Spring常用注解 5.Spring中的设计模式 6.Spring支持的几种bean的作用域 7.Spring中Bean的生命周期? 8.Spring中的事务管理 9.Spring中的依赖注入方式有几种 10.Sprin…

Spark数据倾斜解决方案三:随机key双重聚合

什么是随机key双重聚合 随机Key双重聚合是指Spark分布式计算对RDD调用reduceByKey等聚合类Shuffle算子进行计算,使用对Key值随机数前缀的处理技巧,对Key值进行二次聚合。 第一次聚合(局部聚合):对每个Key值加上一个随机数,执行第一次reduceByKey聚合操作。第二次聚合(双…

从Android UI收集流的更安全方法

从Android UI收集流的更安全方法 在安卓应用中,通常从UI层收集Kotlin flows以显示屏幕上的数据更新。但是,为了确保不做过多的工作、浪费资源(包括CPU和内存)或在视图转到后台时泄漏数据,您需要收集这些flows。 在本…