The Prometheus MySQL exporter (mysqld_exporter) periodically queries your MySQL server and exposes the results as Prometheus gauge metrics (for example query-related metrics and InnoDB buffer pool metrics).
Prometheus is an open-source monitoring and alerting platform. It can be used with a visualization tool like Grafana to build dashboards, query metrics, and set alerts.
You will configure Prometheus to scrape the exporter metrics, and optionally visualize them in Grafana.
Prerequisites
Before proceeding, you need:
- A working MySQL server
- A working Prometheus server (if you don’t have one yet):
Install and configure mysqld_exporter
Download the exporter binary and install it into /usr/local/bin.
Releases: https://github.com/prometheus/mysqld_exporter/releases
| |
Confirm the installation by checking mysqld_exporter version:
| |
Create a dedicated system user
Create a non-login system user for the exporter:
| |
Create a MySQL user for scraping
Create a MySQL user that can query the metrics:
| |
The MAX_USER_CONNECTIONS setting limits connections so monitoring scrapes don’t overload your MySQL server under heavy load.
Configure exporter credentials (/etc/mysqld_exporter.cnf)
Create the credentials file:
| |
Then lock down permissions and ownership:
| |
Run mysqld_exporter with systemd
Create the systemd unit:
| |
Reload systemd and start:
| |
Confirm the exporter is running:
| |
And test the metrics endpoint:
| |
Configure Prometheus scraping
Edit prometheus.yml and add a scrape job for the exporter. Example:
| |
Notes:
- In the example, Prometheus scrapes the exporter from the same host (
127.0.0.1). - If Prometheus is on a different host, set
targetsto the exporter host/IP and make sure the exporter is reachable over the network. - For security, the exporter in this guide binds to
127.0.0.1. If Prometheus is remote, you can either:- change
LISTEN_ADDRESSto a non-loopback interface and open firewall rules, or - run Prometheus on the same host, or
- use a reverse proxy/VPN network to restrict access.
- change
Grafana dashboards (optional)
- Add Prometheus as a data source:
- Name:
Prometheus - Type:
Prometheus - URL:
http://localhost:9090(orhttp://PROMETHEUS_HOST:9090)
- Name:
- Create dashboards from metrics, or import a ready-made one:
- Percona Grafana dashboards: https://github.com/percona/grafana-dashboards
Security best practices
- Keep
/etc/mysqld_exporter.cnfprivate (chmod 600) - Bind the exporter to
127.0.0.1by default - Use least-privilege MySQL grants
- Monitor exporter impact under load (reduce scrape frequency or collection flags if needed)
Troubleshooting
Exporter can’t connect to MySQL
- Check service logs:
| |
- Verify the credentials file:
| |
- Confirm the MySQL user exists and is allowed to connect from
localhost.
Prometheus target is down
- From the Prometheus host, verify you can reach the exporter endpoint:
| |
- Verify firewall/security group rules and that the exporter
LISTEN_ADDRESSmatches your target connectivity.
Summary
You now have mysqld_exporter running on Linux, Prometheus scraping MySQL metrics, and a path to visualize them in Grafana with ready-made dashboards.