Deploy MySQL and mysqld_exporter with Docker Compose and Configure Prometheus Monitoring
This guide shows how to set up a MySQL server and a mysqld_exporter using Docker Compose, configure Prometheus to scrape the exporter, and create alert rules for MySQL downtime and slow queries, providing a complete monitoring solution.
This article provides a step‑by‑step tutorial for deploying MySQL and the Prometheus MySQL exporter (mysqld_exporter) using Docker Compose, and then configuring Prometheus to monitor the database.
1. Deploy MySQL with Docker Compose
Create a directory for MySQL data and a docker-compose.yaml file containing the following service definition:
version: '3.1'
services:
db:
image: mysql
restart: always
container_name: mysql
environment:
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MYSQL_ROOT_PASSWORD: 123456
command:
- --default-authentication-plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_general_ci
- --lower_case_table_names=1
- --performance_schema=1
- --sql-mode=""
- --skip-log-bin
volumes:
- /data/mysql/conf:/etc/mysql/conf.d
- /data/mysql/data:/var/lib/mysql
ports:
- 3306:3306Run the container with docker-compose up -d .
2. Deploy mysqld_exporter with Docker Compose
Create a separate directory and a docker-compose.yaml file for the exporter:
version: '3.3'
services:
mysqld-exporter:
image: prom/mysqld-exporter
container_name: mysqld-exporter
restart: always
command:
- '--collect.info_schema.processlist'
- '--collect.info_schema.innodb_metrics'
- '--collect.info_schema.tablestats'
- '--collect.info_schema.tables'
- '--collect.info_schema.userstats'
- '--collect.engine_innodb_status'
environment:
- DATA_SOURCE_NAME=exporter:password@(192.168.18.69:3306)/
ports:
- 9104:9104Start the exporter with docker-compose up -d .
3. Configure Prometheus to scrape the exporter
Add a new job to prometheus.yml :
- job_name: 'mysqld_exporter'
static_configs:
- targets: ['192.168.18.69:9104']
labels:
instance: mysql服务器Reload the configuration using curl -X POST http://localhost:9090/-/reload (or docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml to verify).
4. Add alert rules for MySQL
Create mysqld.yml with the following rule groups:
groups:
- name: MySQL
rules:
- alert: MysqlDown
expr: mysql_up == 0
for: 30s
labels:
severity: critical
annotations:
summary: "MySQL Down, 实例:{{ $labels.instance }}"
description: "MySQL_exporter 连不上 MySQL,当前状态为:{{ $value }}"
- alert: MysqlSlowQueries
expr: increase(mysql_global_status_slow_queries[2m]) > 0
for: 2m
labels:
severity: warning
annotations:
summary: "Mysql慢日志告警, 实例:{{ $labels.instance }}"
description: "MySQL 在过去 2 分钟有新的 {{ $value }} 条慢查询"After adding the rules, reload Prometheus as described above.
Following these steps results in a fully monitored MySQL instance with automatic alerts for downtime and performance issues.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.