[1] DB monitoring
DB metric ์ ํ์ตํ๊ธฐ ์ํด ๋ก์ปฌ ํ๊ฒฝ์์ mysqld-exporter + prometheus + grapana ๋ฅผ ๊ธฐ๋ฐํ DB monitoring ๊ตฌ์ถํ ๋ด์ฉ์ ๊ดํด ๊ธฐ๋กํ๊ณ ์ ํ๋ค.
(1) mysql-exporter
Prometheus์ ํจ๊ป ์ฌ์ฉ๋๋ MySQL/MariaDB์ฉ ๋ฉํธ๋ฆญ ์์ง ๋๊ตฌ์ด๋ค. MySQL ์๋ฒ์ ์ฑ๋ฅ ๋ฐ ์ํ ์ ๋ณด๋ฅผ Prometheus๊ฐ ์์งํ ์ ์๋๋ก HTTP ์๋ํฌ์ธํธ๋ฅผ ํตํด ๋ ธ์ถํ๋ ์ญํ ์ ํ๋ค. InnoDB, ๋ฒํผ ํ, ํธ๋์ญ์ , ์ฟผ๋ฆฌ ์บ์, ์ฐ๊ฒฐ ์ํ ๋ฑ ๋ค์ํ ์งํ ์ ๊ณตํ๋ฉฐ, Prometheus์ ์ฐ๋ ๊ฐ๋ฅํด mysqld-exporter์ ๋ฉํธ๋ฆญ์ ๊ฐ์ ธ์ ์๊ฐํ ๋ฐ ๊ฒฝ๊ณ ์ค์ ํ ์ ์๋ค.
(2) Prometheus
Prometheus๋ ์คํ์์ค ๋ชจ๋ํฐ๋ง ๋ฐ ์๋ ์์คํ ์ผ๋ก, ๋ฉํธ๋ฆญ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ ์ ์ฅํ๋ ์๊ณ์ด(time-series) ๋ฐ์ดํฐ๋ฒ ์ด์ค์ด๋ค. ์ฃผ๋ก ์๋ฒ, ์ ํ๋ฆฌ์ผ์ด์ , ์ปจํ ์ด๋ ๋ฑ์ ์ฑ๋ฅ์ ๋ชจ๋ํฐ๋งํ๊ณ , ๋ฌธ์ ๋ฐ์ ์ ์๋ฆผ์ ๋ณด๋ด๋ ๋ฐ ์ฌ์ฉ๋๋ค.
1. ์ฃผ์ ํน์ง
โ ์๊ณ์ด ๋ฐ์ดํฐ ๊ธฐ๋ฐ (์๊ฐ์ ๋ฐ๋ผ ๋ณํํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ ๋ฐ ๋ถ์)
โ Pull ๋ฐฉ์์ ๋ฐ์ดํฐ ์์ง (exporter๋ก๋ถํฐ ์ฃผ๊ธฐ์ ์ผ๋ก ๋ฉํธ๋ฆญ ๊ฐ์ ธ์ค๊ธฐ)
โ PromQL (Prometheus Query Language) ์ง์
โ Grafana์ ์ฐ๋ ๊ฐ๋ฅ (๋ฐ์ดํฐ ์๊ฐํ)
โ Alertmanager๋ฅผ ํ์ฉํ ์๋ ์ค์
(3) Grafana
Grafana๋ ์คํ์์ค ๋ฐ์ดํฐ ์๊ฐํ ๋ฐ ๋ชจ๋ํฐ๋ง ๋๊ตฌ์ด๋ค.Prometheus, InfluxDB, MySQL, Elasticsearch ๋ฑ ๋ค์ํ ๋ฐ์ดํฐ ์์ค๋ก๋ถํฐ ๋ฉํธ๋ฆญ์ ๊ฐ์ ธ์ ๋์๋ณด๋ ํํ๋ก ์๊ฐํํ ์ ์๋ค.
1. ์ฃผ์ ํน์ง
โ ๋ค์ํ ๋ฐ์ดํฐ ์์ค ์ง์ (Prometheus, MySQL, AWS CloudWatch ๋ฑ)
โ ๋์๋ณด๋ ๊ธฐ๋ฐ ์๊ฐํ (๊ทธ๋ํ, ์ฐจํธ, ํ
์ด๋ธ ๋ฑ ์ง์)
โ ๊ฒฝ๊ณ (Alert) ๊ธฐ๋ฅ (ํน์ ์กฐ๊ฑด ์ถฉ์กฑ ์ ์๋ ์ ์ก)
โ ๋ค์ํ ํ๋ฌ๊ทธ์ธ ๋ฐ ํ
ํ๋ฆฟ ์ง์
โ ์ฌ์ฉ์ ๊ถํ ๊ด๋ฆฌ ๋ฐ ๋์๋ณด๋ ๊ณต์ ๊ฐ๋ฅ
2. ์ฃผ์ ๊ธฐ๋ฅ
โ ๋์๋ณด๋ : ์ฌ๋ฌ ํจ๋์ ์กฐํฉํด ๋ฐ์ดํฐ ์๊ฐํ
โ ํจ๋(Panel) : ๊ทธ๋ํ, ํ ์ด๋ธ, ํํธ๋งต ๋ฑ ๋ค์ํ ์ฐจํธ ์ ๊ณต
โ ํ ํ๋ฆฟ ๋ณ์ : ๋์ ํํฐ๋ง์ ์ํ ๋ณ์ ์ง์
โ ์๋(Alerting) : ํน์ ์กฐ๊ฑด ์ถฉ์กฑ ์ Slack, Email ๋ฑ์ผ๋ก ์๋ ์ ์ก
โ ์ฌ์ฉ์ ๊ด๋ฆฌ : ์กฐ์ง๋ณ ์ฌ์ฉ์ ๊ถํ ๋ถ์ฌ ๊ฐ๋ฅ
โํ๋ฌ๊ทธ์ธ ์ง์ : ์ปค์คํ ํจ๋ ๋ฐ ๋ฐ์ดํฐ ์์ค ํ์ฅ ๊ฐ๋ฅ
[2] DB monitoring ๊ตฌ์ถ
(1) docker compose.yml
๊ฐ ์ปจํ ์ด๋๋ docker-network ์์ ์๋ธ๋ท ํ์์ ipv4 ์ ์ ์ธํ๊ณ , bridge ์ต์ ์ ์ ์ธํด ์ปจํ ์ด๋ ๊ฐ ํต์ ์ด ๊ฐ๋ฅํ๋๋ก ๊ตฌ์ฑ
version: '3.8'
services:
mysql:
image: mysql:8.0
ports:
- "3306:3306"
volumes:
- ../concert_mysql:/var/lib/mysql
- ./rdb/initdb.d:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: 'concert2025'
MYSQL_DATABASE: concert_db
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
networks:
concert-network:
ipv4_address: 172.10.0.2
mysqld-exporter:
image: prom/mysqld-exporter:latest
volumes:
- ./mysqld-exporter/.my.cnf:/cfg/.my.cnf
depends_on:
- mysql
command:
- --config.my-cnf=/cfg/.my.cnf
- --mysqld.address=172.10.0.2:3306
ports:
- "9104:9104"
networks:
concert-network:
ipv4_address: 172.10.0.5
prometheus:
image: prom/prometheus
container_name: prometheus
volumes:
- ./prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
- ../grafana-data:/var/lib/grafana
ports:
- "9090:9090"
command:
- '--web.enable-lifecycle'
- '--config.file=/etc/prometheus/prometheus.yml'
networks:
concert-network:
ipv4_address: 172.10.0.6
volumes:
grafana-data:
mysql-data:
networks:
concert-network:
driver: bridge
ipam:
config:
- subnet: 172.10.0.0/16
gateway: 172.10.0.1
(2) mysqld-exporter ๋ฅผ ์ํ ์ฌ์ฉ์ ์ถ๊ฐ
- mysql container ๋ฅผ ์์ฑ ํ, ์ฌ์ฉ์ ์์ฑ์ ์๋ํํ๊ธฐ ์ํด init.sql ์ docker-entrypoint-initdb.d ๋ก ๋ง์ดํธ ์ค์
# ์ฌ์ฉ์ ์์ฑ
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'concert2025' WITH MAX_USER_CONNECTIONS 3;
# ์์ฑํ ์ ์ ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ์ ์ ํ ๊ถํ์ ๋ถ์ฌํด์ค์๋ค.
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
(3) mysql-exporter - .my.cnf ์ค์
- mysql-exporter ๊ฐ ๋ฉํธ๋ฆญ์ ๊ฐ์ ธ์ค๊ธฐ ์ํด ์ฌ์ฉํ ์ฌ์ฉ์ ๊ณ์ ๊ณผ ๋น๋ฐ๋ฒํธ ์ ๋ ฅ
- command option ์ --mysqld.address ์ต์ ์ ์ ์ธํด ํต์ ํ ์ฃผ์ ์ค์ (mysql container Ipv4:port = 172.10.0.2:3306)
[client]
user=exporter
password=concert2025
์ค์ ์ ์๋ฃํ๊ณ docker-compose.yml ์ ์คํํ๊ณ localhost:9104/metrics ํ์ธํ๊ธฐ

(4) prometheus ์ค์
mysqld-exporter ์ ์ ๋ฌ๋ metric ์ prometheus ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํด prometheus.yml ์ mysqld-exporter ipv4(172.10.0.2) ์ค์
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: []
scheme: http
timeout: 10s
api_version: v2
scrape_configs:
- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- localhost:9090
- job_name: mysqld-exporter
scrape_interval: 3s
static_configs:
- targets:
- 172.10.0.5:9104 # mysqld-exporter ipv4:port ์ค์
(5) grafana - ์ ์
- grafana container ์คํ ํ, localhost:3000 ์ ์
- id/pw ์ ๋ ฅํ์ฌ ๋ก๊ทธ์ธ (default id/pw = admin/admin)

(6) grafana - datasource ์ค์
- menu > Connections > add new connection > prometheus ์ค์
- Connection : http//172.10.0.6:9090 (prometheus ipv4:port ์ค์ )

(7) grafana - dashboard ์ค์
- menu > Dashboards > import ํด๋ฆญ
- [grafana] MySQL Exporter Quickstart and Dashboard Json Download
- Json file > Import dashboard - import via dashboard JSON model ์ ์ถ๊ฐ


(8) dashboard ํ์ธ

Reference
- [dockerdocs] networks.ipam : https://docs.docker.com/reference/compose-file/networks/#ipam
- [grafana] MySQL Exporter Quickstart and Dashboard : https://grafana.com/grafana/dashboards/14057-mysql/
- MySQL monitoring using Prometheus and Grafana | MySQL Exporter : https://www.youtube.com/watch?v=cjIb-lKeN5s
'๐ backend > test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Testcontainers ์ฝ์ง1 : ํ ์คํธ ์ปจํ ์ด๋ ์ฌ์ฌ์ฉ (0) | 2025.01.13 |
|---|---|
| K6 tutorial (0) | 2024.12.04 |
| nGrinder tutorial (0) | 2024.03.19 |
| pinpoint simple summary (0) | 2024.03.08 |
