广告
返回顶部
首页 > 资讯 > 数据库 >利用Prometheus和Grafana监控MySQL
  • 708
分享到

利用Prometheus和Grafana监控MySQL

2024-04-02 19:04:59 708人浏览 安东尼
摘要

 一、 prometheus 是一个开源的服务监控系统和时间序列数据库。:官方GitHub地址为:https://github.com/prometheus/prometheus 官方地

 一、 prometheus 是一个开源的服务监控系统和时间序列数据库。:

官方GitHub地址为:https://github.com/prometheus/prometheus

官方地址:Https://prometheus.io/

官方拓扑图如下

利用Prometheus和Grafana监控MySQL


特性:

  • 高维度数据模型

  • 自定义查询语言

  • 可视化数据展示

  • 高效的存储策略

  • 易于运维

  • 提供各种客户端开发

  • 警告和报警

  • 数据导出

二、安装Prometheus(服务器环境为Centos7.2,本实验服务端跟被监控Mysql在同一台服务器)

前提条件请关闭SELinux与Firewalld(centos6  为iptables)

下载安装prometheus
$ wget https://github.com/prometheus/prometheus/releases/download/v1.7.1/prometheus-1.7.1.linux-amd64.tar.gz
$ mkdir  /opt/prometheus
$ tar zxf prometheus-1.7.1.linux-amd64.tar.gz  -C /opt/prometheus --strip-components=1
编辑配置prometheus.yml文件,内容如下:
global:
  scrape_interval:     15s
  evaluation_interval: 15s
  external_labels:
      monitor: 'codelab-monitor'
rule_files:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['192.168.1.199:9090']
  - job_name: linux
    static_configs:
      - targets: ['192.168.1.199:9100']
        labels:
          instance: db1
  - job_name: mysql
    static_configs:
      - targets: ['192.168.1.199:9104']
        labels:
          instance: db1

后台启动prometheus
 nohup /opt/prometheus/prometheus -config.file=prometheus.yml & 
tail -200f nohup.out 
time="2017-06-12T11:51:16+08:00" level=info msg="Starting prometheus (version=1.7.0, branch=master, revision=bfa37c8ee39d11078662dce16c162a61dccf616c)" source="main.Go:88" 
time="2017-06-12T11:51:16+08:00" level=info msg="Build context (go=go1.8.3, user=root@7a6329cc02bb, date=20170607-09:43:48)" source="main.go:89" 
time="2017-06-12T11:51:16+08:00" level=info msg="Host details (Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 monitor (none))" source="main.go:90" 
time="2017-06-12T11:51:16+08:00" level=info msg="Loading configuration file prometheus.yml" source="main.go:252" 
time="2017-06-12T11:51:16+08:00" level=info msg="Loading series map and head chunks..." source="storage.go:428" 
time="2017-06-12T11:51:16+08:00" level=info msg="2200 series loaded." source="storage.go:439" 
time="2017-06-12T11:51:16+08:00" level=info msg=" source="WEB.go:259"
Prometheus提供一个内置的web界面。我们可以通过http://192.168.1。199:9090访问

利用Prometheus和Grafana监控MySQL

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

利用Prometheus和Grafana监控MySQL

三、接下来我们安装client客户端

1、安装client
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz
$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
$ mkdir /opt/prometheus_exporters
$ tar zxf node_exporter-0.14.0.linux-amd64.tar.gz -C /opt/prometheus_exporters --strip-components=1  && tar zxf mysqld_exporter-0.10.0.linux-amd64.tar.gz -C /opt/prometheus_exporters --strip-components=1
2、启动os监控client
 nohup /opt/prometheus_exporters/node_exporter & 
tail -200f nohup.out 
time="2017-06-12T11:28:52+08:00" level=info msg=" - hwmon" source="node_exporter.go:162"
time="2017-06-12T11:28:52+08:00" level=info msg=" - netstat" source="node_exporter.go:162"
time="2017-06-12T11:28:52+08:00" level=info msg=" - stat" source="node_exporter.go:162"
time="2017-06-12T11:28:52+08:00" level=info msg=" - vmstat" source="node_exporter.go:162"
time="2017-06-12T11:28:52+08:00" level=info source="node_exporter.go:186"
3、mysqld_exporter需要连接到MySQL,需要授权
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'monitor'@'localhost' identified by 'test123';
mysql> GRANT SELECT ON perfORMance_schema.* TO 'monitor'@'localhost';
4、创建 .my.cnf 文件并运行mysqld_exporter
[root@monitor prometheus_exporters]# cat .my.cnf 
[client]
user=monitor
passWord=test123
[root@monitor prometheus_exporters]# export DATA_SOURCE_NAME="monitor:test123@(monitor:3306)/"
#####(monitor:3306)/此monitor为主机名
启动mysqld_exporter
nohup /opt/prometheus_exporters/mysqld_exporter -config.my-cnf=".my.cnf" &
tail -200f nohup.out
time="2017-06-12T11:30:07+08:00" level=info msg="Starting mysqld_exporter (version=0.10.0, branch=master, revision=80680068f15474f87847c8ee8f18a2939a26196a)" source="mysqld_exporter.go:460"
time="2017-06-12T11:30:07+08:00" level=info msg="Build context (go=go1.8.1, user=root@3b0154cd9e8e, date=20170425-11:24:12)" source="mysqld_exporter.go:461"
time="2017-06-12T11:30:08+08:00" level=info msg=" source="mysqld_exporter.go:479"

我们回到192.168.1.199:9090,可以看到

 Status -> Targets 页面

利用Prometheus和Grafana监控MySQL

四、安装和运行Grafana

1、安装Grafana
#wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.3.2-1.x86_64.rpm 
#yum localinstall grafana-4.3.2-1.x86_64.rpm 
替换grafana的dashboards
Grafana 并没有太多的配置好的图表模板,除了 Percona 开源的一些外,很多需要自行配置。
下载dashboards(https://github.com/percona/grafana-dashboards)
git clone https://github.com/percona/grafana-dashboards.git
cp -r grafana-dashboards/dashboards /var/lib/grafana/
2、编辑配置文件 /etc/grafana/grafana.ini ,修改 dashboards.JSON 段落下两个参数的值:
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
2、运行 Grafana
 systemctl start grafana-server
# [root@monitor prometheus_exporters]# ps aux |grep grafana
root      3211  0.0  0.0 112664   972 pts/1    S+   11:40   0:00 grep --color=auto grafana
grafana  21241  0.3  0.8 805720 39620 ?        Ssl  6月12   4:42 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile= cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins

我们可通过 http://192.168.1.199:3000 访问Grafana网页界面(缺省的帐号/密码为admin/admin):

利用Prometheus和Grafana监控MySQL

登录并设置数据源

利用Prometheus和Grafana监控MySQL

利用Prometheus和Grafana监控MySQL

至此大功告成

我们可以看到os与数据库监控数据了

利用Prometheus和Grafana监控MySQL

利用Prometheus和Grafana监控MySQL





利用Prometheus和Grafana监控MySQL

您可能感兴趣的文档:

--结束END--

本文标题: 利用Prometheus和Grafana监控MySQL

本文链接: https://www.lsjlt.com/news/36771.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作