How to Monitor MySQL with Prometheus, Grafana, and MySQL Exporter

Monitoring MySQL databases is essential for ensuring performance, availability, and reliability in production environments. Without proper monitoring, issues such as high query latency, connection exhaustion, replication lag, or disk bottlenecks can go unnoticed until they impact applications.

In this blog, you’ll learn how to monitor MySQL using MySQL Exporter, Prometheus, and Grafana, starting from setup and ending with real-time dashboards and actionable insights.

What Is MySQL Exporter, Prometheus, and Grafana?

MySQL

MySQL is one of the most widely used open-source relational databases, powering web applications, SaaS platforms, and enterprise systems.

Prometheus

Prometheus is an open-source monitoring and alerting system that collects metrics using a pull-based model and stores time-series data efficiently.

Grafana

Grafana is a powerful visualization and observability platform used to build dashboards, analyze metrics, and create alerts.

MySQL Exporter

MySQL Exporter exposes MySQL internal metrics (connections, queries, InnoDB stats, replication status) in Prometheus format.

Why Monitor MySQL with Prometheus and Grafana?

✔ Real-time visibility into database performance
✔ Early detection of slow queries and connection spikes
✔ Monitor replication and availability
✔ Capacity planning and trend analysis
✔ Production-ready observability stack

MySQL Monitoring Architecture

https://files.readme.io/bced411-prometheus_block_diagram.jpg

Data Flow

MySQL → MySQL Exporter → Prometheus → Grafana

System Requirements

ComponentRequirement
OSRHEL 8/9 or Ubuntu
MySQL5.7 / 8.0
RAMMinimum 2 GB
Ports9104, 9090, 3000
AccessMySQL user with monitoring privileges

Step 1: Create MySQL Monitoring User

Login to MySQL:

mysql -u root -p

Create exporter user:

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;

This user has read-only permissions, making it safe for production.

Step 2: Install MySQL Exporter
cd /opt
sudo wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

Extract:

sudo tar -xvf mysqld_exporter-0.15.1.linux-amd64.tar.gz
sudo mv mysqld_exporter-0.15.1.linux-amd64 mysqld_exporter

Configure Exporter Credentials

sudo nano /etc/.my.cnf
[client]
user=exporter
password=StrongPassword

Secure file:

sudo chmod 600 /etc/.my.cnf
Create MySQL Exporter Service
sudo nano /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
After=network.target

[Service]
User=root
ExecStart=/opt/mysqld_exporter/mysqld_exporter \
  --config.my-cnf=/etc/.my.cnf

[Install]
WantedBy=multi-user.target

Start exporter:

sudo systemctl daemon-reload
sudo systemctl enable mysqld_exporter
sudo systemctl start mysqld_exporter

Verify:

curl http://localhost:9104/metrics
Step 3: Configure Prometheus for MySQL Metrics

Edit Prometheus config:

sudo nano /opt/prometheus/prometheus.yml

Add:

  - job_name: "mysql"
    static_configs:
      - targets: ["localhost:9104"]

Restart Prometheus:

sudo systemctl restart prometheus

Verify target:

http://<server-ip>:9090/targets
Step 4: Install and Access Grafana
sudo dnf install -y https://dl.grafana.com/oss/release/grafana-10.2.3-1.x86_64.rpm

Start Grafana:

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

Access:

http://<server-ip>:3000

Step 5: Add Prometheus Data Source in Grafana

https://www.stackhero.io/locales/documentations/grafana/020_prometheus/grafanaPrometheusDataSourceConfiguration.png

Steps:

  1. Grafana → ⚙ Settings → Data Sources
  2. Add Prometheus
  3. URL: http://localhost:9090
  4. Save & Test

Step 6: Import MySQL Grafana Dashboard

https://miro.medium.com/v2/resize%3Afit%3A1400/1%2AEhQCCGq4eehBLTrGdu1-sg.png

Recommended dashboard:

Dashboard ID: 7362

Steps:

  1. Grafana → Dashboards → Import
  2. Enter 7362
  3. Select Prometheus → Import
Key MySQL Metrics You Can Monitor

✔ Active & running connections
✔ Queries per second (QPS)
✔ Slow queries
✔ InnoDB buffer pool usage
✔ Disk I/O and latency
✔ Replication status & lag

Production Best Practices

✔ Use a dedicated MySQL monitoring user
✔ Enable TLS for MySQL and Grafana
✔ Monitor replication nodes individually
✔ Set alerts for connection saturation
✔ Track long-term trends for capacity planning

Common Issues and Fixes
IssueSolution
No MySQL data in GrafanaCheck Prometheus target
Exporter auth failureVerify .my.cnf
Dashboard emptySelect correct data source
High exporter CPUReduce scrape interval
MySQL Monitoring with Grafana vs Native Tools
FeaturePrometheus + GrafanaNative MySQL
VisualizationAdvancedLimited
AlertsYesNo
Historical dataYesLimited
ScalabilityHighLow
Conclusion

Monitoring MySQL with MySQL Exporter, Prometheus, and Grafana provides deep visibility into database performance and health. This setup helps DevOps teams detect issues early, optimize queries, and maintain high availability in production environments.