How to Set Up Prometheus MySQL Exporter (mysqld_exporter)

Updated

prometheus mysql exporter metrics

Create the user, point the exporter at a credentials file, and scrape port 9104.

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'a-long-random-password'
  WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
mysqld_exporter --config.my-cnf=/etc/mysqld_exporter.cnf

The current release is 0.19.0, published 18 March 2026.

DATA_SOURCE_NAME was removed three years ago

export DATA_SOURCE_NAME="~/.my.cnf" is wrong twice over. DATA_SOURCE_NAME was never a file path — it was a Go DSN string — and it does not exist any more. From the exporter’s own CHANGELOG, under 0.15.0 (16 June 2023), marked BREAKING CHANGES:

“The exporter no longer supports the monolithic DATA_SOURCE_NAME environment variable. To configure connections to MySQL you can either use a my.cnf style config file or command line arguments.”

The only environment variable the 0.19.0 README documents is MYSQLD_EXPORTER_PASSWORD. So there are two supported routes today:

# credentials file (what this guide uses)
mysqld_exporter --config.my-cnf=/etc/mysqld_exporter.cnf

# or flags plus one env var
export MYSQLD_EXPORTER_PASSWORD=secret
mysqld_exporter --mysqld.address=localhost:3306 --mysqld.username=exporter

The same release note carries a second trap: “The default client configuration file is now .my.cnf in the process working directory.” Under systemd the working directory is /, so if you omit --config.my-cnf the exporter looks for /.my.cnf. Always pass the flag.

A ‘#’ in the password file silently truncates it

This one costs people hours. MySQL option files treat # as a comment character, including mid-value. Reproduced against MySQL 8.4.10 with a user whose password contains a #:

$ cat exp.cnf
[client]
user=hw_exp
password=Hw#Exp2026pass

$ mysql --defaults-file=exp.cnf -e "SELECT CURRENT_USER();"
ERROR 1045 (28000): Access denied for user 'hw_exp'@'localhost' (using password: YES)

$ mysql -u hw_exp -p'Hw#Exp2026pass' -e "SELECT CURRENT_USER();"
hw_exp@localhost

The same password works on the command line and fails from the file. Quoting fixes it — password="Hw#Exp2026pass" connected successfully in the same test. Quote the value, or generate passwords without #.

And permission the file. It has your database password in it:

sudo chown mysqld_exporter:mysqld_exporter /etc/mysqld_exporter.cnf
sudo chmod 400 /etc/mysqld_exporter.cnf

What each GRANT actually buys

The SELECT grant does not enable the exporter to fetch system-wide performance data, such as transaction rates — that data needs no privilege at all. Tested on MySQL 8.4.10 by running each query as the granted user and as a control user holding no grants:

Query No grants With the three grants Gated by
SHOW GLOBAL STATUS works works nothing
SHOW GLOBAL VARIABLES works works nothing
SHOW ENGINE INNODB STATUS ERROR 1227 works PROCESS
information_schema.INNODB_METRICS ERROR 1227 works PROCESS
SHOW REPLICA STATUS ERROR 1227 works REPLICATION CLIENT
SHOW BINARY LOG STATUS ERROR 1227 works REPLICATION CLIENT
performance_schema.* ERROR 1142 works SELECT
information_schema.PROCESSLIST 1 row 2 rows PROCESS (silently)
information_schema.TABLES 86 rows 2037 rows SELECT (silently)

The exact refusals, verbatim:

ERROR 1227 (42000): Access denied; you need (at least one of) the PROCESS privilege(s) for this operation
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation
ERROR 1142 (42000): SELECT command denied to user 'hw_noperm'@'localhost' for table 'events_statements_summary_by_digest'

The last two rows do not error — they return a truncated result. An under-privileged exporter therefore fails quietly, reporting a plausible but wrong number of tables and threads.

FLUSH PRIVILEGES is not needed after the GRANT. GRANT updates the grant tables directly; the flush is only needed after editing the mysql system tables by hand.

One thing to weigh before you copy that GRANT line: SELECT ON *.* covers the mysql schema along with everything else, and a read of that schema returns every account’s stored password hash — measured on 8.4.10 with a user holding no other privilege. You can scope the grant to the schemas you want counted instead, at the cost the table above already describes: the row counts narrow with it, silently.

Metric names that do not match what the exporter emits

