DEV Community

Ali Ghaemi
Ali Ghaemi

Posted on

How to Set Up MariaDB/MySQL Exporter on Galera Cluster Nodes for Prometheus Monitoring

Hello, dev.to community! 👋

In this post, I’ll walk you through the steps I took to set up the MariaDB/MySQL Exporter on Galera Cluster nodes for monitoring with Prometheus. In my case, I’m running a MariaDB Galera Cluster on my cloud server provider with three nodes and using Prometheus + Grafana to monitor my setup. The Galera Cluster setup and monitoring tools have been incredibly useful, but getting the right metrics from MariaDB wasn’t straightforward, so I decided to document my solution.

To monitor MariaDB and Galera Cluster metrics such as the number of queries per second, connections, replication status, etc., you need to use a MySQL Exporter in combination with Prometheus. This exporter will expose database-specific metrics, including Galera and MariaDB statistics.

Steps to Set Up MariaDB/MySQL Exporter on Galera Cluster Nodes:

Prerequisites

  • A MariaDB Galera Cluster with multiple nodes.
  • A Prometheus server up and running.
  • Grafana for visualizing metrics (optional but recommended).
  • Access to the nodes to install the exporter.

Note: You can use already Prometheus Grafana's machins on marketplace of your cloud server provider. Hetzner, Digitalocean

Step 1: Install MySQL/MariaDB Exporter

On each Galera node, we need to install the MySQL Exporter, which collects metrics and exposes them for Prometheus. Here’s how you can do that:

Download and install the exporter from the official source

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

Extract the files

tar xvfz mysqld_exporter-0.15.1.linux-amd64.tar.gz

Move the binary to a system path

sudo cp mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter /usr/local/bin/

Add a system user for the exporter

sudo useradd -rs /bin/false exporter

Step 2: Configure Exporter with MySQL/MariaDB Credentials

The exporter needs access to the MariaDB metrics. Create a .my.cnf file in the exporter’s home directory to store the connection credentials:

sudo mkdir /etc/.mysqld_exporter
sudo nano /etc/.mysqld_exporter/.my.cnf

Add the following to the file:

[client]
user=exporter
password=yourpassword
Enter fullscreen mode Exit fullscreen mode

Make sure the user has the proper permissions to access the necessary metrics:

to access use mysql -u root or mariadb -u root

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

Step 3: Set Up the Exporter as a Service

To ensure the MySQL Exporter starts on boot, let’s configure it as a systemd service:

sudo nano /etc/systemd/system/mysqld_exporter.service

Add the following content to the file:

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

[Service]
User=mysqld_exporter
Group=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter \
  --config.my-cnf=/etc/.mysqld_exporter/.my.cnf

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Save the file and reload systemd:

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

Step 4: Add Exporter Targets to Prometheus

Next, update your Prometheus configuration to scrape the metrics from the nodes. Add the following to your prometheus.yml file:

Before that you can check if MySQL Exporter is running by visiting:

curl http://<MariaDB-nodes-ip>:9104/metrics

to access this file for example prometheus.yml use:
sudo nano /opt/containers/prometheus-grafana/prometheus/prometheus.yml

Add inside scrape_configs::

- job_name: 'mariadb-galera'
  static_configs:
    - targets: ['node1-ip:9104', 'node2-ip:9104', 'node3-ip:9104']
Enter fullscreen mode Exit fullscreen mode

replease you ip of your database nodes

Then, reload Prometheus:

sudo systemctl reload prometheus

Step 5: Visualize in Grafana

If you’re using Grafana, you can import pre-built dashboards for MySQL/MariaDB monitoring. One popular dashboard ID for MySQL/MariaDB metrics is 7362.

To import:

  1. Open Grafana.
  2. Navigate to Dashboards > Import.
  3. Enter 7362 in the "Grafana.com Dashboard" field, then click Load.

You should now see detailed metrics from your MariaDB Galera Cluster nodes in your Grafana dashboards!

Conclusion

With the MariaDB Exporter set up on each Galera Cluster node, Prometheus can now scrape and monitor key database metrics. This setup provides better insight into your cluster’s health and performance, making it easier to troubleshoot and optimize as needed.

Feel free to leave any questions or comments below if you're setting up something similar or have any suggestions. Thanks for reading!

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay