手把手教你Prometheus + Granafa实现mysql 性能监测部署

本文介绍了如何在Windows环境下搭建Prometheus和Grafana,用于MySQL数据库的性能监控。首先下载并启动Prometheus、mysqld_exporter和Grafana,然后配置Prometheus以收集mysqld_exporter的数据,解决启动和配置过程中遇到的问题。最后,通过Grafana可视化展示MySQL监控信息,实现类似阿里云和腾讯云的监控功能。
摘要由CSDN通过智能技术生成

        一、前言
         二、部署过程与问题记录
         1.prometheus下载与启动
         2.mysqld_exporter下载与启动
         3.grafana下载与启动
         4.运行Prometheus + Granafa步骤总结

一、前言

    数据库性能监控可以说是十分重要,能否自行搭建环境实现像阿里云或是腾讯云那样直观的展示不同维度数据的功能?答案是肯定的。下面详细说明一下安装部署过程以及过程中出现的问题,希望对你有所帮助!

二、部署过程与问题记录

    首先介绍一下用到的三个开源程序:
    prometheus:普罗米修斯可以简单理解为一个监控工具,以时间为单位展示指定数据维度的变化趋势。数据信息从哪来?主要是依赖数据采集器,对于mysql数据采集使用的是mysqld_exporter
    grafana:主要用于可视化展示的监控软件,让数据监控更直观,支持多种仪表盘类型,就好比经常见的数据大屏,仪表盘就是各种展示形式。下面分别讲下三个如何下载和安装。

1.prometheus下载与启动

    官方下载地址:
https://prometheus.io/download/
    本文主要讲解如何在windows上安装,所以选择windows版本,这里下载版本为:prometheus-2.40.1.windows-amd64.zip
解压之后找到prometheus.exe直接进行双击即可启动。
看到下面的内容说明启动成功:
在这里插入图片描述

查看启动端口:
在这里插入图片描述
浏览器中直接访问:localhost:9090
在这里插入图片描述

2.mysqld_exporter下载与启动

    官方下载地址同上:https://prometheus.io/download/,这里下载的版本为:
mysqld_exporter-0.14.0.windows-amd64.zip.解压之后需要添加my.cnf文件(注意:解压完成之后是不存在这个文件的,需要手动添加)。主要作用是配置数据库连接信息。my.cnf中添加内容如下:

[client]  
user=root  
password=****
host=192.125.256.12
prot=3308

启动方式:mysqld_exporter解压所在目录中执行(注意不是双击mysqld_exporter.exe启动):

mysqld_exporter.exe --config.my-cnf=my.cnf

在这里插入图片描述
访问地址:localhost:9104
收集的mysql信息如下:
在这里插入图片描述
prometheus中配置mysqld_exporter,两者进行关联,从prometheus.yml中最后位置添加mysqld_exporterprometheus.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: ["localhost:9090"]
   # 自定义添加mysql监控任务 
   - job_name: "mysql_monitor"

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

添加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: ["localhost:9090"]
   # 自定义添加mysql监控任务 
   - job_name: "mysql_monitor"

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

	# 监控mysql采集器
    static_configs:
      - targets: ["localhost:9104"]

配置好之后重启prometheus,将prometheus.exe运行窗口关闭,双击prometheus.exe即可。mysqld_exporter重启方式同理。访问localhost:9090,选择targets,正常情况下应该是有两条任务。
在这里插入图片描述
在这里插入图片描述

说下遇到的两个问题:
    关闭prometheus.exe然后重新启动出现闪退。
    出现这种情况一般是yml格式缩进问题,新增内容和原始文件的缩进保持一致即可.
    访问localhost:8090,右边error中显示Get "http://localhost:9104/metrics": dial tcp: lookup localhost on 114.114.114.114:53: no such host
    处理方式:将prometheus.yml中localhost修改为本机ip,也不要使用127.0.0.1.

3.grafana下载与启动

    下载地址:https://grafana.com/grafana/download,下载完成之后按照步骤安装即可,可以自定义安装目录,此处不做截图说明,安装完成之后访问:localhost:3000.这一步可能会出现问题,直接访问出现下图问题:
在这里插入图片描述
    说下当时的原因:
    本机3000端口被占用,所以访问失败,修改端口为3001。找到defaults.ini并打开,将从下面修改端口号即可。

# The http port to use
http_port = 3000

    浏览器问题:电脑上安装的谷歌、360浏览器访问都失败,下载了一个狐火浏览器问题解决。当然也试过其他方式比如上面说的重启(找到grafana-server.exe双击)。
在这里插入图片描述

    默认的用户名密码都是admin,登录之后会立即要求修改密码。重新登录之后设置数据源(按照截图步骤进行设置即可):
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

    设置仪表板:

在这里插入图片描述
在这里插入图片描述
输入常用的仪表板格式:https://grafana.com/grafana/dashboards/7362-mysql-overview
在这里插入图片描述

导入成功之后就能监控各种维度信息。
在这里插入图片描述

4.运行Prometheus + Granafa步骤总结

启动普罗修斯米,双击prometheus.exe;
启动mysql数据采集器,cmd进入到mysql数据采集器安装目录执行:

mysqld_exporter.exe --config.my-cnf=my.cnf

访问Granafa,浏览器访问:http://localhost:3001
    至此mysql数据监控环境搭建完成!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卖柴火的小伙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值