BLOG POSTS
    MangoHost Blog / How to Monitor MongoDB with Grafana and Prometheus on Ubuntu 24
How to Monitor MongoDB with Grafana and Prometheus on Ubuntu 24

How to Monitor MongoDB with Grafana and Prometheus on Ubuntu 24

Monitoring MongoDB is crucial for maintaining database performance, identifying bottlenecks, and ensuring system reliability in production environments. This comprehensive guide will walk you through setting up a robust monitoring stack using Grafana and Prometheus on Ubuntu 24, covering everything from initial installation to advanced dashboard configuration, common troubleshooting scenarios, and performance optimization techniques that will help you maintain visibility into your database operations.

Understanding the MongoDB Monitoring Stack

The Grafana-Prometheus-MongoDB monitoring architecture forms a powerful observability trio. Prometheus acts as the time-series database, collecting metrics from MongoDB through specialized exporters, while Grafana provides the visualization layer with customizable dashboards and alerting capabilities.

MongoDB metrics are exposed through the mongodb_exporter, which connects to your MongoDB instances and translates database statistics into Prometheus-compatible metrics. This approach offers several advantages over built-in monitoring tools:

  • Historical data retention with configurable storage policies
  • Multi-instance monitoring from a centralized location
  • Advanced alerting with notification integrations
  • Custom dashboard creation for specific use cases
  • Integration with existing Prometheus infrastructure

Prerequisites and System Requirements

Before diving into the setup, ensure your Ubuntu 24 system meets these requirements:

Component Minimum RAM Disk Space CPU
Prometheus 2GB 20GB 2 cores
Grafana 512MB 1GB 1 core
MongoDB Exporter 256MB 100MB 1 core

For production environments, consider upgrading to more powerful hardware. VPS services or dedicated servers provide the reliability and performance needed for monitoring critical database infrastructure.

Installing and Configuring Prometheus

Start by creating a dedicated user for Prometheus and setting up the necessary directories:

sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus

Download and install the latest Prometheus release:

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar xvf prometheus-2.45.0.linux-amd64.tar.gz
sudo cp prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
sudo cp -r prometheus-2.45.0.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-2.45.0.linux-amd64/console_libraries /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/consoles /etc/prometheus/console_libraries

Create the main Prometheus configuration file:

sudo nano /etc/prometheus/prometheus.yml

Add this configuration, which includes a job for the MongoDB exporter we’ll set up later:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

rule_files:
  - "mongodb_rules.yml"

alerting:
  alertmanagers:
    - static_configs:
        - targets: []

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  
  - job_name: 'mongodb'
    static_configs:
      - targets: ['localhost:9216']
    scrape_interval: 5s
    metrics_path: /metrics

Create a systemd service file for Prometheus:

sudo nano /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \
    --web.listen-address=0.0.0.0:9090 \
    --web.enable-lifecycle \
    --storage.tsdb.retention.time=30d

[Install]
WantedBy=multi-user.target

Start and enable Prometheus:

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
sudo systemctl status prometheus

Setting Up MongoDB Exporter

The mongodb_exporter requires proper authentication to access MongoDB metrics. First, create a monitoring user in MongoDB with minimal required permissions:

mongosh
use admin
db.createUser({
  user: "mongodb_exporter",
  pwd: "secure_password_here",
  roles: [
    { role: "clusterMonitor", db: "admin" },
    { role: "read", db: "local" }
  ]
})

Download and install the MongoDB exporter:

cd /tmp
wget https://github.com/percona/mongodb_exporter/releases/download/v0.40.0/mongodb_exporter-0.40.0.linux-amd64.tar.gz
tar xvf mongodb_exporter-0.40.0.linux-amd64.tar.gz
sudo cp mongodb_exporter-0.40.0.linux-amd64/mongodb_exporter /usr/local/bin/
sudo chown root:root /usr/local/bin/mongodb_exporter

Create a systemd service for the exporter:

sudo nano /etc/systemd/system/mongodb_exporter.service
[Unit]
Description=MongoDB Exporter
After=network.target

