溫馨提示×

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

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

如何構(gòu)建MySQL監(jiān)控平臺(tái)

發(fā)布時(shí)間:2021-11-02 15:00:21 來(lái)源:億速云 閱讀:163 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章將為大家詳細(xì)講解有關(guān)如何構(gòu)建MySQL監(jiān)控平臺(tái),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

prometheus+grafana 對(duì)于現(xiàn)在這個(gè)時(shí)間點(diǎn)來(lái)說(shuō),相信很多同行都應(yīng)該已經(jīng)開(kāi)始玩起來(lái)了,當(dāng)仍然可能有一部分人可能還不知道prometheus+grafana 的存在,也可能還有一部分人雖然知道它的存在,但卻懶于動(dòng)手,如果是這樣,那后面的內(nèi)容你可得打起精神來(lái)了,因?yàn)榭赡苣銜?huì)被grafana炫酷的視覺(jué)效果驚艷到。

讓我們一起了解一下吧。

首先,簡(jiǎn)單介紹下prometheus+grafana 這對(duì)鴛鴦到底是什么:

prometheus 是由 SoundCloud 開(kāi)發(fā)的開(kāi)源監(jiān)控報(bào)警系統(tǒng)和時(shí)序列數(shù)據(jù)庫(kù)(TSDB),prometheus是一個(gè)監(jiān)控采集與數(shù)據(jù)存儲(chǔ)框架(監(jiān)控server端),具體采集什么數(shù)據(jù)依賴(lài)于具體的exporter(監(jiān)控client端),例如:采集MySQL的數(shù)據(jù)需要使用到mysql_exporter,prometheus調(diào)用mysql_expoter采集到mysql的監(jiān)控指標(biāo)之后,把mysql_exporter采集到的數(shù)據(jù)存放到prometheus所在服務(wù)器的磁盤(pán)數(shù)據(jù)文件中保存。它的各個(gè)組件基本都是用 golang 編寫(xiě),對(duì)編譯和部署十分友好.并且沒(méi)有特殊依賴(lài),基本都是獨(dú)立工作。以下是prometheus架構(gòu)圖(圖片來(lái)源:https://prometheus.io/docs/introduction/overview/) 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

grafana是一個(gè)高顏值的監(jiān)控繪圖程序,也是一個(gè)可視化面板(Dashboard),grafana的厲害之處除了高顏值,還支持多種數(shù)據(jù)源(支持Graphite、zabbix、InfluxDB、Prometheus和OpenTSDB作為數(shù)據(jù)源)、支持靈活豐富的dashboard配置選項(xiàng)(例如:可以把多個(gè)實(shí)例的相同采集項(xiàng)配置在一個(gè)展示框里),使得相較于其他開(kāi)源監(jiān)控系統(tǒng)來(lái)說(shuō)更易用性,學(xué)習(xí)成本更低。從視覺(jué)上來(lái)說(shuō),比以往的任何開(kāi)源的監(jiān)控系統(tǒng)都看起來(lái)要養(yǎng)眼很多,下面先看兩張監(jiān)控效果圖: 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

相信審美還算正常的人都不會(huì)說(shuō)上面兩張圖很丑吧,那么問(wèn)題來(lái)了,我們?cè)撊绾瓮嫫饋?lái)呢?下面就簡(jiǎn)單為大家介紹如何快速搭建起來(lái)!

這里方便演示過(guò)程,我們準(zhǔn)備了如下兩臺(tái)測(cè)試服務(wù)器

  • prometheus+grafana server端主機(jī):10.10.30.165

  • MySQL 客戶(hù)端主機(jī):10.10.20.14

1、安裝prometheus

1.1. 下載安裝包

對(duì)于prometheus,假設(shè)我們需要監(jiān)控MySQL,那么我們需要下載至少3個(gè)組件,如下:

  • prometheus程序包

  • node_exporter:監(jiān)控主機(jī)磁盤(pán)、內(nèi)存、CPU等硬件性能指標(biāo)的采集程序包

  • mysql_exporter: 監(jiān)控mysql各種性能指標(biāo)的采集程序包

下載鏈接(該頁(yè)面始終只有一個(gè)最新版本):https://prometheus.io/download/

  • 下載prometheus 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

  • 下載node_exporter 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

  • 下載mysqld_exporter 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

  • PS:如果你還需要配置監(jiān)控告警,需要下載alertmanager程序包 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

1.2. 解壓程序包

解壓prometheus

[root@localhost ~]# mkdir /data
[root@localhost ~]# tar xvf prometheus-2.1.0.linux-amd64.tar.gz -C /data/

   解壓exporter:由于prometheus主機(jī)自身也需要監(jiān)控,所以也至少需要解壓node_exporter包

