侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

利用Prometheus与Grafana对Mysql服务器的性能监控详解

数据库  /  管理员 发布于 6年前   436

概述

Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。它提供了一个简单的网页界面、一个功能强大的查询语言以及HTTP接口等等。Prometheus通过安装在远程机器上的exporter来收集监控数据,这里用到了以下两个exporter:

  1. node_exporter C 用于机器系统数据
  2. mysqld_exporter C 用于Mysql服务器数据

Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化。它内置了以下数据源的支持:

并可以通过插件扩展支持的数据源。

架构图

下面是本次部署的架构图

安装并运行Prometheus

1.在 Monitor 上安装 Prometheus

安装至/opt/prometheus

$ wget https://github.com/prometheus/prometheus/releases/download/v1.5.2/prometheus-1.5.2.linux-amd64.tar.gz$ tar zxvf prometheus-1.5.2.linux-amd64.tar.gz$ mv prometheus-1.5.2.linux-amd64 /opt/prometheus

2.在安装目下编辑配置文件 prometheus.yml

vim /opt/prometheus/prometheus.yml
# my global configglobal: 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). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first.rules" # - "second.rules"# 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'] # 以下是添加的部分 - job_name: linux static_configs: - targets: ['172.30.251.214:9100'] labels: instance: db1 - job_name: mysql static_configs: - targets: ['172.30.251.214:9104'] labels: instance: db1

其中:172.30.251.214是MySQL的IP,端口则是对应的exporter的监听端口。

3.启动Prometheus

[loya@centos6 prometheus]$ ./prometheusINFO[0000] Starting prometheus (version=1.5.2, branch=master, revision=bd1182d29f462c39544f94cc822830e1c64cf55b) source=main.go:75INFO[0000] Build context (go=go1.7.5, user=root@a8af9200f95d, date=20170210-14:41:22) source=main.go:76INFO[0000] Loading configuration file prometheus.yml source=main.go:248INFO[0000] Loading series map and head chunks... source=storage.go:373INFO[0000] 0 series loaded. source=storage.go:378INFO[0000] Starting target manager... source=targetmanager.go:61INFO[0000] Listening on :9090 source=web.go:259

Prometheus内置了一个web界面,可以通过http://monitor_host:9090进行访问:

在Status->Targets页面下,可以看到我们配置的两个Target,它们的State为DOWN。

4.接下来需要在被监控机器上安装并运行exporter

下载exporters并解压:

$ mkdir /opt/prometheus/prometheus_exporters$ cd /opt/prometheus/prometheus_exporters$ wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0-rc.1/node_exporter-0.14.0-rc.1.linux-amd64.tar.gz$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.9.0/mysqld_exporter-0.9.0.linux-amd64.tar.gz$ tar zxvf node_exporter-0.14.0-rc.1.linux-amd64.tar.gz$ tar zxvf mysqld_exporter-0.9.0.linux-amd64.tar.gz# 将各自的可执行文件移动到/opt/prometheus/prometheus_exporters$ ll /opt/prometheus/prometheus_exporterstotal 24464-rwxr-xr-x 1 root root 12182376 Feb 23 19:01 mysqld_exporter-rwxr-xr-x 1 root root 12862209 Feb 23 19:01 node_exporter

运行node_exporter

$ cd /opt/prometheus/prometheus_exporters$ ./node_exporter INFO[0000] Starting node_exporter (version=0.14.0-rc.1, branch=master, revision=5a07f4173d97fa0dd307db5bd3c2e6da26a4b16e) source="node_exporter.go:136"INFO[0000] Build context (go=go1.7.4, user=root@ed143c8f2fcd, date=20170116-16:00:03) source="node_exporter.go:137"INFO[0000] No directory specified, see --collector.textfile.directory source="textfile.go:57"INFO[0000] Enabled collectors: source="node_exporter.go:156"INFO[0000] - entropy source="node_exporter.go:158"INFO[0000] - loadavg source="node_exporter.go:158"INFO[0000] - stat source="node_exporter.go:158"INFO[0000] - diskstats source="node_exporter.go:158"INFO[0000] - textfile source="node_exporter.go:158"INFO[0000] - vmstat source="node_exporter.go:158"INFO[0000] - meminfo source="node_exporter.go:158"INFO[0000] - filefd source="node_exporter.go:158"INFO[0000] - filesystem source="node_exporter.go:158"INFO[0000] - mdadm source="node_exporter.go:158"INFO[0000] - netdev source="node_exporter.go:158"INFO[0000] - sockstat source="node_exporter.go:158"INFO[0000] - time source="node_exporter.go:158"INFO[0000] - zfs source="node_exporter.go:158"INFO[0000] - edac source="node_exporter.go:158"INFO[0000] - hwmon source="node_exporter.go:158"INFO[0000] - netstat source="node_exporter.go:158"INFO[0000] - uname source="node_exporter.go:158"INFO[0000] - conntrack source="node_exporter.go:158"INFO[0000] Listening on :9100 source="node_exporter.go:176"

mysqld_exporter需要连接到MySQL,所以需要MySQL权限,我们为其创建用户并赋予所需的权限:

mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'prom'@'localhost' identified by 'abc123';mysql> GRANT SELECT ON performance_schema.* TO 'prom'@'localhost';

创建.my.cnf文件并运行mysqld_exporter:

$ cd /opt/prometheus/prometheus_exporters$$ cat << EOF > .my.cnf[client]user=prompassword=abc123EOF$$ ./mysqld_exporter -config.my-cnf=".my.cnf"INFO[0000] Starting mysqld_exporter (version=0.9.0, branch=master, revision=8400af20ccdbf6b5e0faa2c925c56c48cd78d70b) source=mysqld_exporter.go:432INFO[0000] Build context (go=go1.6.3, user=root@2c131c66ca20, date=20160926-18:28:09) source=mysqld_exporter.go:433INFO[0000] Listening on :9104 source=mysqld_exporter.go:451

回到Prometheus web界面Status->Targets,可以看到两个Target的状态已经变成UP了:

安装和运行Grafana

安装:

Grafana 的安装官方文档比较清晰,这里不再赘述。

配置:

编辑配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下两个参数的值:

[dashboards.json]enabled = truepath = /var/lib/grafana/dashboards

导入Prometheus Dashboard:

$ git clone https://github.com/percona/grafana-dashboards.git$ cp -r grafana-dashboards/dashboards /var/lib/grafana

启动:

$ /etc/init.d/grafana-server start

通过http://monitor_host:3000访问Grafana Web界面(缺省帐号/密码为admin/admin)

登录后,通过Data Sources页面添加数据源:

然后就可以通过选择不同的仪表盘(左上角)和时间段(右上角)来呈现图表了:

System Overview:

MySQL Overview:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。


  • 上一条:
    MySQL慢查询之开启慢查询
    下一条:
    MySql安装及登录详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客