溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用Prometheus和Grafana監(jiān)控MySQL

發(fā)布時(shí)間:2020-06-08 17:13:49 來(lái)源:網(wǎng)絡(luò) 閱讀:14110 作者:weiyanwei412 欄目:MySQL數(shù)據(jù)庫(kù)

 一、 Prometheus 是一個(gè)開(kāi)源的服務(wù)監(jiān)控系統(tǒng)和時(shí)間序列數(shù)據(jù)庫(kù)。:

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

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

官方拓?fù)鋱D如下

利用Prometheus和Grafana監(jiān)控MySQL


特性:

  • 高維度數(shù)據(jù)模型

  • 自定義查詢語(yǔ)言

  • 可視化數(shù)據(jù)展示

  • 高效的存儲(chǔ)策略

  • 易于運(yùn)維

  • 提供各種客戶端開(kāi)發(fā)庫(kù)

  • 警告和報(bào)警

  • 數(shù)據(jù)導(dǎo)出

二、安裝Prometheus(服務(wù)器環(huán)境為Centos7.2,本實(shí)驗(yàn)服務(wù)端跟被監(jiān)控MySQL在同一臺(tái)服務(wù)器)

前提條件請(qǐng)關(guān)閉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文件,內(nèi)容如下:
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

后臺(tái)啟動(dòng)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提供一個(gè)內(nèi)置的web界面。我們可以通過(guò)http://192.168.1。199:9090訪問(wèn)

利用Prometheus和Grafana監(jiān)控MySQL

在 Status -> Targets 頁(yè)面下,我們可以看到我們配置的兩個(gè)Target,它們的 State 

利用Prometheus和Grafana監(jiān)控MySQL

三、接下來(lái)我們安裝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、啟動(dòng)os監(jiān)控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,需要授權(quán)
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'monitor'@'localhost' identified by 'test123';
mysql> GRANT SELECT ON performance_schema.* TO 'monitor'@'localhost';
4、創(chuàng)建 .my.cnf 文件并運(yùn)行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為主機(jī)名
啟動(dòng)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 頁(yè)面

利用Prometheus和Grafana監(jiān)控MySQL

四、安裝和運(yùn)行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 并沒(méi)有太多的配置好的圖表模板,除了 Percona 開(kāi)源的一些外,很多需要自行配置。
下載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 段落下兩個(gè)參數(shù)的值:
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
2、運(yùn)行 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

我們可通過(guò) http://192.168.1.199:3000 訪問(wèn)Grafana網(wǎng)頁(yè)界面(缺省的帳號(hào)/密碼為admin/admin):

利用Prometheus和Grafana監(jiān)控MySQL

登錄并設(shè)置數(shù)據(jù)源

利用Prometheus和Grafana監(jiān)控MySQL

利用Prometheus和Grafana監(jiān)控MySQL

至此大功告成

我們可以看到os與數(shù)據(jù)庫(kù)監(jiān)控?cái)?shù)據(jù)了

利用Prometheus和Grafana監(jiān)控MySQL

利用Prometheus和Grafana監(jiān)控MySQL





利用Prometheus和Grafana監(jiān)控MySQL

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI