Introduction
In modern DevOps practices, ensuring high performance and availability of your systems is essential. One critical aspect of this is monitoring databases. Prometheus, an open-source monitoring system, is widely adopted for its robustness, scalability, and ease of integration.
However, Prometheus doesn’t natively support monitoring MySQL or MongoDB. That’s where exporters come in. Prometheus exporters act as a bridge between Prometheus and systems like MySQL and MongoDB by exposing database metrics via HTTP endpoints.
In this post, we will walk you through setting up Prometheus exporters for both MySQL and MongoDB, helping you gather valuable performance metrics and integrate them with your Prometheus monitoring stack.
What is a Prometheus Exporter?
A Prometheus exporter is a service that collects data from systems like databases, web servers, or application performance monitoring tools and exposes it in a format that Prometheus can scrape.
Exporters typically run on the same host as the monitored service (like MySQL or MongoDB) and provide metrics on various system-level parameters. These metrics are then scraped by Prometheus at regular intervals for further processing and visualization.
What is mysqld_exporter?
The mysqld_exporter is an official Prometheus exporter developed by the Prometheus community to monitor MySQL and MariaDB databases.
It connects to your MySQL instance using a low-privileged user and collects detailed internal metrics—such as query throughput, cache hit ratios, connection statistics, and replication status. These metrics are then exposed via an HTTP endpoint (usually at :9104), ready to be scraped by Prometheus.
This exporter is lightweight, highly configurable, and designed to provide deep insights into the performance and health of your MySQL server.
Setting Up the MySQL Exporter
Prometheus uses the mysqld_exporter to gather MySQL-specific metrics. Let’s dive into the installation and configuration process.
Step 1: Install the MySQL Exporter
The easiest way to install the MySQL exporter is through a precompiled binary. You can download the latest version from the Prometheus MySQL Exporter GitHub repository.
For Ubuntu or Debian, you can install it with:
sudo apt-get update
sudo apt-get install prometheus-mysqld-exporter
Step 2: Configure the MySQL Exporter
The mysqld_exporter connects to MySQL using a specific user account. Create a MySQL user with the necessary privileges:
CREATE USER 'prometheus'@'localhost' IDENTIFIED BY 'your-password';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'prometheus'@'localhost';
FLUSH PRIVILEGES;
Step 3: Run the Exporter
Start the MySQL exporter by passing the connection string (DSN) to the exporter. The basic syntax is:
mysqld_exporter --config.my-cnf=/path/to/your/.my.cnf
Make sure your .my.cnf file contains the appropriate MySQL credentials.
Step 4: Configure Prometheus to Scrape MySQL Metrics
Default prometheus configuration file path:
sudo nano /etc/prometheus/prometheus.yml
Add the following job to your Prometheus configuration file:
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104']
Prometheus will now scrape metrics from the MySQL exporter at the specified interval.
Step 5: Configure and Run as a Service
Modify the exporter’s service config to include the path to the .my.cnf file if necessary. Then run:
sudo systemctl daemon-reload
sudo systemctl enable mysqld_exporter
sudo systemctl start mysqld_exporter
Step 6: Verify in Browser
http://localhost:9104/metrics
You should see Prometheus-style metrics text in the browser.
Step 7: Key MySQL Metrics
Here are some important MySQL metrics to track:
- mysql_global_status_threads_connected — Number of current connections.
- mysql_global_status_queries — Number of queries executed.
- mysql_global_status_slow_queries — Detect inefficient query patterns.
- mysql_global_status_innodb_buffer_pool_pages_dirty — Helps tune memory use.
- mysql_slave_status_seconds_behind_master — Replication lag, if using replication.
What is mongodb_exporter?
The mongodb_exporter is a service designed to extract metrics from MongoDB instances and expose them in Prometheus-compatible format.
One of the most widely used versions is maintained by Percona, and it provides comprehensive support for various MongoDB environments including standalone nodes, replica sets, and sharded clusters.
It collects statistics such as insert/update/delete operation counts, memory usage, active connections, index usage, and replication status. Metrics are typically exposed at :9216 and can be secured using authentication mechanisms.
Setting Up the MongoDB Exporter
The mongodb_exporter helps you monitor MongoDB instances, exposing key metrics for replica sets, memory usage, and operations.
Step 1: Install the MongoDB Exporter
To install the mongodb_exporter, use the following command:
For Ubuntu/Debian
sudo apt-get update
sudo apt-get install prometheus-mongodb-exporter
Alternatively, you can download the binary from the MongoDB exporter GitHub page.
Step 2: Configure the MongoDB Exporter
Start the MongoDB exporter with a basic configuration, providing the necessary MongoDB URI:
mongodb_exporter --mongodb.uri="mongodb://your_mongo_user:your_password@localhost:27017"
Step 3: Configure Prometheus to Scrape MongoDB Metrics
Default prometheus configuration file path:
sudo nano /etc/prometheus/prometheus.yml
Add the following to your Prometheus config file to scrape MongoDB metrics:
scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['localhost:9216']
Step 4: Run and Enable as a Service
sudo systemctl daemon-reload
sudo systemctl enable mongodb_exporter
sudo systemctl start mongodb_exporter
Step 5: Verify in Browser
http://localhost:9216/metrics
You should see MongoDB-related metrics in Prometheus format.
Step 6: Key MongoDB Metrics
Here are some critical MongoDB metrics to track:
- mongodb_opcounters_insert: Number of insert operations.
- mongodb_connections: Number of active connections.
- mongodb_mem_resident: Amount of memory used by MongoDB.
Combining Metrics from Both Databases
You can run both MySQL and MongoDB exporters simultaneously. In your Prometheus configuration, define separate scrape jobs for each exporter:
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104']
- job_name: 'mongodb'
static_configs:
- targets: ['localhost:9216']
This setup allows you to monitor both databases independently while using the same Prometheus instance.
Best Practices for Monitoring
Security:
Ensure that the MySQL and MongoDB exporters are only accessible from trusted networks. Use basic authentication or IP whitelisting to restrict access.
Performance:
Be mindful of scrape intervals. Scraping too frequently can impact your databases’ performance, while infrequent scrapes may lead to missing important metrics.
Alerting:
Set up alerts in Prometheus or Grafana for critical metrics such as replication lag or high connection counts.
Conclusion
Integrating MySQL and MongoDB with Prometheus using exporters provides a powerful way to track performance and ensure the health of your databases. By monitoring critical metrics like query performance, replication status, and memory usage, you can proactively manage your databases and prevent issues before they impact your users.
With these exporters in place, you’re well on your way to building a robust observability stack using Prometheus and Grafana.
For more in-depth details, check out the official documentation for Prometheus MySQL Exporter and MongoDB Exporter.