Support Online
Skip to main content

Guide to Monitoring MongoDB with Grafana and Prometheus on Ubuntu

Noticing database performance problems late is the biggest cause of system outages.
In this guide, you will turn your MongoDB infrastructure into a professional monitoring system.
You'll catch problems early with real-time metrics.

📌 What Will You Learn in This Guide?

  • Prometheus installation
  • Configure MongoDB Exporter
  • Creating a monitoring user
  • Grafana dashboard installation
  • Alarm and security settings

⚙️ Technical Summary

This guide describes the MongoDB monitoring system on Ubuntu.
The goal is to detect performance and memory problems early.

Steps followed:

  1. Prometheus installation
  2. MongoDB Exporter configuration
  3. Prometheus target definition
  4. Grafana panel installation
  5. Alarm and security settings

🛠️ Prerequisites

Before you start:

  • VDS on Ubuntu 20.04 or above
  • Installed MongoDB service
  • Installed Grafana service
  • sudo authority

✅ Step 1: Prometheus Installation

Prometheus collects metrics at regular intervals.

1.1 Creating Users and Directories

This step ensures safe service operation.

sudo apt update
sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus
sudo mkdir -p /etc/prometheus
sudo mkdir -p /var/lib/prometheus
  • These commands create custom users and directories for Prometheus.

1.2 Prometheus Download and Installation

  1. This step installs the Prometheus binaries.
wget https://github.com/prometheus/prometheus/releases/download/v2.31.0/prometheus-2.31.0.linux-amd64.tar.gz
tar -xvf prometheus-2.31.0.linux-amd64.tar.gz
cd prometheus-2.31.0.linux-amd64
sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml

  • These commands integrate Prometheus into the system.

1.3 Service Configuration

  • This step allows Prometheus to run in the background.
sudo chown -R prometheus:prometheus /etc/prometheus/ /var/lib/prometheus/
sudo chmod -R 775 /etc/prometheus/ /var/lib/prometheus/
sudo nano /etc/systemd/system/prometheus.service
  • This command creates a service file.
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Restart=always
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus/ \
--web.listen-address=0.0.0.0:9090

[Install]
WantedBy=multi-user.target
  • This configuration starts Prometheus as a service.
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
  • These commands activate the service.

✅ Step 2: MongoDB Exporter Installation

  1. Exporter converts MongoDB metrics to Prometheus format.

2.1 Exporter Download

mkdir mongodb-exporter && cd mongodb-exporter
wget https://github.com/percona/mongodb_exporter/releases/download/v0.7.1/mongodb_exporter-0.7.1.linux-amd64.tar.gz
tar xvzf mongodb_exporter-0.7.1.linux-amd64.tar.gz
sudo mv mongodb_exporter /usr/local/bin/
  • These commands install the exporter.

2.2 Creating a Monitoring User

  1. This step provides access to MongoDB.
use admin

db.createUser({
user: "monitor_user",
pwd: "GucluSifreDegistir",
roles: [
{ role: "clusterMonitor", db: "admin" },
{ role: "read", db: "local" }
]
})

exit
  • These commands create an authorized monitoring user.

2.3 Creating Exporter Service

sudo nano /lib/systemd/system/mongodb_exporter.service
  • This command opens the service file.

[Unit]
Description=MongoDB Exporter
User=prometheus

[Service]
Type=simple
Restart=always
Environment="MONGODB_URI=mongodb://monitor_user:GucluSifreDegistir@localhost:27017"
ExecStart=/usr/local/bin/mongodb_exporter

[Install]
WantedBy=multi-user.target
  • This configuration turns the exporter into a service.
sudo systemctl daemon-reload
sudo systemctl start mongodb_exporter
sudo systemctl enable mongodb_exporter
  • These commands start the exporter.

✅ Step 3: Prometheus Target Definition

sudo nano /etc/prometheus/prometheus.yml
  • This command opens the configuration.
- job_name: "mongodb"
static_configs:
- targets: ["localhost:9216"]
  • This setting adds MongoDB metrics.

sudo systemctl restart prometheus
  • This command refreshes Prometheus.

✅ Step 4: Grafana Dashboard Installation

  1. Enter http://server-ip:3000

  2. Configuration → Data Sources

Select Prometheus

URL: http://localhost:9090
  1. Save & Test

To install Dashboard:

  1. Create → Import

  2. ID: 7353

3.Import

  • These steps install the trackpad.

🚨 Alarm (Alert) Configuration Recommendations

Recommended thresholds:

  1. CPU > 80%

  2. RAM > 90%

  3. Connection Spike > 200%

  4. Disk IO > 75%

  • You can use Grafana Alert or Alertmanager.

🔐 Safety Tips

  1. Restrict the Exporter port with firewall

  2. Turn off root MongoDB access

  3. Store passwords in .env file

  4. Use SSH key


❓ Frequently Asked Questions

1. Why do we use three tools? Exporter translates, Prometheus collects, Grafana visualizes.

2. Will system performance decrease? No. Resource consumption is low.

3. Are multiple servers monitored? Yes. Exporter is installed on each server.

4. What should I do if the dashboard appears empty? Check the Targets page.

5. Is there a problem in a cloud environment? No. It works smoothly on VDS and cloud systems.


🎯 Conclusion With this guide:

You monitored MongoDB performance You caught problems early You increased security You professionalized the system

With the GenixNode infrastructure, you can immediately install high-performance monitoring systems and grow your projects safely. 🚀