Metric names are built by the collectors, and reading the 0.19.0 source shows the first two names below never appear:

  • mysql_innodb_buffer_pool_bytes_data → the real name is mysql_global_status_innodb_buffer_pool_bytes_data. global_status is the subsystem, and this variable does not match any of the collector’s special-cased prefixes, so it falls through to the generic naming.
  • mysql_global_status_handler_read_rnd_next → the collector matches ^handler_(.*)$ and emits a single labelled counter instead: mysql_global_status_handlers_total{handler="read_rnd_next"}.
  • mysql_slave_status_slave_io_running → correct on older servers, wrong on MySQL 8.4. See below.

MySQL 8.4 renamed the replication columns, and the metric name follows

SHOW SLAVE STATUS is not deprecated on MySQL 8.4 — it is gone:

mysql> SHOW SLAVE STATUS;
ERROR 1064 (42000): You have an error in your SQL syntax; ... near 'SLAVE STATUS' at line 1

The exporter survives this. Its slave_status collector — enabled by default — tries three statements in order:

var slaveStatusQueries = [3]string{"SHOW ALL SLAVES STATUS", "SHOW SLAVE STATUS", "SHOW REPLICA STATUS"}

and falls through to the third on 8.4. But the metric name is derived from the column name, lowercased, under the slave_status subsystem. Reading the column metadata of SHOW REPLICA STATUS on this 8.4.10 server returns 60 columns including Replica_IO_Running, Replica_SQL_Running, Source_Host and Seconds_Behind_Source — the Slave_* names are gone. So on MySQL 8.4 the metric to alert on is:

mysql_slave_status_replica_io_running

Any dashboard or alert rule copied from a pre-8.4 guide will match nothing and stay silently green.

Collectors: defaults and opt-in flags

Only three collectors are enabled by default, per the 0.19.0 README: collect.global_status, collect.global_variables, and collect.slave_status. Everything else is opt-in by flag:

--collect.engine_innodb_status              # SHOW ENGINE INNODB STATUS      (needs PROCESS)
--collect.info_schema.innodb_metrics        # information_schema.innodb_metrics (needs PROCESS)
--collect.perf_schema.eventsstatements      # statement digests               (needs SELECT)
--collect.perf_schema.file_events           # file I/O summary                (needs SELECT)
--collect.info_schema.tables                # per-table size/row estimates    (needs SELECT)

Combined with the privilege table above, that explains why a default-configured exporter often works fine with a barely-privileged user and then starts erroring the moment someone enables an InnoDB or performance-schema collector.

Prometheus scrape config: indentation that parses

Indented like this, the scrape job does not parse:

- job_name: 'mysql_exporter'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9104']

Feeding that to a YAML parser produces ParserError: while parsing a block mapping ... line 2, column 3. The keys after job_name are indented past it, so they are not siblings. The correct block, which parses:

scrape_configs:
  - job_name: 'mysql_exporter'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9104']

A systemd unit that can read its own config

[Unit]
Description=Prometheus MySQL Exporter
After=network-online.target

[Service]
User=mysqld_exporter
Group=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter \
    --config.my-cnf=/etc/mysqld_exporter.cnf \
    --web.listen-address=127.0.0.1:9104
Restart=on-failure
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Two choices in that unit matter. Running as User=mysql would give a metrics process the database account’s file access for no reason, so use a dedicated unprivileged account. And binding to every interface would publish an unauthenticated endpoint that exposes schema names and query text, so bind to localhost, or configure TLS and basic auth via --web.config.file.

What was executed and what was not

Executed against a real MySQL 8.4.10 server: the CREATE USER ... WITH MAX_USER_CONNECTIONS syntax, the GRANT statement, the whole privilege table (each query run twice, once as a user with the three grants and once as a control user with none), the #-in-.my.cnf truncation, the SHOW SLAVE STATUS syntax error, and the 60-column metadata of SHOW REPLICA STATUS. The YAML parse failure was reproduced with a YAML parser. The checks above are scripted in prometheus-mysql-exporter.sh, which creates the two users, runs the privilege queries as each, and drops both afterwards.

Not executed: the exporter binary itself was never run, and no scrape output appears anywhere on this page. Statements about release 0.19.0, the removal of DATA_SOURCE_NAME, the default-enabled collector set, flag names, the :9104 default and metric naming come from reading the exporter’s published source and CHANGELOG at tag v0.19.0. No Prometheus server and no Grafana instance were involved.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.