Prometheus+Grafana监控Mysql数据库

news/2024/9/12 23:17:13/文章来源:https://www.cnblogs.com/wanxun/p/18373388

安装配置

Prometheus是Go语言编写的,所以仅依赖二进制编译库,从官网根据操作系统下载对应的二进制库:https://prometheus.io/download/
解压到/usr/local/prometheus目录下

tar -xzvf prometheus-2.45.2.linux-amd64.tar.gz
mv prometheus-2.45.2.linux-amd64 /usr/local/prometheus

其中prometheus.yml是其配置文件,其内容如下所示

# 全局配置
global:scrape_interval: 15s # 设置采集信息的间隔,默认一分钟evaluation_interval: 15s # 设置评估数据的间隔,默认一分钟# scrape_timeout 采集超时时间默认10s.# 报警设置
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# 规则文件,一次性加载后间隔固定时间会对监测数据进行评估
rule_files:# - "first_rules.yml"# - "second_rules.yml"# 设置采集数据的来源
scrape_configs:# 定义采集任务的名字- job_name: "prometheus"# 定义数据来源,由于prometheus自己会在9090端口暴露自己的监测数据,因此可以通过如下路径采集自身监测数据# 默认采用http协议,数据路径参数metrics_path默认为'/metrics',因此可以从http://localhost:9090/metrics得到监测数据static_configs:- targets: ["localhost:9090"]

添加系统prometheus服务,在启动命令中指定配置文件和数据目录

