Prometheus 監(jiān)控MySQL使用grafana展示
prometheus通過exporter監(jiān)控mysql,并用grafana圖表展示
概述:
prometheus是由SoundCloud開發(fā)的開源監(jiān)控告警系統(tǒng)并且自帶時序數(shù)據(jù)庫,基于Go語言。Prometheus根據(jù)配置的任務(job)以周期性pull的方式獲取指定目標(target)上的指標(metric)。
Prometheus 生態(tài)圈中包含了多個組件:
- Prometheus Server: 根據(jù)配置完成數(shù)據(jù)采集, 服務發(fā)現(xiàn)以及數(shù)據(jù)存儲。
- Push Gateway : 為應對部分push場景提供的插件,監(jiān)控數(shù)據(jù)先推送到 Push Gateway 上,然后再由 Prometheus Server 端采集 pull 。用于存在時間較短,可能在 Prometheus 來 pull 之前就消失了的 jobs (若 Prometheus Server 采集間隔期間,Push Gateway 上的數(shù)據(jù)沒有變化, Prometheus Server 將采集到2次相同的數(shù)據(jù),僅時間戳不同)
- Exporters(探針): 是Prometheus的一類數(shù)據(jù)采集組件的總稱。它負責從目標處搜集數(shù)據(jù),并將其轉(zhuǎn)化為Prometheus支持的格式。與傳統(tǒng)的數(shù)據(jù)采集組件不同的是,它并不向中央服務器發(fā)送數(shù)據(jù),而是等待中央服務器主動前來抓取。
- Alertmanager: Prometheus server 主要負責根據(jù)基于PromQL的告警規(guī)則分析數(shù)據(jù),如果滿足PromQL定義的規(guī)則,則會產(chǎn)生一條告警,并發(fā)送告警信息到Alertmanager,Alertmanager則是根據(jù)配置處理告警信息并發(fā)送。常見的接收方式有:電子郵件,webhook 等。Alertmanager三種處理告警信息的方式:分組,抑制,靜默。
接下來開始演示
1、測試機器
prometheus-server 192.168.56.140 MySQL host01 192.168.56.103 MySQL host02 192.168.56.104
2、配置mysql host01
MySQL使用版本:
8.0.25 MySQL Community Server
3、創(chuàng)建exporter帳號
mysqld_exporter通過查詢mysql的狀態(tài)表及狀態(tài)命令獲取數(shù)據(jù)。所以,需要先在mysql內(nèi),創(chuàng)建相應帳號
create user 'exporter'@'%' identified by 'Xiaopang*803'; GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'exporter'@'%'; GRANT SELECT ON performance_schema.* TO 'exporter'@'%'; flush privileges;
4、下載,安裝mysqld_exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
tar xvzf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/.
cd /usr/local && ln -s mysqld_exporter-0.13.0.linux-amd64/ mysqld_exporter
4.1 編輯文件輸入密碼
編緝?nèi)缦挛募斎雃xporter用戶句與密碼(與前面mysql內(nèi)創(chuàng)建的帳號密碼一致)
[root@host01 mysqld_exporter]# vi .my.cnf [client] user=exporter password=Xiaopang*803
4.2 添加啟動服務文件
[root@host01 ~]# vi /etc/systemd/system/mysqld_exporter.service
[Unit] Description=mysqld_exporter After=network.target [Service] Type=simple ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf Restart=on-failure [Install] WantedBy=multi-user.target
4.3 啟動mysqld_exporter
service mysqld_exporter start
4.4 測試驗證
mysqld_exporter默認使用9104端口,我們可以在瀏覽器內(nèi)輸入如下地址。查看是否有數(shù)據(jù)輸出。
輸入 http://192.168.56.103:9104/metrics
輸出信息類似如下:
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 2.5395e-05 go_gc_duration_seconds{quantile="0.25"} 3.5372e-05 go_gc_duration_seconds{quantile="0.5"} 3.9393e-05 go_gc_duration_seconds{quantile="0.75"} 5.5068e-05 go_gc_duration_seconds{quantile="1"} 0.062537624 go_gc_duration_seconds_sum 0.453204071 go_gc_duration_seconds_count 2131 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge
5、下載,安裝node_exporter
如果只安裝mysqld_exporter則無法監(jiān)控OS相關(guān)的數(shù)據(jù),所以需要安裝node_exporter進行OS監(jiān)控。
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz tar xvzf node_exporter-1.2.2.linux-amd64.tar.gz -C /usr/local/. cd /usr/local && ln -s node_exporter-1.2.2.linux-amd64/ node_exporter
5.1 添加啟動服務文件
[root@host01 ~]# vi /etc/systemd/system/node_exporter.service [Unit] Description=node_export Documentation=https://github.com/prometheus/node_exporter After=network.target [Service] Type=simple User=root Group=root ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target
5.2 啟動node_exporter
service node_exporter start
5.3 測試驗證
node_exporter默認使用9100端口,我們可以在瀏覽器內(nèi)輸入如下地址。查看是否有數(shù)據(jù)輸出。
輸入 http://192.168.56.103:9100/metrics
輸出結(jié)果類似如下:
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 2.5934e-05 go_gc_duration_seconds{quantile="0.25"} 4.0072e-05 go_gc_duration_seconds{quantile="0.5"} 4.7616e-05 go_gc_duration_seconds{quantile="0.75"} 6.726e-05 go_gc_duration_seconds{quantile="1"} 0.228887598 go_gc_duration_seconds_sum 0.550266258 go_gc_duration_seconds_count 793 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge
6、安裝prometheus+grafana
使用版本:
prometheus 2.28
grafana 6.7.6
6.1 安裝
下載軟件包
wget https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz
6.2 解壓并添加軟鏈接
tar xvzf prometheus-2.28.1.linux-amd64.tar.gz -C /usr/local/. cd /usr/local/ ln -s prometheus-2.28.1.linux-amd64/ prometheus
6.3 增加啟動服務
[root@prometheus-server prometheus]# vi /etc/systemd/system/prometheus.service [Unit] Description=Prometheus Monitoring System Documentation=Prometheus Monitoring System [Service] Type=simple User=root Group=root ExecStart=/usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path="data/" \ --storage.tsdb.retention.time=15d \ --web.max-connections=512 \ --web.listen-address=:9090
6.4 添加mysql監(jiān)控
vi /usr/local/prometheus/prometheus.yml
scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'mysql' static_configs: - targets: ['192.168.56.103:9104'] labels: instance: mysql_instance1 - job_name: 'linux' static_configs: - targets: ['192.168.56.103:9100'] labels: instance: mysql_instance1
6.5 啟動prometheus
service prometheus start
6.7 查看prometheus
prometheus默認監(jiān)控端口
http://192.168.56.140:9090/
點擊status->target。如果一切正常,可以看到如下mysql/linux的state為UP
7、下載,安裝grafana
wget https://dl.grafana.com/oss/release/grafana-6.7.6-1.x86_64.rpm
rpm -ivh grafana-6.7.6-1.x86_64.rpm
7.1 訪問grafana
prometheus的展示功能很弱,為了更好的進行圖形展示,所以我們需要grafana
輸入 http://192.168.56.140:3000/
配置data source為proemtheus的HTTP鏈接(注意是HTTP,而不是HTTPS)
7.2導入mysql監(jiān)控模板
grafana數(shù)據(jù)的展示是通過模板實現(xiàn)的。grafana網(wǎng)站上面有很多共享的模板,你可以自行探索。
本例模板,我是從如下鏈接下載的。
https://grafana.com/api/dashboards/9623/revisions/4/download
版本不匹配問題
因為版本不太匹配的原因,完成后有些項目如法正常顯示。它使用的版本是grafana5.0版本,我的是6.x版本。
但是這點小問題,難不倒我,我自己修改了一下。就能正常顯示了,如下是修改后的JSON文件。
修改過程
很多時候,很多東西并不完全能拿來即用。我們需要根據(jù)自己的需要進行一些修改。
接下來大概花了半個多小時,弄清楚了如何修改了。而后大概又花了兩小時,修改完成的相應的項目。
修改過程中,碰到的問題,主要就兩類:
1)grafana 5.x和6.x組件的名稱發(fā)生了變化。
"Buffer Pool Size of Total RAM"無法正常顯示,原因是6.0和5.0組件名不同。
替換 pmm-singlestat-panel -> singlestat搞定
2)exporter提取的屬性名字發(fā)生了變化
我用的是node_exporter-1.2.2,這里面關(guān)于OS提取的屬性與JSON文件的定義屬性名不匹配。
方法是直接在“http://192.168.56.103:9100/metrics”里面搜索新的屬性名,替換JSON文件里面的舊的屬性名。
例如:
替換 node_memory_MemTotal->node_memory_MemTotal_bytes 替換 node_memory_MemTotal->node_memory_MemTotal_bytes
進行導入
因為我進行了一些修改,你可以import的時候,直接把JSON的內(nèi)容輸入進去。
點擊Load加載,接下來,選擇數(shù)據(jù)源為prometheus。
7.3 啟動sysbench壓測工具
開啟sysbench工具的目的是通過壓測生成有數(shù)據(jù)變化的圖表(不然,沒有流量,數(shù)據(jù)也不會動)。
這里,我從遠端壓測(在另一臺機器host02上運行sysbench)。目的是為了生成網(wǎng)絡流量數(shù)據(jù)。
[root@host02 ~]# sysbench /usr/share/sysbench/oltp_read_write.lua --time=9180 --mysql-host=host01 --mysql-port=3306 --mysql-user=dbusr --mysql-password=Xiaopang*803 --mysql-db=db1 --table-size=50000 --tables=15 --threads=15 --report-interval=10 run sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 15 Report intermediate results every 10 second(s) Initializing random number generator from current time Initializing worker threads... Threads started! [ 10s ] thds: 15 tps: 112.68 qps: 2268.92 (r/w/o: 1589.76/452.30/226.85) lat (ms,95%): 277.21 err/s: 0.00 reconn/s: 0.00 [ 20s ] thds: 15 tps: 113.91 qps: 2282.81 (r/w/o: 1598.47/456.52/227.81) lat (ms,95%): 211.60 err/s: 0.00 reconn/s: 0.00 [ 30s ] thds: 15 tps: 109.80 qps: 2192.95 (r/w/o: 1536.66/436.69/219.59) lat (ms,95%): 240.02 err/s: 0.00 reconn/s: 0.00 [ 40s ] thds: 15 tps: 112.70 qps: 2265.36 (r/w/o: 1583.17/456.79/225.40) lat (ms,95%): 193.38 err/s: 0.00 reconn/s: 0.00 [ 50s ] thds: 15 tps: 101.00 qps: 2013.42 (r/w/o: 1413.32/398.10/202.00) lat (ms,95%): 325.98 err/s: 0.00 reconn/s: 0.00
7.4查看grafana,完成后效果
這里只貼出了部分圖表。
到此這篇關(guān)于Prometheus MySQL監(jiān)控使用grafana展示的文章就介紹到這了,更多相關(guān) MySQL grafana內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
刪除mysql數(shù)據(jù)庫中的重復數(shù)據(jù)記錄
mysql中select distinct * from text不能顯示不重復的記錄,而是直接全部顯示2008-06-06MySQL數(shù)據(jù)庫的索引原理與慢SQL優(yōu)化的5大原則
這篇文章主要介紹了MySQL數(shù)據(jù)庫的索引原理與慢SQL優(yōu)化的5大原則,包括:建立索引的原則,慢查詢優(yōu)化基本步驟,慢查詢優(yōu)化案例,explain使用,需要的朋友可以參考下2023-02-02MySQL百萬數(shù)據(jù)深度分頁優(yōu)化思路解析
這篇文章主要為大家介紹了MySQL百萬數(shù)據(jù)深度分頁優(yōu)化思路分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05