8 Tips to Monitor MongoDB Performance

Previous article we have covered How to Install MongoDB On Ubuntu 18.04/16.04 LTS. In this article, We are going to cover 8 tips to Monitor MongoDB performance and resource utilization.

Introduction

MongoDB is a most popular free, open source document-oriented NoSQL and cross-platform database server used for high volume data storage.It uses JSON like documents which makes the database very flexible and scalable.

Prerequisites

  • SSH access with sudo privileges
  • Preinstalled MongoDB
  • MongoDB user with Admin/root authorization

Monitor MongoDB Performance

If We are using MongoDB in Production Environment, then we should aware about to check MongoDB performance before affecting on Application performance.

MongoDB has provides many ways to Monitor performance metrics and health checks such as Free Cloud monitoring, Database commands, MongoDB Atlas, MongoDB Cloud Manager, MongoDB Ops Manager for Enterprise and MongoDB utility.Go through this link to know more about MongoDB Monitoring.

In this article , We are going to cover how to monitor MongoDB performance using MongoDB utility and Database commands .

MongoDB Monitoring Utilities:

MongoDB Provided several commands to monitor performance. Below are some commands to monitor.

1. Mongostat

Mongostat is very important monitoring command to monitor the status of the MongoDB build. It serves the following purposes.

  • It checks the status of all running mongodb instances and returns the counters (i.e. number of DB queries served) of database operations.
  • These counters include DB Queries like inserts, queries, updates, deletes, and cursors.
  • It also displays the case when you are hitting page faults, and showcase your lock percentage. This is the indicator that you are running low on memory, and the hitting write capacity is not sound and other associated performance issues.

Run below command on MongoDB instance with MongoDB username and password as show below.

$ mongostat -u "fossadmin" -p 'fosstechnix@123' --authenticationDatabase "admin"

Output:

mongostat

2. Mongotop

Mongotop is another important monitoring command to monitor the status of the MongoDB build. It serves the following purposes.

  • It is used to track and report the read and write activities of the MongoDB instance on a per collection level. By defaults mongotop returns values every second.

Run below command on MongoDB instance with MongoDB username and password as show below.

$ mongotop -u "fossadmin" -p 'fosstechnix@123' --authenticationDatabase "admin"

MongoDB Monitoring Commands:

Below are some important commands to Monitor MongoDB States.

3. serverStatus

serverStatus is a main commands to check overview metrics of MongoDB which provides some of below information.

  • Instance Information
  • Asserts
  • Connections
  • Locking
  • Operations Statistics
  • Security Overview
  • Replication Statistics
  • Storage Engine Statistics
  • Log Metrics
  • Memory Utilization
  • Concurrent Transactions

To run db.serverStatus() command login to MongoDB Instance.

$ mongotop -u "fossadmin" -p 'fosstechnix@123' --authenticationDatabase "admin"

Run below command on MongoDB Shell

> db.serverStatus()

OR

> db.runCommand( { serverStatus: 1 } )

Sample Output:

> db.serverStatus()
{
"host" : "fosstechnix",
"version" : "4.0.12",
"process" : "mongod",
"pid" : NumberLong(1506),
"uptime" : 61911,
"uptimeMillis" : NumberLong(71911869),
"uptimeEstimate" : NumberLong(71911),
"localTime" : ISODate("2019-11-09T05:08:22.489Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 2,
"rollovers" : 0
},
"connections" : {
"current" : 1,
"available" : 51199,
"totalCreated" : 2,
"active" : 1
},
"extra_info" : {
"note" : "fields vary by platform",
"page_faults" : 328
},
"freeMonitoring" : {
"state" : "undecided"

4. dbStats

dbStats provides metrics about storage usage of the database which includes number of objects, memory taken by documents and padding in the database.By default it shows data size and index size in bytes.

Here we are viewing metrics for admin database , you have to switch database to see metrics of particular database .

> db.stats()

OR

> db.stats()( { serverStatus: 1 } )

Sample Output:

> db.stats()
{
"db" : "admin",
"collections" : 2,
"views" : 0,
"objects" : 3,
"avgObjSize" : 209.33333333333334,
"dataSize" : 628,
"storageSize" : 49152,
"numExtents" : 0,
"indexes" : 3,
"indexSize" : 65536,
"fsUsedSize" : 4228214784,
"fsTotalSize" : 1065816850432,
"ok" : 1
}

To check storage usage data and index size in Kilobytes (KB)

> db.stats(1024);

To check storage usage data and index size in MB (Megabytes)

> db.stats(1024*1024);

To check storage usage data and index size in GB (Gigabytes)

> db.stats(1024*1024*1024);

5. collStats

collStats OR db.collection.stats() provides metrics similar to the dbStats output but for a specified collection like size of a collection, number of objects inside it, average size of objects, number of indexes in the collection, etc.

> db.collection.stats()

6. replSetGetStatus

replSetGetStatus provides metrics about members of your replica set: state, metrics required to calculate replication lag

> db.adminCommand( { replSetGetStatus: 1 } )

MongoDB Resource Utilization:

In above steps we have covered Monitoring MongoDB performance using utilities and commands. It is recommended to check how much resource utilizing by MongoDB Instance. Below are few commands to check resource utilization.

7. Memory Usage

To check Memory used by MongoDB use below command.

> db.serverStatus().mem

Output:

> db.serverStatus().mem
   {
        "bits" : 64,

        "resident" : 143,

        "virtual" : 1150,

        "supported" : true,

        "mapped" : 0,

        "mappedWithJournal" : 0
      }

8. Number of Connections

To get the number of clients connected , available connection limit and total connections created till now.

> db.serverStatus().connections

Output:

db.serverStatus().connections
{
        "current" : 8,

        "available" : 51192,

        "totalCreated" : 39993,

        "active" : 1
}

Conclusion:

In this article, We have performed , How to Monitor MongoDB performance and resource utilization using MongoDB utilities, Database commands and also we have covered MongoDB instance resource utilization.

Related Articles:

How to Download and Install MongoDB on Windows

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap