Learn MongoDB 4.x
上QQ阅读APP看书,第一时间看更新

Performance monitoring

Determining what is considered normal for your database requires gathering statistics over a period of time. The period of time varies widely, depending upon the frequency and volume of database usage. In this section, we examine two primary means of gathering statistics: the db*stats() and db.serverStatus() methods. First, we have a look at how to monitor MongoDB performance using built-in mongo shell methods that produce statistics.

Mongo shell stats() methods

The most readily available monitoring command is the stats() shell method, available at both the database and collection levels. At the database level, this shell method gives important information such as the number of collections and indexes, as well as information on the average document size, average file size, and information on the amount of filesystem storage used.

Here is an example of output from db.stats() using the sweetscomplete database:

Information given by db.<COLLECTION>.stats() (substitute the name of the collection in place of <COLLECTION>), a wrapper for the collStats database command, easily produces 10 times the amount of information as db.stats(). The output from db.<COLLECTION>.stats() gives the following general information:

In addition to the general information just described, another output key is WiredTiger, which gives detailed information on database caching, operational aspects of the B-tree algorithm used by WiredTiger, compression performance, block management, and other details specific to the internals of the WiredTiger storage engine. A detailed description of the hundreds of statistics available on WiredTiger performance is well beyond the scope of this book!

If all you need to do is to find information on the amount of storage space used by MongoDB collection components, a number of shortcut methods have been created that leverage db.collection.stats(). The following fall into this category:

Let's now turn our attention to server status.

Monitoring server status

Another extremely useful shell method used to gather information about the state of the database server is db.serverStatus(). This shell method is a wrapper for the serverStatus. database command. As with the stats() method described in the previous sub-section, this utility provides literally hundreds of statistics. An explanation of all of them is simply beyond what this book is able to cover.

For a complete list of all output information, please see the documentation under serverStatus /Output ( https://docs.mongodb.com/manual/reference/command/serverStatus/#output).

Here is a screenshot of the first few statistics:

Here is a brief summary of the more important statistics:

 

This table provides statistics that might indicate problems with your database:

As of MongoDB 4.2, the Storage Node Watchdog is in the Community (that is, free) as well as Enterprise editions of MongoDB. This feature allows MongoDB to automatically shut down the monitored mongod instance where its underlying OS filesystem becomes unresponsive. This feature can be activated by setting a MongoDB server parameter either using db.adminCommand ( { setParameter: 1,watchdogPeriodSeconds: <value>  } ) or by adding set.Parameter.watchdogPeriodSeconds to the mongod.conf file. See  https://docs.mongodb.com/master/administration/monitoring/#storage-node-watchdog.
MongoDB provides a separate utility called mongostat that provides real-time database monitoring. For more information, see the documentation on mongostat ( https://docs.mongodb.com/manual/reference/program/mongostat/#bin.mongostat).
As of MongoDB 4.0, a feature known as free monitoring is available. This feature automatically monitors execution times, memory, and CPU utilization, as well as statistics on the number of operations performed. The feature is enabled using the db.enableFreeMonitoring() command. Here is the documentation reference for more information: https://docs.mongodb.com/manual/administration/free-monitoring/#free-monitoring.

And that concludes our coverage of essential MongoDB administration techniques.