
作者 李文航 · 沃趣科技数据库技术专家
出品 沃趣科技
添加MySQL监控
[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar
[root@localhost ~]# mv mysqld_exporter-0.10.0.linux-amd64 mysqld_exporter复制
[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复制
mysqld_exporter集中部署
node | IP |
mysqld_exporter | 172.18.0.23 |
mysql-11 | 172.18.0.11 |
mysql-12 | 172.18.0.13 |
在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 &复制
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暴露的端口复制
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
添加172.18.0.13节点
添加新的节点到监控,只需要做4步就可以。
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是否添加成功
集中管理
当我们需要添加新的节点,只需要将新节点的连接信息配置好,划分新的端口,启动mysqld_exporter,然后在prometheus中的mysql.yml文件添加新节点暴露的端口以及新节点的自定义别名就可以了。
| 作者简介
李文航·沃趣科技数据库技术专家
熟悉MySQL体系结构和工作原理、SQL调优、数据库故障诊断、数据迁移、备份恢复。
相关链接
Oracle RAC Cache Fusion 系列十:Oracle RAC Enqueues And Lock Part 1
Oracle RAC Cache Fusion 系列九:Oracle RAC 分布式资源管理(二)
Oracle RAC Cache Fusion 系列八:Oracle RAC 分布式资源管理(一)

更多干货,欢迎来撩~