> For the complete documentation index, see [llms.txt](https://docs.percona.com/percona-monitoring-and-management/3/llms.txt).

# Connect MongoDB databases to PMM

Connect a MongoDB instance to PMM to monitor a [MongoDB](https://www.mongodb.com/) or [Percona Server for MongoDB](https://docs.percona.com/percona-server-for-mongodb/) database server.

## Prerequisites

Before you start, ensure you have:

- [PMM Server installed](https://docs.percona.com/percona-monitoring-and-management/3/install-pmm/install-pmm-server/index.md) and running with a known IP address or hostname accessible from the Client node.
- [PMM Client installed](https://docs.percona.com/percona-monitoring-and-management/3/install-pmm/install-pmm-client/index.md) and the nodes are registered with PMM Server.
- admin privileges to install and configure PMM Client on the host.
- preconfigured MongoDB user with appropriate monitoring privileges, or sufficient privileges to create the required roles and users.
- MongoDB server version 6.0 or higher. PMM may work with MongoDB versions as old as 4.4, but we recommend using MongoDB 6.0+ for complete feature support.

## Step 1: Set up MongoDB monitoring permissions

Set up MongoDB with a dedicated user for PMM and the required permissions. First, create custom roles with the necessary privileges, then assign them to a PMM-specific user.

Role privileges depend on:

- MongoDB version: 8.0+ requires the additional `directShardOperations` role for shard metrics
- Required features: basic monitoring only, or monitoring plus backup management.
- Query collection method: profiler or diagnostic log.

### Create monitoring role

After connecting to your MongoDB instance, create a custom role with the privileges required for metric collection, working with Query Analytics (QAN) and optionally creating/restoring backups:

#### Minimum privileges

This role grants the essential minimum privileges needed for monitoring and QAN:

```javascript
db.getSiblingDB("admin").createRole({
"role": "pmmMonitor",
"privileges": [
    {
    "resource": { "db": "", "collection": "" },
    "actions": [ "dbHash", "find", "listIndexes", "listCollections", "collStats", "dbStats", "indexStats" ]
    },
    {
    "resource": { "db": "", "collection": "system.version" },
    "actions": [ "find" ]
    },
    {
    "resource": { "db": "", "collection": "system.profile" },
    "actions": [ "find", "dbStats", "collStats", "indexStats" ]
    }         
],
"roles": [ ]
})
```

#### Full backup management privileges

If you plan to use PMM’s backup features, also create a role with full backup management privileges:

```javascript
db.getSiblingDB("admin").createRole({
    "role": "pbmAnyAction",
    "privileges": [
    {
        "resource": { "anyResource": true  },
        "actions": [ "anyAction" ]
    }
    ],
    "roles": []
});
```

### Create user and assign created role

After creating the custom roles, create the PMM user and assign the roles based on your MongoDB version and requirements:

**MongoDB 8.0+ (Standard)**

MongoDB 8.0 introduced stricter security for direct shard access. For MongoDB 8.0 and later, the PMM user also requires the `directShardOperations` role to collect complete metrics from all cluster components:

```javascript
db.getSiblingDB("admin").createUser({
    "user": "pmm",
    "pwd": "<SECURE_PASSWORD>",  // Replace with a secure password
    "roles": [
        { "db": "admin", "role": "pmmMonitor" },
        { "db": "local", "role": "read" },
        { "db": "admin", "role": "clusterMonitor" },
        { "db": "admin", "role": "directShardOperations" }
    ]
})
```

**MongoDB 8.0+ (With backups)**

If you intend to use PMM’s backup management features, create a user with grant these permissions:

```javascript
db.getSiblingDB("admin").createUser({
    "user": "pmm",
    "pwd": "<SECURE_PASSWORD>",  // Replace with a secure password
    "roles": [
        { "db": "admin", "role": "pmmMonitor" },
        { "db": "local", "role": "read" },
        { "db": "admin", "role": "readWrite", "collection": "" },
        { "db": "admin", "role": "backup" },
        { "db": "admin", "role": "clusterMonitor" },
        { "db": "admin", "role": "restore" },
        { "db": "admin", "role": "pbmAnyAction" },
        { "db": "admin", "role": "directShardOperations" }
    ]
})
```

**MongoDB \<8.0 (Standard)**

Create the PMM user with standard monitoring roles:

```javascript
db.getSiblingDB("admin").createUser({
    "user": "pmm",
    "pwd": "<SECURE_PASSWORD>",  // Replace with a secure password
    "roles": [
        { "db": "admin", "role": "pmmMonitor" },
        { "db": "local", "role": "read" },
        { "db": "admin", "role": "clusterMonitor" }
    ]
})
```

**MongoDB \<8.0 (With backups)**

If you intend to use PMM’s backup management features, create a user with these additional permissions:

```javascript
db.getSiblingDB("admin").createUser({
    "user": "pmm",
    "pwd": "<SECURE_PASSWORD>",  // Replace with a secure password
    "roles": [
        { "db": "admin", "role": "pmmMonitor" },
        { "db": "local", "role": "read" },
        { "db": "admin", "role": "readWrite", "collection": "" },
        { "db": "admin", "role": "backup" },
        { "db": "admin", "role": "clusterMonitor" },
        { "db": "admin", "role": "restore" },
        { "db": "admin", "role": "pbmAnyAction" }
    ]
})
```

## Step 2: Configure query source for MongoDB query analytics

PMM offers two methods for collecting MongoDB queries. Choose based on your environment’s requirements and constraints.

### Compare query source methods

| Feature                   | MongoDB Profiler       | Diagnostic log                            |
| ------------------------- | ---------------------- | ----------------------------------------- |
| Database connections      | Uses pool continuously | One connection at startup to get log path |
| Connection pool impact    | High                   | Minimal                                   |
| Requires `system.profile` | Yes                    | No                                        |
| Support remote instances  | Yes                    | No                                        |
| Supports `mongos`         | No                     | Yes                                       |
| Database overhead         | Moderate-High          | Minimal                                   |
| File-based logging        | No                     | Yes                                       |
| Query history durability  | Volatile               | Disk-persisted                            |
| Scales with DB count      | Linear degradation     | Constant                                  |

**MongoDB Profiler (Default)**

Choose this standard method for simple setups with fewer than 100 databases, remote MongoDB instances, or when you need real-time query collection.

The MongoDB Profiler stores query performance data in `system.profile` collections for each database. PMM continuously reads from these collections to provide query analytics.

Key advantages:

- Real-time query collection and analysis
- No additional file system access required
- Works with managed MongoDB services
- Immediate data availability after profiling is enabled

To enable the MongoDB Profiler, choose one of the following methods:

**In MongoDB configuration file (Recommended)**

This method ensures your settings persist across server restarts and system reboots. It’s the recommended approach for production environments:

1. Edit the configuration file (usually `/etc/mongod.conf`).

1. Add or modify the `operationProfiling` section in the configuration file. Pay close attention to indentation as YAML is whitespace-sensitive:

   ```text
   operationProfiling:
       mode: all             
       slowOpThresholdMs: 200
       rateLimit: 100
   ```

   These settings control the following:

   - `mode: all` - Collects data for all operations.
   - `slowOpThresholdMs: 200` - Marks operations exceeding 200ms as “slow.”
   - `rateLimit: 100` - Limits profiling sampling rate (Percona Server for MongoDB only).

   For more information about profiling configuration options, see the [MongoDB documentation](https://docs.mongodb.com/manual/reference/configuration-options/#operationprofiling-options) and the [Percona Server for MongoDB documentation](https://www.percona.com/doc/percona-server-for-mongodb/LATEST/rate-limit.html#enabling-the-rate-limit).

1. Restart the `mongod` service using the appropriate command for your system. For example, for `systemd`:

   ```sh
   systemctl restart mongod
   ```

**On CLI**

Use this method when starting the MongoDB server manually. Smaller values improve accuracy but can adversely affect the performance of your server:

```sh
mongod --dbpath=DATABASEDIR --profile 2 --slowms 200 --rateLimit 100
```

- `--dbpath`: The path to database files (usually `/var/lib/mongo`).
- `--profile`: The MongoDB profiling level. A value of `2` tells the server to collect profiling data for *all* operations. To lower the load on the server, use a value of `1` to only record slow operations.
- `--slowms`: An operation is classified as *slow* if it runs for longer than this number of milliseconds.
- `--rateLimit`: (Only available with Percona Server for MongoDB.) The sample rate of profiled queries. A value of `100` means sample every 100th fast query. ([Read more](https://www.percona.com/doc/percona-server-for-mongodb/LATEST/rate-limit.html#enabling-the-rate-limit))

**In MongoDB shell (temporary)**

This method enables profiling until the next server restart. Profiling must be enabled for **each** database you want to monitor. For example, to enable the profiler in the `testdb`, run this:

```json
use testdb
db.setProfilingLevel(2, {slowms: 0})
```

If you have already [added a service](#step-3-add-mongodb-service-to-pmm), remove it and re-add it after changing the profiling level.

**Diagnostic log (Recommended for scale)**

Choose this method for production environments with 100+ databases, when experiencing connection pool issues, or when monitoring mongos routers.

Available from PMM 3.3.0+, this method reads query data directly from MongoDB’s log files instead of querying the database. This eliminates connection pool usage and reduces performance impact.

Key advantages:

- Zero database connections required for metrics collection
- Eliminates connection pool errors completely
- Scales linearly regardless of database count
- Identical query analytics data as traditional profiler

Prerequisites for diagnostic log:

- MongoDB 5.0+ (tested with 5.0.20-17)
- Write access to the configured log directory for MongoDB process
- Read access to log file for PMM Agent user

To configure mongolog for MongoDB:

1. Choose one of the following methods to configure MongoDB to log slow operations to the diagnostic log file:

   **Config file (recommended)**

   Edit your MongoDB configuration file (`mongod.conf`):

   ```yaml
   systemLog:
   destination: file
   path: /var/log/mongodb/mongod.log
   logAppend: true
   logRotate: reopen

   operationProfiling:
   mode: off
   slowOpThresholdMs: 100
   ```

   Configuration explained:

   - `destination: file` - ensures MongoDB logs to a file (required for mongolog)
   - `path` - specifies the log file location that mongolog will read
   - `logAppend: true` - appends to existing log file instead of overwriting
   - `mode: off` - logs operations to file only (does NOT populate system.profile)
   - `slowOpThresholdMs: 100` - set based on your requirements

   Restart MongoDB after making changes:

   ```sh
   systemctl restart mongod
   ```

   **Command-line flags**

   Start `mongod` with these flags:

   ```bash
   mongod \
   --dbpath /var/lib/mongo \
   --logpath /var/log/mongodb/mongod.log \
   --logappend \
   --profile 0 \
   --slowms 100
   ```

   Flag reference:

   | Flag           | Purpose                                                 |
   | -------------- | ------------------------------------------------------- |
   | `--logpath`    | Enables logging to a file (required by mongolog)        |
   | `--logappend`  | Appends to the log file instead of overwriting          |
   | `--profile 0`  | Enables logging of slow operations (not full profiling) |
   | `--slowms 100` | Sets slow operation threshold (in milliseconds)         |

1. Create a logrotate configuration file (e.g., `/etc/logrotate.d/mongodb`) to configure log rotation:

   ```text
   /var/log/mongodb/mongod.log {
   daily
   rotate 7
   compress
   delaycompress
   copytruncate
   missingok
   notifempty
   create 640 mongod mongod
   postrotate
       /bin/kill -SIGUSR1 `cat /var/run/mongod.pid 2>/dev/null` >/dev/null 2>&1
   endscript
   }
   ```

   Critical requirements:

   - Use `copytruncate` to preserve file handle for mongolog
   - Avoid moving/renaming log files as this breaks mongolog’s file tail
   - Do not delete active log files during rotation

## Step 3: Add MongoDB service to PMM

After configuring your database server, add a MongoDB service using either the user interface or the command line.

Important

To monitor MongoDB sharded clusters, PMM requires access to all cluster components. Make sure to add all config servers, all shards, and at least one or two mongos routers. Otherwise, PMM will not be able to correctly collect metrics and populate dashboards.

**Via CLI**

Use `pmm-admin` to add the database server as a service using one of these example commands:

**Standalone MongoDB instance**

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--host=127.0.0.1 \
--port=27017
```

**With diagnostic log**

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--query-source=mongolog \
--host=127.0.0.1 \
--port=27017
```

**Replica Set or Sharded cluster component**

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--host=127.0.0.1 \
--port=27017 \
--cluster=my_cluster_or_rs_name
```

**Arbiter**

An arbiter stores no data, so it has no users and serves no queries. Add it without `--username` and `--password`, because with credentials the connection check fails. Use the same `--cluster` and `--environment` values as the other members of its replica set. Turn off Query Analytics with `--query-source=none`:

```sh
pmm-admin add mongodb \
--host=127.0.0.1 \
--port=27017 \
--cluster=my_cluster_or_rs_name \
--environment=my_environment_name \
--query-source=none
```

**Ignoring insecure server certificate**

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--host=127.0.0.1 \
--port=27017 \
--cluster=my_cluster_or_rs_name \
--tls-skip-verify
```

**SSL/TLS secured MongoDB**

Use this when MongoDB requires TLS connections and PMM logs in with a username and password. If the client certificate file has a password, also pass `--tls-certificate-key-file-password`:

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--host=fqdn_of_your_mongo_host \
--port=27017 \
--tls \
--tls-certificate-key-file=/path/to/client.pem \
--tls-ca-file=/path/to/ca.pem \
--cluster=my_cluster_or_rs_name
```

**X.509 certificate authentication**

Use this when PMM logs in with its client certificate instead of a password. MongoDB identifies the user by the certificate subject, so don’t pass `--username` or `--password`. Before you add the service, [create a user in the `$external` database](https://docs.percona.com/percona-server-for-mongodb/latest/x509-ldap.html#authenticate-with-the-x509-certificate) whose name matches the certificate subject, and give it the [roles PMM needs](#step-1-set-up-mongodb-monitoring-permissions).

```sh
pmm-admin add mongodb \
--host=fqdn_of_your_mongo_host \
--port=27017 \
--tls \
--tls-certificate-key-file=/path/to/client.pem \
--tls-ca-file=/path/to/ca.pem \
--authentication-mechanism=MONGODB-X509 \
--authentication-database='$external' \
--cluster=my_cluster_or_rs_name
```

When successful, PMM Client will print `MongoDB Service added` with the service’s ID and name. Use the `--environment` and `--custom-labels` options to set tags for the service to help identify them.

Tips

- When adding members of a replica set or sharded cluster, add each node using the same `--cluster my_cluster_or_rs_name`. This allows the [MongoDB Cluster Summary](https://docs.percona.com/percona-monitoring-and-management/3/reference/dashboards/dashboard-mongodb-cluster-summary.md) and [MongoDB ReplSetSummary](https://docs.percona.com/percona-monitoring-and-management/3/reference/dashboards/dashboard-mongodb-replset-summary.md) dashboards to populate correctly.
- Some dashboard panels, such as **Total data size** on the [MongoDB ReplSet Summary](https://docs.percona.com/percona-monitoring-and-management/3/reference/dashboards/dashboard-mongodb-replset-summary.md) and [MongoDB Sharded Cluster Summary](https://docs.percona.com/percona-monitoring-and-management/3/reference/dashboards/dashboard-mongodb-cluster-summary.md) dashboards, require the `dbstats` collector. Pass [`--enable-all-collectors`](https://docs.percona.com/percona-monitoring-and-management/3/use/commands/pmm-admin/add.md#collector-options) when adding the service to enable it.
- To collect histogram metrics from MongoDB’s diagnostic data, pass [`--enable-diagnostic-data-histograms`](https://docs.percona.com/percona-monitoring-and-management/3/use/commands/pmm-admin/add.md#collector-options). `--enable-all-collectors` doesn’t include this option, so pass it explicitly, even when you use `--enable-all-collectors`. It can significantly increase `mongodb_exporter` memory usage.
- PMM does not gather collection and index metrics if it detects you have more than 200 collections, in order to limit the resource consumption. Check the [advanced options](https://docs.percona.com/percona-monitoring-and-management/3/use/commands/pmm-admin/add.md#collector-options) section if you want to modify this behaviour.
- When running mongos routers in containers, specify the `diagnosticDataCollectionDirectoryPath` to ensure that pmm-agent can properly capture mongos metrics. For example: `mongos --setParameter diagnosticDataCollectionDirectoryPath=/var/log/mongo/mongos.diagnostic.data/`

**Via web UI**

To add a service with the UI:

1. Select **Inventory > Add service > MongoDB**.
1. Fill in the required fields.
1. Click **Add service**.

*Image: !*

**With environment variables**

Use `--agent-env-vars` to pass environment variables from `pmm-agent` to `mongodb_exporter`:

```sh
pmm-admin add mongodb \
--username=pmm \
--password=your_secure_password \
--host=127.0.0.1 \
--port=27017 \
--agent-env-vars="KRB5_KTNAME,KRB5_CONFIG"
```

PMM passes only variables that are already set in the `pmm-agent` environment to the exporter. To change the list later without re-adding the service, see [Pass environment variables to the exporter](https://docs.percona.com/percona-monitoring-and-management/3/use/commands/pmm-admin/inventory.md#pass-environment-variables-to-the-exporter).

## Step 4: Verify MongoDB service configuration

After adding MongoDB service to PMM, verify that it’s properly configured and collecting data. This ensures your monitoring setup is working correctly.

1. Check service registration:

   **Via command line**

   Look for your service in the output of this command:

   ```sh
   pmm-admin list
   ```

   For mongolog specifically, verify with:

   ```sh
   pmm-admin status
   ```

   Confirm that `mongodb_profiler_agent` appears in the output with status **Running** and query source `profiler`.

   **Via web UI**

   To check the service from the UI:

   1. Select **Inventory > Services**.

   1. Find your MongoDB service in the list and verify that its **Status** shows **Up**.

   1. Verify the **Service Name**, **Address**, and other connection details are correct.

   1. Check that the **Monitoring** column shows **OK**.

   1. To check each agent, click **OK**. The agents show these statuses:

      - **MongoDB exporter**: **Running**
      - your Query Analytics agent, **QAN MongoDB Profiler agent** or **QAN MongoDB Mongo Log agent**: **Running**
      - **Real-Time Analytics MongoDB agent**: **Running** during a Real-Time Analytics session, and **Done** otherwise

1. Verify data collection:

   - On the **MongoDB Instances Overview** dashboard
   - Set the **Service Name** to the newly-added service
   - Confirm that metrics are being displayed in the dashboard

1. Verify Query Analytics for the service:

   - Open the **PMM Query Analytics** dashboard and use the filters to select your MongoDB service.
   - Check that query data is visible (it may take a few minutes for data to appear after initial setup).
   - Performance impact is virtually zero since metrics are sourced from existing log files (for mongolog) or real-time profiler data.

## Remove MongoDB service

If you need to remove MongoDB service from PMM, follow these steps:

**Via command line**

Replace `SERVICE_NAME` with the name you used when adding the service. You can list all services with `pmm-admin`:

```sh
pmm-admin remove mongodb SERVICE_NAME
```

**Via web UI**

To remove the services through the PMM interface:

1. Go to **Inventory > Services**.
1. Select the checkbox of the service that you want to remove, then click **Delete**.
1. Select **Force mode**. PMM can’t remove a service that still has agents, and Force mode removes them with the service. Force mode also removes the scheduled backups and restore history of the service, but keeps its backup artifacts.
1. Click **Yes, delete service**.

## Related topics

- [`pmm-admin add mongodb`](https://docs.percona.com/percona-monitoring-and-management/3/use/commands/pmm-admin/add.md)
- [Troubleshooting connection difficulties](https://docs.percona.com/percona-monitoring-and-management/3/troubleshoot/config_issues.md#connection-difficulties)
