一、創(chuàng)建用于性能監(jiān)控的mysql賬號(hào)
1、創(chuàng)建數(shù)據(jù)庫(kù)用戶
CREATE USER 'exporter'@'localhost' INEDTIFIED BY 'exporter'
2、給予權(quán)限
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'
二、啟動(dòng)用于收集mysql性能信息的mysqld_exporter
vim docker-compose.yml
version: '3'
services:
mysqld-exporter:
container_name: mysqld-exporter
image: prom/mysqld-exporter
restart: always
ports:
- "9104:9104"
environment:
- DATA_SOURCE_NAME=exporter:exporter@(xxx.xxx.xxx.xxx:3306)/
networks:
- proxy
networks:
proxy:
external: true
啟動(dòng)后訪問(wèn)http://xxx.xxx.xxx.xxx:9104/metrics,看數(shù)據(jù)是否可以成功捕獲
記得開(kāi)放9104端口
三、配置prometheus,添加mysql監(jiān)控
1、編輯配置文件
vim prometheus/prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
remote_write:
- url: "http://xxx.xxx.xxx.xxx:8086/api/v1/prom/write?db=prometheus&u=admin&p=***"
remote_read:
- url: "http://xxx.xxx.xxx.xxx:8086/api/v1/prom/read?db=prometheus&u=admin&p=***"
rule_files:
- "./rules/*.yml"
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
# 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=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'agent'
basic_auth:
username: admin
password: ***
static_configs:
- targets: ['xxx.xxx.xxx.xxx:9100']
# 添加traefik監(jiān)控
- job_name: 'traefik'
basic_auth:
username: admin
password: ***
static_configs:
- targets: ['xxx.xxx.xxx.xxx:8080']
# 添加mysql監(jiān)控
- job_name: 'mysql'
static_configs:
- targets: ['xxx.xxx.xxx.xxx:9104']
2、重啟prometheus
docker-compose restart
四、grafana添加mysql監(jiān)控儀表盤(pán)
導(dǎo)入mysql監(jiān)控儀表盤(pán),左邊菜單欄點(diǎn)擊import,輸入7362,點(diǎn)擊load即可

導(dǎo)入7362儀表盤(pán).png

mysql監(jiān)控儀表盤(pán).png