[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/mongodb_exporter \
    --mongodb.uri=mongodb://mongodb_exporter:secure_password_here@localhost:27017 \
    --web.listen-address=:9216 \
    --collect-all \
    --mongodb.collstats-colls=myapp.users,myapp.orders
Environment=MONGODB_URI=mongodb://mongodb_exporter:secure_password_here@localhost:27017
Restart=always

[Install]
WantedBy=multi-user.target

Start the MongoDB exporter:

sudo systemctl daemon-reload
sudo systemctl start mongodb_exporter
sudo systemctl enable mongodb_exporter
sudo systemctl status mongodb_exporter

Verify metrics collection by checking the exporter endpoint:

curl http://localhost:9216/metrics | grep mongodb

Installing and Configuring Grafana

Add the Grafana APT repository and install Grafana:

sudo apt-get install -y software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana

Start and enable Grafana:

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

Access Grafana at http://your-server-ip:3000 using the default credentials (admin/admin). Change the password when prompted.

Configuring Prometheus Data Source in Grafana

Navigate to Configuration > Data Sources and add Prometheus with these settings:

  • URL: http://localhost:9090
  • Access: Server (default)
  • Scrape interval: 15s

Test the connection to ensure Grafana can communicate with Prometheus.

Creating MongoDB Monitoring Dashboards

While you can import pre-built dashboards, creating custom ones provides better insight into your specific use cases. Here are key panels to include:

Essential MongoDB Metrics

Metric Category Key Indicators Prometheus Query Examples
Connections Active, Available mongodb_connections
Operations Insert, Query, Update, Delete rates rate(mongodb_op_counters_total[5m])
Memory Resident, Virtual, Mapped mongodb_memory
Storage Data size, Index size mongodb_dbstats_dataSize
Performance Query execution time mongodb_mongod_op_latencies_latency

Example panel configuration for operation rates:

{
  "targets": [
    {
      "expr": "rate(mongodb_op_counters_total[5m])",
      "legendFormat": "{{type}} operations/sec"
    }
  ],
  "title": "MongoDB Operations Rate",
  "type": "graph"
}

Advanced Alerting Configuration

Create alerting rules for critical MongoDB conditions. Add this to /etc/prometheus/mongodb_rules.yml:

groups:
- name: mongodb
  rules:
  - alert: MongoDBDown
    expr: up{job="mongodb"} == 0
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "MongoDB is down"
      description: "MongoDB has been down for more than 5 minutes"

  - alert: MongoDBHighConnections
    expr: mongodb_connections{state="current"} > 800
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "MongoDB high connection usage"
      description: "MongoDB connections are above 800 (current: {{ $value }})"

  - alert: MongoDBReplicationLag
    expr: mongodb_replset_member_replication_lag > 30
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "MongoDB replication lag"
      description: "MongoDB replication lag is {{ $value }} seconds"

  - alert: MongoDBHighQueryTime
    expr: mongodb_mongod_op_latencies_latency{type="query"} > 100000
    for: 3m
    labels:
      severity: warning
    annotations:
      summary: "MongoDB high query latency"
      description: "Query latency is {{ $value }}ΞΌs"

Reload Prometheus configuration:

sudo systemctl reload prometheus

Performance Optimization and Best Practices

Optimize your monitoring setup with these configurations:

Prometheus Storage Optimization

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

Add these flags to the ExecStart line for better performance:

--storage.tsdb.retention.size=10GB \
--storage.tsdb.wal-compression \
--web.enable-admin-api

MongoDB Exporter Tuning

For high-throughput environments, adjust the exporter configuration:

--collect.database \
--collect.collection \
--collect.topmetrics \
--collect.indexusage \
--compatible-mode

Common Issues and Troubleshooting

Authentication Problems

If the MongoDB exporter fails to connect, verify user permissions:

mongosh --eval "db.runCommand({connectionStatus: 1})" -u mongodb_exporter -p

Missing Metrics

Check if specific collections are being monitored:

curl -s http://localhost:9216/metrics | grep "myapp_users"

High Memory Usage

Monitor Prometheus memory consumption:

curl http://localhost:9090/api/v1/query?query=process_resident_memory_bytes

Connection Timeouts

Increase timeout values in mongodb_exporter:

--mongodb.socket-timeout=10s \
--mongodb.sync-timeout=30s

Real-World Use Cases and Examples

E-commerce Platform Monitoring

For online stores, focus on these metrics:

  • Order collection write rates during peak hours
  • User session storage performance
  • Inventory update query patterns
  • Payment processing database latency

IoT Data Collection

Monitor sensor data ingestion with custom queries:

rate(mongodb_op_counters_total{type="insert"}[1m]) * 60

Content Management Systems

Track content delivery performance:

  • Article read query performance
  • Media file metadata access patterns
  • User comment insertion rates
  • Search index utilization

Integration with Other Tools

Extend your monitoring capabilities by integrating with:

  • AlertManager: Advanced notification routing and silencing
  • Jaeger: Distributed tracing for MongoDB operations
  • ELK Stack: Log correlation with performance metrics
  • Node Exporter: System-level metrics correlation

This monitoring setup provides comprehensive visibility into MongoDB performance, enabling proactive maintenance and optimization. Regular review of dashboards and alert thresholds ensures your monitoring evolves with your application requirements. The combination of Prometheus’s powerful querying capabilities and Grafana’s flexible visualization options creates a robust foundation for database observability that scales with your infrastructure needs.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked