MongoDB Monitoring
What Will You Learn in This Guide?
In this guide, you will learn how to monitor MongoDB performance.
You will follow the steps of collecting metrics with Prometheus and visualizing with Grafana.
You will establish an end-to-end monitoring infrastructure using MongoDB Exporter.
Stage 1 – Technical Analysis (Summary)
Main topic: MongoDB monitoring and metrics collection.
Resolved problem: Failure to detect performance, memory and connection issues early.
Steps followed: Prometheus installation, MongoDB Exporter configuration, Grafana dashboard integration.
1. Prerequisites
The following should be ready before continuing:
- A server running Ubuntu 20.04
- MongoDB installed and running
- Grafana installed and accessible
- UFW firewall active
2. Prometheus Installation and Configuration
Prometheus collects and stores time series metrics.
2.1 System Update
This command updates the package list:
sudo apt update
2.2 Creating Directories
- These commands create Prometheus directories:
sudo mkdir -p /etc/prometheus
sudo mkdir -p /var/lib/prometheus
2.3 Prometheus Download and Installation
- This command downloads the Prometheus package:
wget https://github.com/prometheus/prometheus/releases/download/v2.31.0/prometheus-2.31.0.linux-amd64.tar.gz
- This command unpacks the archive:
tar -xvf prometheus-2.31.0.linux-amd64.tar.gz
- This command moves binaries to the system:
sudo mv prometheus promtool /usr/local/bin/
- This command moves the configuration files:
sudo mv consoles console_libraries /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml
2.4 Running Prometheus as a Service
- This command creates Prometheus user:
sudo useradd -rs /bin/false prometheus
- This command sets the permissions:
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
- This service file starts Prometheus:
[Unit]
Description=Prometheus
After=network.target
[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus
[Install]
WantedBy=multi-user.target
- This command starts the service:
sudo systemctl start prometheus
sudo systemctl enable prometheus
- Prometheus is now accessible on port :9090.
3. MongoDB Exporter Installation
- MongoDB Exporter delivers MongoDB metrics to Prometheus.
3.1 Exporter Download
- This command downloads the exporter:
wget https://github.com/percona/mongodb_exporter/releases/download/v0.7.1/mongodb_exporter-0.7.1.linux-amd64.tar.gz
- This command opens the file:
tar -xvzf mongodb_exporter-0.7.1.linux-amd64.tar.gz
- This command adds the exporter to the system:
sudo mv mongodb_exporter /usr/local/bin/
3.2 Creating a MongoDB Monitoring User
- Open the MongoDB shell:
mongo
Admin veritabanına geçin:
use admin
- This command creates the monitoring user:
db.createUser({
user: "monitor",
pwd: "securepass",
roles: [
{ role: "clusterMonitor", db: "admin" },
{ role: "read", db: "local" }
]
})
3.3 Creating the Exporter Service
- This service runs MongoDB Exporter:
[Unit]
Description=MongoDB Exporter
[Service]
ExecStart=/usr/local/bin/mongodb_exporter
Restart=always
[Install]
WantedBy=multi-user.target
- This command starts the service:
sudo systemctl daemon-reload
sudo systemctl start mongodb_exporter
- For metric validation:
curl http://localhost:9216/metrics
4. Adding MongoDB Exporter to Prometheus
- Open Prometheus configuration:
sudo nano /etc/prometheus/prometheus.yml
- This target adds MongoDB Exporter:
scrape_configs:
- job_name: "mongodb"
static_configs:
- targets: ["localhost:9216"]
- This command restarts Prometheus:
sudo systemctl restart prometheus
5. Grafana Dashboard Setup
- Log in to the Grafana interface from port :3000.
5.1 Adding a Prometheus Data Source
-
Configuration → Data Sources
-
Add Data Source → Prometheus
URL: http://localhost:9090*
5.2 MongoDB Dashboard Import
- Use Grafana dashboard ID:
7353
- This dashboard visualizes MongoDB metrics.
Frequently Asked Questions (FAQ)
1. Is MongoDB Exporter secure in production? Yes, it works with read-only privileges.
2. Can I set an alarm on Grafana? Yes, alarms can be defined based on metric thresholds.
3. Does it work on a single server? Yes, it is suitable for small and medium-sized systems.
4. Can the cluster be monitored? Yes, clusterMonitor supports the role.
Result
With this guide, you have set up a complete monitoring infrastructure for MongoDB. Prometheus collects metrics, Grafana visualizes them. Performance issues can now be detected earlier.
You can safely monitor your production systems by using this structure in the GenixNode infrastructure.