vim /etc/systemd/system/prometheus.service[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus/prometheus \--config.file /usr/local/bin/prometheus/prometheus.yml \--storage.tsdb.path /usr/local/bin/prometheus/data[Install]
WantedBy=multi-user.target

指定使用上述配置文件启动应用

# 添加用户
useradd --no-create-home --shell /bin/false prometheus
# 为用户赋予文件夹权限
chown -R prometheus:prometheus /usr/local/bin/prometheus# 重新加载system服务
systemctl daemon-reload
# 设置开机启动
systemctl enable prometheus.service
# 启动
systemctl start prometheus
# 查看状态
systemctl status prometheus

通过http://localhost:9090端口可以看到prometheus的管理页面

Grafana

Grafana: https://grafana.com/

Grafana是一个跨平台的开源的度量分析和可视化工具,支持从多种数据源(如prometheus)获取数据进行可视化数据展示。

下载页面:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

CentOS可以通过yum命令直接安装

sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3-1.x86_64.rpm

默认安装在/usr/share/grafana目录下

配置

配置文件在/etc/grafana/grafana.ini ,其中服务的协议、域名、端口的配置如下

#################################### Server ####################################
[server]
# Protocol (http, https, h2, socket)
protocol = http# This is the minimum TLS version allowed. By default, this value is empty. Accepted values are: TLS1.2, TLS1.3. If nothing is set TLS1.2 would be taken
;min_tls_version = ""# The ip address to bind to, empty will bind to all interfaces
;http_addr =# The http port  to use
http_port = 3000# The public facing domain name used to access grafana from a browser
domain = localhost# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
root_url = %(protocol)s://%(domain)s:%(http_port)s/

启动

通过如下命令启动Grafana

sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server

之后访问上面配置的3000端口就可以看到Grafana页面,第一次登陆默认用户名和密码都是admin

监控mysql

为mysql数据库创建一个exporter账户

# 切换到自带的权限管理数据库
use mysql; 
# 创建work帐号,同时设置密码   
CREATE USER 'exporter'@'%' IDENTIFIED BY 'Exporter1234!';
# 分配权限
grant SELECT,UPDATE,INSERT,DELETE on *.* To 'exporter'@'%';
# 刷新使配置生效
flush privileges;

从prometheus官网下载mysqld_exporter,之后解压并启动即可

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gztar xvzf mysqld_exporter-0.15.1.linux-amd64.tar.gz 
mv mysqld_exporter-0.15.1.linux-amd64 /usr/local/bin/mysqld_exporter

在当前目录下编辑配置文件.my-exporter.cnf

[client]
user=exporter
password=Exporter1234!
host=localhost
port=3306

根据配置文件启动mysqld_exporter,并将数据暴露到9104端口,并且通过参数指定暴露的数据

./mysqld_exporter --web.listen-address=localhost:9104 --config.my-cnf=/usr/bin/mysqld_exporter/.my-exporter.cnf

也可以注册到系统服务,

vim /etc/systemd/system/mysqld_exporter.service[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
User=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/bin/mysqld_exporter/.my-exporter.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target

以服务的方式进行启动管理

# 添加用户
useradd --no-create-home --shell /bin/false mysqld_exporter
# 为用户赋予文件夹权限
chown -R mysqld_exporter:mysqld_exporter /usr/local/bin/mysqld_exporter# 重新加载系统配置
systemctl daemon-reload
# 开机启动
systemctl enable mysqld_exporter.service
# 启动服务
systemctl start mysqld_exporter
# 查看状态
systemctl status mysqld_exporter

看到mysqld_exporter启动成功,默认在9104端口,通过http://localhost:9104/metrics可以看到采集到的数据
修改prometheus配置文件信息并重启prometheus

scrape_configs:# 添加作业并命名- job_name: 'mysql'# 静态添加nodestatic_configs:# 指定监控端- targets: ['47.98.138.176:9104']

在Grafana创建Dashboard可视化地观测数据,这里可以选择监控模版来显示mysql的关键指标,模版ID为7362
image

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/785304.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1-数据操作数据

torch.cat操作广播机制tensor和numpy的转换对NA数据的处理

SOA架构开发小助手PAVELINK.SOA-Converter V1.4.2新版本发布

PAVELINK.SOA-Converter转换工具,用于衔接基于SOA的控制器设计、开发及测试过程中所常见的各类软件工具。PAVELINK.SOA-Converter能提供IDL及服务矩阵等文件的语法及规则检查,自动化完成多种不同格式IDL文件之间的转换,以及其它常用的各类型格式文件转换。PAVELINK.SOA-Conv…

Mysql锁查看

查看InnoDB的锁情况 SHOW ENGINE INNODB STATUS;命令会返回一个包含详细InnoDB引擎状态的报告,其中包含当前锁的详细信息。在输出的 LATEST DETECTED DEADLOCK 部分找到死锁信息,并在 TRANSACTIONS 部分找到当前的锁等待信息。 查看 MyISAM 锁情况 -- 方法一: 查看表锁 SHOW …

GPT4SM论文阅读笔记

Are GPT Embeddings Useful for Ads and Recommendation?论文阅读笔记 Abstract 现存的问题: ​ 尽管 LLMs 潜力巨大,但关于其文本嵌入是否能帮助广告和推荐服务的讨论却十分有限。 提出方法: ​ 为了探索 GPT 嵌入在广告和推荐中的应用,我们提出了三种策略,将 LLMs 的知…

题解:P10892 SDOI2024

题解:P10892 SDOI2024 题目传送门 题目思路 通过阅读题面,我们可以看出,其实对于每一次纠结,如果交出了 \(\frac{n-1}{2}\) 只猫猫,则剩下的为 \(\frac{n+1}{2}\) 只猫猫;如果交出了 \(\frac{n+1}{2}\) 只猫猫,则剩下的为 \(\frac{n-1}{2}\) 只猫猫。 为了使纠结的次数尽…

转载方法

博客园:搜cnblogsCSDN:搜article_content

python开发环境安装-包含Anaconda的安装配置和pycharm的安装

一、 需要得安装包 1、 Anaconda3-5.3.0-Windows-x86_64.exe python环境 2、pycharm-professional-2021.2.2.exe 开发工具 3、ide-eval-resetter-2.1.13.zip 破解工具 二、Anaconda安装 Anaconda,中文大蟒蛇,是一个开源的Python发行版本。 1、获取方式(免…

春秋云镜 Delegation

春秋云镜 Delegation现用fscan扫一下入口机发现是cmseasy,在/admin路由处可以登录,弱密码admin123456 这里存在CVE漏洞,写个马进去get: /index.php?case=template&act=save&admin_dir=admin&site=default post: sid=#data_d_.._d_.._d_.._d_11.php&slen=693&a…

企业市值排名3D可视化,重塑商业版图新维度

在这个数据驱动的时代,每一个数字背后都蕴藏着无限的可能与机遇。企业市值,作为衡量企业综合实力与市场认可度的关键指标,其动态变化不仅是投资者关注的焦点,也是全球商业竞争格局的晴雨表。当枯燥的数据表格被转化为生动的3D场景,全球数千家企业的市值排名不再只是冷冰冰…

微信小程序echarts-饼状图

为了兼容小程序 Canvas,我们提供了一个小程序的组件,用这种方式可以方便地使用 ECharts。 首先,下载 GitHub 上的 ecomfe/echarts-for-weixin 项目。一、封装pieChart组件 pieChart.wxml:<view class="container"><ec-canvas id="mychart-dom-bar&…

【Python脚本】刚度矩阵格式转换

一个Python脚本,用来将一个刚度转换到不同的商业软件,进行二次开发对于1-2-3坐标系:应力矩阵如下: \[\left.[\sigma]=\left[\begin{array}{ccc}\sigma_{11}&\sigma_{12}&\sigma_{13}\\\sigma_{12}&\sigma_{22}&\sigma_{23}\\\sigma_{13}&\sigma_{23}&…

DP斜率优化学习笔记

最后一次修改:2024.7.16 14:39 P.M By 哈哈铭 简介 “斜率优化”顾名思义就是用斜率进行优化,让 \(DP\) 的时间复杂度更优。 一般情况下,将动态转移方程化简后得到这样的关系式: \[\frac{y_1-y_2}{x_1-x_2} \leq K \]然后通过该式进行转移,以达到优化时间复杂度的目的。 小…