[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar -C /root/
# 如果需要監(jiān)控mysql,則繼續(xù)解壓mysql_exporter
[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar  -C /root/
1.3. 啟動(dòng)prometheus

進(jìn)入prometheus的工作目錄

[root@localhost ~]# cd /data/
[root@localhost data]# mv prometheus-2.1.0.linux-amd64/ prometheus
[root@localhost ~]# cd /data/prometheus

    配置 prometheus.yml配置文件

[root@localhost data]# cat prometheus.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).
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- file_sd_configs:
- files:
- host.yml
job_name: Host
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9100
- file_sd_configs:
files:
- mysql.yml
job_name: MySQL
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9104
- job_name: prometheus
static_configs:
- targets:
- localhost:9090

 啟動(dòng)prometheus進(jìn)程,30d表示prometheus只保留30天以?xún)?nèi)的數(shù)據(jù)

[root@localhost prometheus]# /data/prometheus/prometheus  --storage.tsdb.retention=30d &

如果是7.x系統(tǒng),可以按照如下方式配置service啟動(dòng)腳本

# 修改WorkingDirectory參數(shù)為你的prometheus的工作目錄
[root@localhost prometheus]# cat /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus instance
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service
[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/
RuntimeDirectory=prometheus
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/prometheus  --storage.tsdb.retention=30d --config.file=/data/prometheus/prometheus.yml
LimitNOFILE=10000
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target

PS:prometheus默認(rèn)的web訪問(wèn)端口為9090,可以使用如下地址訪問(wèn)http://10.10.30.165:9090

2、安裝grafana

前面說(shuō)過(guò),grafana是一個(gè)出圖展示框架,grafana根據(jù)grafana-dashboards來(lái)進(jìn)行展示,grafana-dashboards就類(lèi)似于grafana的出圖配置文件,根據(jù)在grafana-dashboards中的定義來(lái)確定在頁(yè)面中需要展示什么指標(biāo),需要如何展示等,需要分別對(duì)這兩個(gè)組件進(jìn)行下載與安裝。

2.1. 下載安裝包

對(duì)于grafana來(lái)說(shuō),需要下載一個(gè)程序包,一個(gè)grafana-dashboards包

下載鏈接

grafana程序包:https://grafana.com/grafana/download 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

grafana-dashboards包:https://github.com/percona/grafana-dashboards/releases 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

2.2. 解壓程序包

解壓grafana

[root@localhost ~]# tar xf grafana-4.6.3.linux-x64.tar.gz -C /data/prometheus/
[root@localhost ~]# cd /data/prometheus
[root@localhost prometheus]# mv grafana-4.6.3/ grafana
2.3. 啟動(dòng)grafana

進(jìn)入grafana工作目錄,并啟動(dòng)

[root@localhost ]# cd /data/prometheus/grafana
[root@localhost ]# ./bin/grafana-server

如果是7.x系統(tǒng),可以按照如下方式配置service啟動(dòng)腳本

[root@localhost service]# cat /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service
[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/grafana/bin/grafana-server
LimitNOFILE=10000
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target

打開(kāi)grafana頁(yè)面(默認(rèn)帳號(hào)和密碼:admin/admin,默認(rèn)的端口為3000,通過(guò)地址:http://10.10.30.165:3000 訪問(wèn)),配置數(shù)據(jù)來(lái)源。

如何構(gòu)建MySQL監(jiān)控平臺(tái)

指定prometheus地址,這里我們把grafana裝在了同一臺(tái)機(jī)器,直接使用127.0.0.1的地址配置即可,如下圖 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

2.4. 在grafana中導(dǎo)入grafana-dashboards

解壓grafana-dashboards包,該包中提供了大量的json格式文件的grafana dashboards,根據(jù)需要自行選擇,我們這里需要監(jiān)控主機(jī)和MySQL,就選擇如下一些json文件。

[root@localhost ~]# tar xvf grafana-dashboards-1.6.1.tar.gz 
[root@localhost ~]# cd grafana-dashboards-1.6.1
[root@localhost grafana-dashboards-1.6.1]# updatedb 
[root@localhost grafana-dashboards-1.6.1]# locate json |grep dashboards/
............
/root/grafana-dashboards-1.6.1/dashboards/CPU_Utilization_Details_Cores.json
/root/grafana-dashboards-1.6.1/dashboards/Disk_Performance.json
/root/grafana-dashboards-1.6.1/dashboards/Disk_Space.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_InnoDB_Metrics.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_InnoDB_Metrics_Advanced.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Overview.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Performance_Schema.json
............
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Replication.json
/root/grafana-dashboards-1.6.1/dashboards/MySQL_Table_Statistics.json
............
/root/grafana-dashboards-1.6.1/dashboards/Summary_Dashboard.json
/root/grafana-dashboards-1.6.1/dashboards/System_Overview.json
............

    在grafana頁(yè)面中,導(dǎo)入需要的json文件 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

   在彈出的窗口中選擇你需要導(dǎo)入的json文件

如何構(gòu)建MySQL監(jiān)控平臺(tái)

然后,如果你的grafana中已經(jīng)添加過(guò)主機(jī),此時(shí),就可以看到相應(yīng)的json dashboard監(jiān)控?cái)?shù)據(jù) 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

至此,prometheus+grafana的基礎(chǔ)架構(gòu)(server端)已經(jīng)搭建好了,現(xiàn)在,你可以去給他們添加監(jiān)控節(jié)點(diǎn)了(client端)

3、監(jiān)控節(jié)點(diǎn)部署

3.1. 添加主機(jī)監(jiān)控

以添加prometheus主機(jī)(10.10.30.165)為例進(jìn)行說(shuō)明

解壓exporter壓縮包

[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar 
[root@localhost ~]# mv node_exporter-0.15.2.linux-amd64 node_exporter

 啟動(dòng)node_exporter程序

[root@localhost ~]# cd node_exporter
[root@localhost node_exporter]# nohup ./node_exporter &

配置prometheus主機(jī)監(jiān)控配置列表文件,由于之前主配置文件prometheus.yml 中已經(jīng)定義了監(jiān)控主機(jī)的配置文件host.yml,這里只需要把主機(jī)IP信息填入即可動(dòng)態(tài)生效

[root@localhost node_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165

然后,在grafana頁(yè)面中就可以看到你配置的主機(jī)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

PS:如果該文件中已經(jīng)配置過(guò)lables且不需要使用獨(dú)立的service標(biāo)簽進(jìn)行標(biāo)記,則新添加的實(shí)例的IP可以直接放在同一個(gè)targets下,如下:

[root@localhost mysqld_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14
3.2. 添加MySQL監(jiān)控

添加MySQL監(jiān)控主機(jī),這里以添加10.10.20.14為例進(jìn)行說(shuō)明

解壓exporter壓縮包

[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar 
[root@localhost ~]# mv mysqld_exporter-0.10.0.linux-amd64 mysqld_exporter

配置監(jiān)控?cái)?shù)據(jù)庫(kù)需要的主機(jī)IP、數(shù)據(jù)庫(kù)端口、數(shù)據(jù)庫(kù)賬號(hào)和密碼的環(huán)境變量(注意:該賬號(hào)需要單獨(dú)創(chuàng)建,需要對(duì)所有庫(kù)所有表至少具有PROCESS, REPLICATION CLIENT, SELECT權(quán)限)

[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)/'
[root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)/'" >> /etc/profile

啟動(dòng)exporter

# 由于目前最新的版本默認(rèn)關(guān)閉了大量的mysql采集項(xiàng),需要顯式使用相應(yīng)的選項(xiàng)開(kāi)啟(截止到寫(xiě)稿時(shí)間,最新的開(kāi)發(fā)版本可以通過(guò)prometheus端的配置項(xiàng)讓exporter端生效,而無(wú)需再exporter中使用大量的啟動(dòng)選項(xiàng)開(kāi)啟)
[root@localhost ~]# cd mysqld_exporter
[root@localhost mysqld_exporter]# nohup ./mysqld_exporter --collect.info_schema.processlist --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_metrics  --collect.perf_schema.tableiowaits --collect.perf_schema.indexiowaits --collect.perf_schema.tablelocks --collect.engine_innodb_status --collect.perf_schema.file_events --collect.info_schema.processlist --collect.binlog_size --collect.info_schema.clientstats --collect.perf_schema.eventswaits &

配置prometheus MySQL監(jiān)控配置列表文件,由于之前主配置文件prometheus.yml 中已經(jīng)定義了監(jiān)控主機(jī)的配置文件mysql.yml,這里只需要把主機(jī)IP信息填入即可動(dòng)態(tài)生效

[root@localhost mysqld_exporter]# cat /data/prometheus/host.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14

然后,在grafana頁(yè)面中就可以看到你配置的MySQL實(shí)例 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

PS:如果該文件中已經(jīng)配置過(guò)lables且不需要使用獨(dú)立的service標(biāo)簽進(jìn)行標(biāo)記,則新添加的實(shí)例的IP可以直接放在同一個(gè)targets下,如下:

[root@localhost mysqld_exporter]# cat /data/prometheus/mysql.yml
- labels:
service: test
targets:
- 10.10.30.165
- 10.10.20.14
3.3. grafana頁(yè)面dashboard切換

根據(jù)需要切換監(jiān)控模板 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

如何構(gòu)建MySQL監(jiān)控平臺(tái)

然后,就能看到你想要的數(shù)據(jù) 

如何構(gòu)建MySQL監(jiān)控平臺(tái)

關(guān)于“如何構(gòu)建MySQL監(jiān)控平臺(tái)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向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