prometheus + grafana 监控Mysql、主机、Redis超简入门


grafana

安装参考 https://grafana.com/grafana/download?pg=get&platform=linux&plcmt=selfmanaged-box1-cta1
快速开始 https://grafana.com/docs/grafana/latest/getting-started/getting-started/

安装

Red Hat, CentOs , RPM安装参考

# 下载安装
wget https://dl.grafana.com/oss/release/grafana-8.0.6-1.x86_64.rpm
sudo yum install grafana-8.0.6-1.x86_64.rpm

# 启动停止管理
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server

# 将 Grafana 服务器配置为在引导时启动:
sudo systemctl enable grafana-server

启动

默认使用3000端口, 用户名和密码默认admin,登陆后修改密码

在这里插入图片描述

如何导入需要的dashboard

https://grafana.com/grafana/dashboards/

可以在线搜索需要的监控大盘,然后导入即可。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


导入选择数据源,至于这个prometheus数据源怎么出来的,后面介绍,因为后面加监控都必须加监控大盘,这里提前说怎么搜索添加大盘

在这里插入图片描述
这样一个大盘就添加好了,然后回到dashboard主页就能看到新添加的dashboard了
在这里插入图片描述

prometheus

https://prometheus.io/

安装

https://prometheus.io/download/

到下载页面下载对应平台和版本的压缩包

mkdir -p /opt/promutheus
cd /opt/promutheus
# 配合github的加速插件获取下载的加速地址
wget https://github.91chi.fun//https://github.com//prometheus/prometheus/releases/download/v2.33.5/prometheus-2.33.5.linux-amd64.tar.gz
tar -zxvf prometheus-2.33.5.linux-amd64.tar.gz
cd prometheus-2.33.5.linux-amd64/
# 后台启动
nohup ./prometheus &

为了方便管理进程,使用supervisor来管理prometheus进程的配置文件,不使用的话用上面的命令启动也行

; 这个是注释标识
[program:prometheus]; 进程名
command=/opt/prometheus/prometheus-2.33.5.linux-amd64/./prometheus
; 生成子进程时的工作目录
directory=/opt/prometheus/prometheus-2.33.5.linux-amd64/
; 自动重启
autostart=true
autorestart=true
; 用哪个用户启动
; user=
; 日志文件
stdout_logfile=/opt/prometheus/prometheus-2.33.5.linux-amd64/prometheus.log
; 重定向错误日志到stdout
redirect_stderr=true
; 日志级别
loglevel=info

访问http://localhost:9090/ 进入到默认界面
在这里插入图片描述

访问http://localhost:9090/metrics 查看所有监控指标,后面添加的exporter都可以在此查看
在这里插入图片描述

集成grafana

在grafana界面中添加prometheus的datasource,填写上面的访问地址localhost:9090, 其他默认即可。<br /在这里插入图片描述

切换到Dashboards界面,导入prometheus的监控大盘

在这里插入图片描述

然后看到一个特别好看但是不知道干啥用的界面, 到这里prometheus的数据源就添加完成了,后续所有使用的exporter都会将数据拉取到这个数据源里
在这里插入图片描述

Exporter

exporter使用Pull的方式将指定监控资源的指标拉取到prometheus数据源中

还有一种push的方式,未去了解。

mysqld_exporter

https://github.com/prometheus/mysqld_exporter

安装配置
mkdir -p /opt/prometheus/exporter
cd /opt/prometheus/exporter
# 配合github的加速插件获取下载的加速地址
wget https://github.91chi.fun//https://github.com//prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz

创建账号

CREATE USER 'exporter'@'%' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
  • 使用环境变量方式配置一下数据库连接认证信息并启动 Exporter
export DATA_SOURCE_NAME='exporter:password@(localhost:3306)/'
cd /opt/prometheus/exporter/mysqld_exporter-0.14.0.linux-amd64
nohup ./mysqld_exporter &

  • 使用配置文件,在当前机器~/.my.cnf配置文件中增加配置信息,注意修改自己实际情况参数。注意这个.my.cnf在exporter所在机器创建就行,和mysql实际自己的配置文件my.cnf没有一点关系
[client]
   host=106.75.227.xxx
   port=3306
   user=exporter
   password=xxxxx

使用supervisor来管理进程的配置文件

; 这个是注释标识
[program:mysqld_exporter]; 进程名
command=/opt/prometheus/exporter/mysqld_exporter-0.14.0.linux-amd64/./mysqld_exporter
; 生成子进程时的工作目录
directory=/opt/prometheus/exporter/mysqld_exporter-0.14.0.linux-amd64/
; 自动重启
autostart=true
autorestart=true
; 用哪个用户启动
; user=
; 日志文件
stdout_logfile=/opt/prometheus/exporter/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter.log
; 重定向错误日志到stdout
redirect_stderr=true
; 日志级别
loglevel=info

在这里插入图片描述


export启动成功后,注意控制台输出,默认端口是9104, 访问localhost:9104/metrics 即可查看mysql相关指标

修改Prometheus的配置文件prometheus.yml 增加 mysql 相关 exporter job

cd /opt/prometheus/prometheus-2.33.5.linux-amd64
vim prometheus.yml
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: ["localhost:9090"]

  # 增加抓取mysql指标的exporter
  - job_name: 'mysql'
    static_configs:
     - targets: ['localhost:9104']
       labels:
          instance: mysql # 这个会在grafana中显示用

杀掉prometheus的进程,然后重新启动, 进入prometheus的web界面,再次选择targets, 可以看到新加的mysql端点
在这里插入图片描述

然后导入mysql的监控大盘, 可以自己再grafana官网搜索自己需要的,这里选择的是https://grafana.com/dashboards/7362
在这里插入图片描述

在这里插入图片描述

效果图如下
在这里插入图片描述

在这里插入图片描述

node_exporter

监控主机

https://github.com/prometheus/node_exporter

mkdir -p /opt/prometheus/exporter/
# 通过加速插件获得的下载地址
wget https://github.91chi.fun//https://github.com//prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz
cd node_exporter-1.3.1.linux-amd64
nohup ./node_exporter &

使用supervisor来管理进程的配置文件

; 这个是注释标识
[program:node_exporter]; 进程名
command=/opt/prometheus/exporter/node_exporter-1.3.1.linux-amd64/./node_exporter
; 生成子进程时的工作目录
directory=/opt/prometheus/exporter/node_exporter-1.3.1.linux-amd64/
; 自动重启
autostart=true
autorestart=true
; 用哪个用户启动
; user=
; 日志文件
stdout_logfile=/opt/prometheus/exporter/node_exporter-1.3.1.linux-amd64/node_exporter.log
; 重定向错误日志到stdout
redirect_stderr=true
; 日志级别
loglevel=info

在这里插入图片描述

修改Prometheus的配置文件prometheus.yml 增加 mysql 相关 exporter job

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # 增加抓取主机指标的exporter
  - job_name: 'host'
    static_configs:
      - targets: ['localhost:9100']
        labels:
          instance: host

导入grafana监控大盘https://grafana.com/grafana/dashboards/1860

效果图如下
在这里插入图片描述
在这里插入图片描述

redis_exporter

https://github.com/oliver006/redis_exporter

mkdir -p /opt/prometheus/exporter/
# 通过加速地址下载
wget https://github.com/oliver006/redis_exporter/releases/download/v1.36.0/redis_exporter-v1.36.0.linux-amd64.tar.gz
tar -zxvf redis_exporter-v1.36.0.linux-amd64.tar.gz
cd redis_exporter-v1.36.0.linux-amd64/
# 后台启动
nohup ./redis_exporter --redis.password=123456 &

使用supervistor管理进程

[program:redis_exporter]; redis_exporter
command=/opt/prometheus/exporter/redis_exporter-v1.36.0.linux-amd64/./redis_exporter --redis.password=redis密码
; 自动重启
autostart=true
autorestart=true
; 日志文件
stdout_logfile=/opt/prometheus/exporter/redis_exporter-v1.36.0.linux-amd64/redis_exporter.log
; 重定向错误日志到stdout
redirect_stderr=true
; 日志级别
loglevel=info

在这里插入图片描述

配置prometheus.yml

如下是支持监控多个redis主机的配置, 密码必须为同一个,配置在export的启动命令上,通过--redis.password指定,如果密码不一致,只能配置多个exporter了, 这个从README中抄的,还是有一点没有看懂,仅供参考

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:  
  # 增加抓取redis指标的exporter
  - job_name: 'redis'
    static_configs:
       - targets:
          - redis://localhost:6379
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9121

  ## config for scraping the exporter itself
  - job_name: 'redis_exporter'
    static_configs:
      - targets:
        - localhost:9121
      labels:
          instance: redis

在grafana中导入监控大盘, https://grafana.com/grafana/dashboards/763
在这里插入图片描述

效果图如下
拿不到内存占用,不知道为啥
在这里插入图片描述

在这里插入图片描述

### 配置 Grafana监控 MySQL 性能 #### 数据源设置 为了使 Grafana 能够监控 MySQL 的性能,首先需要配置 PrometheusMySQL Exporter 来收集 MySQL 的指标数据。以下是具体操作: 1. **安装 MySQL Exporter**: MySQL Exporter 是一个用于导出 MySQL 指标的工具,它会将这些指标暴露给 Prometheus 进行抓取。可以通过以下命令安装 MySQL Exporter[^2]: ```bash [root@prometheus_exporters ~]# git clone https://github.com/prometheus/mysqld-exporter.git ``` 2. **启动 MySQL Exporter**: 使用 `mysqld_exporter` 命令来运行该程序,并指定 MySQL 用户名和密码文件路径: ```bash ./mysqld_exporter --config.my-cnf=/path/to/my.cnf ``` 此处 `/path/to/my.cnf` 文件应包含访问 MySQL 所需的用户名和密码。 3. **配置 Prometheus 抓取 MySQL 指标**: 编辑 Prometheus 的配置文件 (`prometheus.yml`) 并添加如下内容以定义目标主机上的 MySQL Exporter 地址: ```yaml scrape_configs: - job_name: 'mysql' static_configs: - targets: ['localhost:9104'] ``` 然后重启 Prometheus 服务使其生效。 4. **在 Grafana 中添加数据源**: 登录到 Grafana Web UI (默认端口为 3000),导航至 “Configuration -> Data Sources”,点击 “Add data source”。选择 Prometheus 类型的数据源并填写 URL 字段指向本地或远程的 Prometheus 实例地址[^3]。 #### 指标展示最佳实践 完成上述步骤之后,在仪表板上可以创建面板显示各种重要的 MySQL 绩效度量标准。一些推荐的关键指标包括但不限于以下几个方面: 1. **查询统计信息**: - 查询延迟(`query_response_time_total`) - 每秒执行次数(`com_select`, `com_insert`, etc.) 2. **连接池状态**: - 当前活跃线程数(`threads_running`) - 已建立的最大并发连接数量(`max_used_connections`) 3. **缓存命中率**: - Query Cache Hit Rate(`Qcache_hits / (Qcache_hits + Qcache_inserts)`)[^1] 通过拖拽预设模板或者手动构建自定义图表的方式呈现以上提到的各项数值变化趋势图谱形式便于直观理解当前数据库健康状况以及发现潜在瓶颈所在位置。 ```python import requests response = requests.get('http://localhost:3000/api/datasources') if response.status_code == 200: print("Data Source Configured Successfully.") else: print(f"Error configuring data source: {response.text}") ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值