prometheus學習筆記(3)-使用exporter監控mysql


上一篇學習了使用java client向prometheus寫入數據,但更多時候,我們希望借助prometheus來監控一些標准中間件,比如mysql、haproxy等等。本篇將以mysql為例,學習如何利用exporter來監控其核心指標。(注:以下均為mac環境)

 

一、下載mysql exporter
要監控某個對象,首先得拿到被監控對象的指標數據,這就要借助各種exporter,它的主要作用就是把核心指標數據暴露出來,這樣監控系統才能獲取到相應數據。

prometheus自帶了很多exporter, mysqld_exporter 就是專用於暴露mysql各種指標的插件。

下載地址:https://prometheus.io/download/#mysqld_exporter,下載到本地然后解壓即可,假設下載解壓后的目錄名為:/Users/jimmy/Downloads/mysqld_exporter-0.12.1.darwin-386

 

二、配置exporter
連接mysql需要用戶名、密碼,所以下載之后,首先要創建配置文件,把用戶名、密碼以及mysql服務器地址,這些基本信息填寫進去。
在mysqld_exporter的目錄下,創建一個.my.cnf的文件,內容參考下面的內容:

[client]
host=127.0.0.1
port=3306
user=exporter
password=exporter123

配置很簡單,一看就明白,當然,如果mysql中沒有exporter這個用戶名,需要提前創建,參考以下命令:

GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' IDENTIFIED BY 'exporter123' WITH MAX_USER_CONNECTIONS 3;

flush privileges;

密碼:exporter123可以根據需要更改,盡量復雜些,保證安全。

 

三、啟動exporter
在mysqld_exporter的解壓目錄下,輸入

./mysqld_exporter --config.my-cnf=/Users/jimmy/Downloads/mysqld_exporter-0.12.1.darwin-386/.my.cnf

--config.my-cnf的配置文件路徑,大家根據自己的情況,做相應調整。

➜ mysqld_exporter-0.12.1.darwin-386 ./mysqld_exporter --config.my-cnf=/Users/jimmy/Downloads/mysqld_exporter-0.12.1.darwin-386/.my.cnf
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, user=root@0b3e56a7bc0a, date=20190729-12:37:01) 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_cmp 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"
INFO[0000] Listening on :9104 source="mysqld_exporter.go:283"

啟動完成后,會看到類似上面的輸出,注意最后一行,表示將在9104這個端口上,啟動mysql監聽, 可以瀏覽 http://localhost:9104/metrics 驗證是否有數據輸出,如果看到類似下面的輸出,表示工作正常。

 

四、配置prometheus
mysqld_exporter啟動后,prometheus怎么知道它的存在呢? 這就要修改prometheus的配置文件:prometheus.yml ,該文件默認在 /usr/local/etc/ 目錄下。

scrape_configs:
  - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]
  - job_name: "push-metrics"
    static_configs:
    - targets: ["localhost:9091"]
  - job_name: "mysql"
    static_configs:
    - targets: ["localhost:9104"]
    honor_labels: true

文件的結構大致說一下,scrape_config節點下,有一堆job_name,prometheus就是通過配置各種job來獲取監控數據的。參考上面的內容,在最后添加mysql的job即可。

添加完成后,需要重啟prometheus, 重啟完成后,即可看到mysql開頭的各種數據。

 

 

五、grafana導入mysql模板

有了mysql的各種指標,就可以在grafana里配置監控圖表了,當然可以手動一項項指標自己添加,但是這樣效率太低了,對於mysql這種標准的中間件而言,grafana官網已經有很多現成的圖表模板,可以直接拿來用,如下圖:

 

在grafana中,點擊import

輸入mysql dashboard模板地址: https://grafana.com/grafana/dashboards/11323

加載成功后,會出現下圖:

注意要選擇Prometheus(參考上圖),點擊import ,大功告成!

 注:如果導入錯誤了,想要刪除,可參考下圖操作


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM