天天看點

mysqld_exporter+Prometheus+grafana監控Mysql

關于Prometheus和grafana的安裝這裡不做贅述。

需要的朋友可以參考上一篇文章,node_exporter+Prometheus+grafana搭建伺服器資源監控系統

一.部署mysqld_exporter

mysqld_exporter需部署到被監控的伺服器上。

1.下載下傳部署

(base) [email protected]:~# cd /usr/local/src/
(base) [email protected]:/usr/local/src# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
           

伺服器下載下傳太慢時,可直接從github網站下載下傳,并将壓縮包上傳至伺服器。

文章最後分享了網盤連結也可自行下載下傳。

(base) [email protected]:/usr/local/src# tar xvf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
(base) [email protected]:/usr/local/src# cd /usr/local/
(base) [email protected]:/usr/local# mv mysqld_exporter-0.13.0.linux-amd64/ mysql_exporter/
           
mysqld_exporter+Prometheus+grafana監控Mysql

安裝運作mysqld_exporter

mysqld_exporter要連接配接到Mysql,是以需要Mysql的權限,我們先為它建立使用者并賦予所需的權限:

#登入mysql的root使用者,并建立使用者
(base) [email protected]:/usr/local# mysql -uroot -p'password'
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'tom'@'localhost' identified by 'qaz691';
mysql> GRANT SELECT ON performance_schema.* TO 'tom'@'localhost';
           

建立.my.cnf檔案并運作mysqld_exporter:

(base) [email protected]:/usr/local# cd /usr/local/mysql_exporter/
(base) [email protected]:/usr/local/mysql_exporter# vim .my.cnf
           

user與password與之前建立的一緻,.my.cnf中内容如下:

[client]

host=127.0.0.1

user= tom

password=qaz691

啟動mysqld_exporter:

(base) [email protected]:/usr/local/mysql_exporter# ./mysqld_exporter 
INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96)  source="mysqld_exporter.go:257"
INFO[0000] Build context (go=go1.12.7, [email protected], date=20190729-12:35:58)  source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:269"
INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:273"
INFO[0000]  --collect.slave_status                       source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmpmem          source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.query_response_time    source="mysqld_exporter.go:273"
           

2.通路驗證

通路http://192.168.1.12:9104,這裡的ip替換為伺服器ip。

出現以下頁面說明啟動成功。

mysqld_exporter+Prometheus+grafana監控Mysql

3.開機自啟動

配置檔案易出錯,文章最後分享了網盤連結也可自行下載下傳。

mysqld_exporter+Prometheus+grafana監控Mysql

二.prometheus接收mysqld_exporter采集的資料

修改prometheus配置檔案,重新啟動prometheus。

mysqld_exporter+Prometheus+grafana監控Mysql

通路http://192.168.1.13:9090/targets:

mysqld_exporter+Prometheus+grafana監控Mysql

三.Grafana展示mysqld_exporter采集的資料

1.檢查Data souces是否正常。

mysqld_exporter+Prometheus+grafana監控Mysql

2.導入Dashboard

登入之後,點選左側“+”->Import

mysqld_exporter+Prometheus+grafana監控Mysql

輸入ID:7362,點選“Load”。(7362是比較經典好用的一個展示MySQL使用情況的模闆,這裡以導入這個模闆為例)

mysqld_exporter+Prometheus+grafana監控Mysql

選擇data source,下拉選擇上一步配置的資料源,點選“Import”

mysqld_exporter+Prometheus+grafana監控Mysql

展示效果如圖:

mysqld_exporter+Prometheus+grafana監控Mysql

至此,我們已經完成了對MySQL監控系統的搭建。

安裝所需包及配置檔案在百度網盤裡面,此連結永久有效,如有需要可下載下傳:

連結:MySQL監控系統

提取碼:2pu3

mysqld_exporter+Prometheus+grafana監控Mysql