使用Prometheus和Grafana,可以快速的构建项目中性能测试绝大多数的监控模型场景:数据库监控、服务器监控、Jvm监控、Tomcat监控、Mq监控、Redis监控、Consul监控、Haproxy监控、Nginx监控、Elasticsearch等。
使用的组件
Mysqld_exporter 下载地址(https://prometheus.io/download/)node_exporter 下载地址(https://prometheus.io/download/)Prometheus 下载地址(https://prometheus.io/download/ 版本prometheus-2.18.1.linux-amd64.tar.gz)Grafana 下载地址(https://grafana.com/grafana/download?platform=linux) 或者docker安装
内网机器环境搭建
172.28.104.82 MySQL数据库(docker形式部署),在82上安装node_exporter,mysqld_exporter172.28.104.120 prometheus服务端和Grafana
在内网82上安装mysqld_exporter和node_exporter
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gztar -zxvf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
编辑mysqld_exporter目录下的my.cnf
[client]user=rootpassword=123456ip=172.28.104.82port=3307
在82机器上mysqld_exporter目录下启动,启动成功后,如下图
nohup ./mysqld_exporter --config.my-cnf=my.cnf & > mysqld_exporter.log nohup ./node_exporter > node_exporter.log
在120机器上安装Prometheus和Grafana
# 安装 Prometheustar -zxvf prometheus-2.18.1.linux-amd64.tar.gz# 安装 Grafanatar -zxvf grafana-7.0.0-1.x86_64.tar.gz
编辑prometheus.yml
# my global configglobal: 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. # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first_rules.yml" # - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'mysql' static_configs: - targets: ['172.28.104.82:9104'] labels: instance: 172.28.104.82 - job_name: 'linux' static_configs: - targets: ['172.28.104.82:9100'] labels: instance: 172.28.104.82
在 120机器上的usr/local/prometheus-2.18.1.linux-amd64目录下启动prometheus
#启动 prometheunohup ./prometheus > prometheus.log
在120机器上的usr/local/grafana-7.0.0/bin/目录下启动grafna
#启动 grafananohup ./grafana-server > grafana-server.log
在Grafana中添加Data source
在Grafana中添加Data source——在列表中选择Prometheus
在Grafana中添加Data source——在列表中选择MySQL
导入Mysql仪表盘 https://grafana.com/grafana/dashboards/7362
选择MySQL 仪表盘
MySQL Overview 主页面
在Prometheus的默认页面,在Status菜单项找到Targets选项,UP为上线健康状态
慢查询性能
查询性能方面,慢查询也是查询告警的一个重要的指标。MySQL还提供了一个Slow_queries的计数器,当查询的执行时间超过long_query_time的值后,计数器就会+1,其默认值为10秒,可以通过以下指令在MySQL中查询当前long_query_time的设置
SHOW VARIABLES LIKE 'long_query_time';也可以修改时间(修改为3秒)SET GLOBAL long_query_time = 3;