[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 확인하기

위와 같이 metric 이 보이면 성공이다.

 

 

(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 - 접속

  1. grafana container 실행 후, localhost:3000 접속
  2. id/pw 입력하여 로그인 (default id/pw = admin/admin)

grafana 로그인 화면

 

(6) grafana - datasource 설정

  • menu > Connections > add new connection > prometheus 설정
  • Connection : http//172.10.0.6:9090 (prometheus ipv4:port 설정)

 

(7) grafana - dashboard 설정

  1. menu > Dashboards > import 클릭
  2. [grafana] MySQL Exporter Quickstart and Dashboard Json Download
  3. Json file > Import dashboard - import via dashboard JSON model 에 추가

 

 

 

(8) dashboard 확인

 

 

Reference

'test' 카테고리의 다른 글

Testcontainers 삽질1 : 테스트 컨테이너 재사용  (0) 2025.01.13
K6 tutorial  (0) 2024.12.04