Overview #
I’m using the following Ubuntu 24.04 based servers in this tuturial:
192.168.30.10 # Prometheus Docker Compose stack
192.168.30.11 # MySQL Container, MySQL Server Exporter Container
Prerequisites #
Create Docker Network #
# Create a new Docker network
docker network create prometheus-network
MySQL Container #
File and Folder Structure #
# Create a directory for MySQL
sudo mkdir -p /opt/mysql && cd /opt/mysql
Docker Compose Manifest #
# Create a Docker Compose manifest
sudo vi docker-compose.yml
services:
mysql:
image: mysql:8.3.0
container_name: mysql
restart: unless-stopped
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=my-secure-pw
networks:
- prometheus-network
networks:
prometheus-network:
driver: bridge
# Create the container
sudo docker compose up -d
Connect to MySQL Database #
# Install the SQL client
sudo apt update &&
sudo apt install mysql-client -y
# Connect to the MySQL server
mysql -u root -p -h 127.0.0.1
-p
Prompt for password
# Create a new "exporter" user
CREATE USER 'exporter'@'%' IDENTIFIED BY 'my-secure-exporter-pw' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
# Exit the SQL client
exit
Create a new user:
-
CREATE USER 'exporter'@'%'
Create a new MySQL user “exporter” that can connect from any host@'%'
-
IDENTIFIED BY 'my-secure-exporter-pw'
Define a password for the new MySQL user -
WITH MAX_USER_CONNECTIONS 3
Limit the user to maximum 3 simultaneous connections to the MySQL server
Grant the exporter user the following permissions:
-
PROCESS
Allows the user to see information about server processes (needed for monitoring) -
REPLICATION CLIENT
Allows the user to check replication status (useful for monitoring replication health) -
SELECT
Allows the user to read (query) data from all databases and tablesON *.*
MySQL Server Exporter Container #
File and Folder Structure #
# Create a directory for the MySQL Server Exporter
sudo mkdir -p /opt/mysql-exporter && cd /opt/mysql-exporter
Exporter Configuration file #
sudo vi mysql-exporter.cnf
[client]
user = exporter
password = my-secure-exporter-pw
host = mysql
Docker Compose Manifest #
# Create a Docker Compose manifest
sudo vi docker-compose.yml
services:
exporter:
image: prom/mysqld-exporter:main
container_name: mysql-exporter
restart: unless-stopped
ports:
- "9104:9104"
volumes:
- ./mysql-exporter.cnf:/cfg/mysql-exporter.cnf
command: ["--config.my-cnf=/cfg/mysql-exporter.cnf"]
networks:
- prometheus-network
networks:
prometheus-network:
driver: bridge
# Create the container
sudo docker compose up -d
Verify the Exporter #
# List the containers
docker ps
# Shell output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16902383d30d prom/mysqld-exporter:main "/bin/mysqld_exporte…" 2 minutes ago Up 2 minutes 0.0.0.0:9104->9104/tcp, :::9104->9104/tcp mysql-exporter
af002dbd5003 mysql:8.3.0 "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
# Curl the exporter metrics
curl http://localhost:9104/metrics
Prometheus Configuration #
Prometheus Configuration: prometheus.yml #
# Adapt the Prometheus configuration
sudo vi prometheus_conf/prometheus.yml
# prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 10s
rule_files:
- rules.yml
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets: [ 'alertmanager:9093' ]
scrape_configs:
# Prometheus Server
- job_name: 'Prometheus-Server'
static_configs:
- targets:
- localhost:9090
# Linux Servers / Node Exporter
- job_name: 'Linux-Server'
static_configs:
- targets:
- 192.168.30.11:9100
- 192.168.30.12:9100
# MySQL Server / MySQL Server Exporter
- job_name: 'MySQL-Server'
params:
auth_module: [client] # Specify authentication mode
scrape_interval: 5s
static_configs:
- targets: ['192.168.30.11:3306']
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.30.11:9104
Prometheus Configuration: rules.yml #
# Adapt the rules.yml configuration
sudo vi prometheus_conf/rules.yml
# rules.yml
groups:
- name: NodeExporter
rules:
- alert: InstanceDown
expr: up{job="Linux-Server"} == 0
for: 1m
- name: MysqldExporter
rules:
- alert: MysqlDown
expr: mysql_up == 0
for: 0m
labels:
severity: critical
annotations:
summary: MySQL down (instance {{ $labels.instance }})
description: "MySQL instance is down on {{ $labels.instance }}\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
Restart Docker Container #
# Restart the Docker container
sudo docker compose restart
Dashboards #
Grafana Dashboard #
Open Webinterface #
# Grafana webinterface
192.168.30.10:3000
Import the “Prometheus MySQL Exporter” Dashboard #
Import the “Prometheus MySQL Exporter" dashboard with the ID “7362”:
Go to: “Home” > “Dashboard”
Click “+ Create dashboard”
Select “Import dashboard”
Paste the dashboard ID “7362” and click “Load”
Select the “Prometheus” data source
Click “Import”
The dashboard should look like this:
Prometheus Webinterface #
Open Webinterface #
# Open the Prometheus webinterface
http://192.168.30.10:9090
Verify Endpoints #
- Select “Status” > “Target health”
Example Alert #
Stop MySQL Container #
Stop the MySQL Docker container:
# CD into the Docker Compose directory
cd /opt/mysql
# Stop the Docker container
sudo docker compose down
Verify the Prometheus Alert #
# Verify the alert in the Prometheus webinterface
http://192.168.30.10:9090/alerts
Verify the Alertmanager Alert #
# Verify the alert in the Prometheus webinterface
http://192.168.30.10:9093/#/alerts