添加MySQL監控
添加MySQL監控主機,這里以添加10.10.20.14為例進行說明。解壓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
配置監控數據庫需要的主機IP、數據庫端口、數據庫賬號和密碼的環境變量。
[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:letsg0@(10.10.20.14:3306)/'
[root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:letsg0@(10.10.20.14:3306)/'" >> /etc/profile
啟動exporter。
# 由於目前最新的版本默認關閉了大量的mysql采集項,需要顯式使用相應的選項開啟(截止到寫稿時間,最新的開發版本可以通過prometheus端的配置項讓exporter端生效,而無需再exporter中使用大量的啟動選項開啟) [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 & # 注意,新版本的mysqld_exporter可能不支持--collect.info_schema.processlist 選項,請自行使用./mysqld_exporter --help查看
配置prometheus MySQL監控配置列表文件,由於之前主配置文件prometheus.yml 中已經定義了監控MySQL的配置文件mysql.yml,這里只需要把主機IP信息填入即可動態生效。
[root@localhost mysqld_exporter]# cat /data/prometheus/mysql.yml - labels: service: mysql_test targets: - 10.10.30.165 - 10.10.20.14
然后,在grafana頁面中就可以看到你配置的MySQL實例 。
這種方式是在每個mysql服務器上跑一個exporter程序,比如10.10.20.14服務器上跑自己的exporter,然后再到登到10.10.30.165服務器上啟動自己的exporter,也就是分離部署,這樣的話每個mysql服務器上除了mysqld進程外還會多一個mysqld_exporter的進程。但是如果我們想要保持mysql服務器零入侵的純凈環境,應該怎么辦呢,這時候我們就可以嘗試一下集中部署+配置文件的方式。
mysqld_exporter集中部署
集中部署,就是說我們將所有的mysqld_exporter部署在同一台服務器上,在這台服務器上對mysqld_exporter進行統一的管理,下面介紹一下集中部署的方法。這里我們專門起一台IP為172.18.0.23的服務器,另外兩台172.18.0.11和172.18.0.13作為2個MySQL節點。
在172.18.0.23上下載安裝mysqld_exporter
添加172.18.0.11節點
1、在172.18.0.11上建立監控用戶
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'pmm'@'%' IDENTIFIED BY 'pmm';
2、在172.18.0.23上編輯172.18.0.11節點的配置文件
[root@mysqld_exporter-23 /]# cd /data/mysqld_exporter/ [root@mysqld_exporter-23 mysqld_exporter]# cat etc/.dk-11.cnf [client] user=pmm password=pmm host=172.18.0.11 port=3306
3、在172.18.0.23上啟動mysqld_exporter
[root@mysqld_exporter-23 ~]# cd /data/mysqld_exporter/
[root@mysqld_exporter-23 mysqld_exporter]# nohup ./mysqld_exporter --web.listen-address=172.18.0.23:9104 --config.my-cnf=etc/.dk-11.cnf --collect.auto_increment.columns --collect.binlog_size --collect.global_status --collect.global_variables --collect.info_schema.innodb_metrics --collect.info_schema.innodb_cmp --collect.info_schema.innodb_cmpmem --collect.info_schema.processlist --collect.info_schema.query_response_time --collect.info_schema.tables --collect.info_schema.tablestats --collect.info_schema.userstats --collect.perf_schema.eventswaits --collect.perf_schema.file_events --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.perf_schema.tablelocks --collect.slave_status &
這里看到mysqld_exporter的啟動多了兩個選項,"--web.listen-address"和"--config.my-cnf"。
web.listen-address代表這個mysqld_exporter進程綁定的端口,以供prometheus調用,這里暴露的是172.18.0.23的9104端口。
config.my-cnf代表這個mysqld_exporter進程監控的MySQL的連接信息。
4、在prometheus服務器配置prometheus文件
[root@prometheus-21 /]# cd /data/prometheus/ [root@prometheus-21 prometheus]# 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. # A scrape configuration containing exactly one endpoint to scrape: scrape_configs: - file_sd_configs: - files: - mysql.yml job_name: MySQL metrics_path: /metrics relabel_configs: - source_labels: [__address__] regex: (.*) target_label: __address__ replacement: $1
5、在prometheus服務器配置mysql.yml文件
[root@prometheus-21 prometheus]# cat mysql.yml - labels: instance: dk-11:3306 # grafana顯示的實例的別名 targets: - 172.18.0.23:9104 # mysqld_exporter暴露的端口
mysql.yml的172.18.0.23:9104會通過prometheus配置文件中的file_sd_configs配置,作為變量傳給$1,然后替換__address__,因而被prometheus所識別。
6、使prometheus配置生效
[root@prometheus-21 prometheus]# pgrep -fl prometheus 33 /data/prometheus/prometheus --storage.tsdb.retention=30d [root@prometheus-21 prometheus]# kill -HUP 33
7、驗證172.18.0.11是否添加成功
瀏覽器輸入Prometheus_IP:9090
瀏覽器輸入Grafana_IP:3000
添加172.18.0.13節點
1、在172.18.0.13上建立監控用戶
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'pmm'@'%' IDENTIFIED BY 'pmm';
2、在172.18.0.23上編輯172.18.0.13節點的配置文件
[root@mysqld_exporter-23 /]# cd /data/mysqld_exporter/ [root@mysqld_exporter-23 mysqld_exporter]# cat etc/.dk-13.cnf [client] user=pmm password=pmm host=172.18.0.13 port=3306
3、在172.18.0.23上啟動mysqld_exporter
[root@mysqld_exporter-23 ~]# cd /data/mysqld_exporter/ [root@mysqld_exporter-23 mysqld_exporter]# nohup ./mysqld_exporter --web.listen-address=172.18.0.23:9105 --config.my-cnf=etc/.dk-13.cnf --collect.auto_increment.columns --collect.binlog_size --collect.global_status --collect.global_variables --collect.info_schema.innodb_metrics --collect.info_schema.innodb_cmp --collect.info_schema.innodb_cmpmem --collect.info_schema.processlist --collect.info_schema.query_response_time --collect.info_schema.tables --collect.info_schema.tablestats --collect.info_schema.userstats --collect.perf_schema.eventswaits --collect.perf_schema.file_events --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.perf_schema.tablelocks --collect.slave_status & 這里暴露的是9105端口,使用的是.dk-13.cnf的文件。
4、在prometheus服務器配置mysql.yml文件
[root@prometheus-21 prometheus]# cat mysql.yml - labels: instance: dk-11:3306 targets: - 172.18.0.23:9104 - labels: instance: dk-13:3306 targets: - 172.18.0.23:9105
只需要添加172.18.0.13的mysqld_exporter對應的端口和別名就可以。
5、驗證172.18.0.13是否添加成功
瀏覽器輸入Prometheus_IP:9090
瀏覽器輸入Grafana_IP:3000
集中管理
當我們需要添加新的節點,只需要將新節點的連接信息配置好,划分新的端口,啟動mysqld_exporter,然后在prometheus中的mysql.yml文件添加新節點暴露的端口以及新節點的自定義別名就可以了。
這樣我們就可以在mysqld_exporter節點上對mysqld_exporter的進程進行統一管理了。