MongoDB monitoring tools.
Backup and restore with MongoDB.
MongoDB Monitoring
MongoDB provides various tools to monitor the database. These MongoDB monitoring tools help us to understand what is happening with a database at various levels.
Log File
The first basic tool is the log file.
Recipe 7-1. Working with MongoDB Log Files
In this recipe, we are going to discuss MongoDB log files.
Problem
You want to see the MongoDB log file and set the log level.
Solution
Use the db.setLogLevel function to set the log level.
How It Works
Let’s follow the steps in this section to see the log file in mongo shell.
Step 1: To Display the Log File Content and Set the Log Level
We cannot filter the log messages in the console.
MongoDB has several logging levels, which can be set using the db.setLogLevel function.
MongoDB echoes back the previous configuration before the new setting was applied. At the top, you can see the default level, which indicates that any components that are not set will log at this level. Here, it will override only the query component. The -1 verbosity indicates that it inherits the default level from its parent.
Here, the query level is set to 2.
This query will not return any results because there is no collection named demos in the test database.
You can see some additional information about the query.
Now check the end of the log file: We cannot see the extra logging information for this find query.
Use db.setLogLevel(0) to turn off the logging level.
If we do not specify the option as query or write or anything, it will be applied for all options.
The log level 5 is very detailed; levels 1 to 3 are more useful options and these are recommended unless you need all of the detailed information provided by the highest log level of 5.
Also, setting the appropriate log level helps us to improve the performance of queries by identifying the query plans logged in the log file and also by identifying the slow operations by checking the time taken to execute a query.
Note
Refer to Recipe 7-10 to learn more about MongoDB query plans.
MongoDB Performance
It is mandatory to analyze the performance of the database when we develop new applications with MongoDB. Performance degradation could happen due to hardware availability issues, number of open database connections, and database access strategies.
Performance issues indicate the database is operating at full capacity and abnormal traffic load due to an increase in the number of connections.
The database profiler can help us to understand the various operations performed on the database that cause performance degradation.
Database Profiler
The database profiler is used to collect information about the commands that are executed against a running mongod instance. The database profiler writes all the collected data to the system.profile capped collection in the admin database.
MongoDB also has a Performance Advisor tool that automatically monitors slow queries and suggests new indexes to improve query performance. A query is considered to be slow if it takes more than 100 milliseconds to execute.
Note
The Performance Advisor tool is available in the MongoDB Atlas cluster. MongoDB Atlas is the global cloud database service for modern applications.
Profiling Levels
Level | Description |
---|---|
0 | The profiler is off and does not collect any data. This is the default profiler level. |
1 | The profiler collects data for operations that take longer than the value of the slowms parameter. |
2 | The profiler collects data for all operations. |
Recipe 7-2. Working with Database Profiler
In this recipe, we are going to discuss how to enable database profiler and how to set the profiling level.
Problem
You want to enable database profiling with a profile level.
Solution
Use the db.setProfilingLevel() helper in the mongo shell.
How It Works
Let’s follow the steps in this section to enable the database profiler.
Step 1: Enable and Configure Database Profiler
was is the key/value pair indicating the previous level of profiling.
Here, the slow operation threshold is 30 milliseconds.
That code sets the profiler to sample 53% of all slow operations.
Note
The default threshold value for the slow operation is 100 milliseconds.
The was field indicates the current profiling level.
This command sets the profiling level to 1, the slow operation threshold to 20 milliseconds, and it profiles only 50% of the slow operations.
Recipe 7-3. View Database Profiler
In this recipe, we are going to discuss how to view database profiler.
Problem
You want to view database profiler information.
Solution
Use db.system.profile.find() in the mongo shell.
How It Works
Let’s follow the steps in this section to view database profiler information.
Step 1: Enable and Configure Database Profiler
The database profiler logs information in the system.profile collection. You need to query system.collection to view profiling information.
op: Component type (i.e., query or command).
ts: Timestamp.
ns: Collection details where the query is executed.
These options can be used to filter or sort as in the queries that follow.
Note
ts specifies the timestamp and the value -1 specifies to sort in descending order. We can specify 1 for ascending or -1 for descending order.
mongostat
mongostat is a monitoring utility that provides information about a mongod instance if you are working on a single mongod instance. If you are working on a shared cluster, it shows information about a mongos instance.
Recipe 7-4. Working with mongostat
In this recipe, we are going to discuss how to use mongostat to monitor the MongoDB server.
Problem
You want to see the details of a MongoDB server.
Solution
Use the mongostat tool, which is available in the installation directory. Usually the installation directory is C:Program FilesMongoDBServer4.0in or the custom path specified during your installation process.
Avoid navigating to the directory each time to use the mongostat tool and add the directory path to your PATH environment variable.
How It Works
Let’s follow the steps in this section to view server information using mongostat.
Step 1: mongostat Command
Issue the mongostat command by specifying the hostname and port of MongoDB server.
Note
27017 is the default port. We can simply use mongostat without any options if we want to connect and get statistics of localhost and default port 27017. To connect a different or remote instance, specify the option -host as shown.
insert/query/update/delete: These columns show the number of insert, query, update, and delete operations per second.
getmore: This column shows the number of times the getmore operation is executed in one second.
command: This column shows the number of commands executed on the server in one second.
flushes: This column shows the number of times data was flushed to disk in one second.
mapped: This column shows the amount of memory used by the mongo process against a database. It is the same as the size of the database.
vsize (virtual size): This column represents virtual memory allocated to the entire mongod process.
res (resident memory): This column represents the physical memory used by MongoDB.
faults: This column shows the number of Linux page faults per second.
qr|qw: This column shows queued-up reads and writes that are waiting for the chance to be executed.
ar|aw: This column shows number of active clients.
netIn and netOut: These columns show the network traffic in and out of the MongoDB server within a given time frame.
conn: This column shows the number of open connections.
time: This column shows the time frame in which operations are performed.
mongotop
mongotop provides a method to track the amount of time spent by the mongod process during reads and writes. mongotop provides statistics on a per-collection level.
Recipe 7-5. Working with mongotop
In this recipe, we are going to discuss how to use mongotop to track the amount of time spent by the mongod process during reads and writes.
Problem
You want to see the amount of time spent by the mongod process during reads and writes.
Solution
Issue the mongotop command in the installation path of MongoDB.
How It Works
Let’s follow the steps in this section to view the amount of time spent during reads and writes.
Step 1: mongotop Command
db.stats()
db.stats() provides the statistics of a single database.
Recipe 7-6. Working with db.stats()
In this recipe, we are going to discuss how to get the statistics for a single database.
Problem
You want to see the disk and memory usage estimates for a database.
Solution
Use the db.stats command to display the statistics for a database.
How It Works
Let’s follow the steps in this section to view the statistics for the database.
Step 1: db.stats() Command
db.serverStatus()
db.serverStatus() returns a document that provides statistics for the state of a database process.
Recipe 7-7. Working with db.serverStatus()
In this recipe, we are going to discuss the db.serverStatus() command .
Problem
You want to see the memory status of a database.
Solution
Use the db.serverStatus() command .
How It Works
Let’s follow the steps in this section to view the statistics for the memory status of a database.
Step 1: db.serverStatus() Command
Backup and Restore with MongoDB Tools
MongoDB provides mongodump and mongorestore utilities to work with BSON data dumps. These utilities are useful for creating backups for small deployments. To create resilient and nondisruptive backups for large deployments, we can use file system or block-level disk snapshot methods.
We should use a system-level tool to create a copy of the device file system that holds MongoDB files. The file system snaphots or the disk-level snapshot backup method require additional system configuration outside of MongoDB, so we do not cover this in depth in this book.
When deploying MongoDB in production, we should have a backup and failover strategy for capturing and restoring backups in the case of data loss events.
Recipe 7-8. Working with mongodump
In this recipe, we are going to discuss how to back up data using mongodump.
Problem
You want to back up data using mongodump.
Solution
Use the mongodump command.
How It Works
Let’s follow the steps in this section to back up data.
The mongodump utility makes a backup by connecting to a running mongod or mongos instance.
When you run the mongodump command without specifying any arguments, it connects to the mongodb running on localhost on port 27017 and creates a database backup named dump in the current directory.
- 1.
Ensure mongod is running.
- 2.
Open a command prompt with administrator privileges, navigate to the mongodb installation folder, and type mongodump as shown here.
c:Program FilesMongoDBServer4.0in>mongodump - 3.
You can see the dump folder in the current directory as shown in Figure 7-1.
Automatic and Regular Backup Scheduling Using mongodump
- 1.
We can create scripts to create the backup directory for the current date and time and export the database to the same directory.
For Windows, create a bat (batch script) file with the commands shown in Figure 7-2.
- 2.
Schedule the script file to execute at a certain interval. Use task scheduler or any other scheduler for Windows, and use the CRON schedule for Unix/Linux.
Recipe 7-9. Working with mongorestore
In this recipe, we are going to discuss how to restore data using mongorestore.
Problem
You want to restore data using mongorestore .
Solution
Use the mongorestore command.
How It Works
Let’s follow the steps in this section to restore data.
Step 1: Restore Data Using mongorestore
Step 2: Restore Data Using mongorestore to Remote Instances
Specify the username and password using options --user and --password only if the remote mongod instance requires authentication.
Recipe 7-10. Working with mongodb Query Plans
In this recipe, we are going to discuss query plans to understand the MongoDB query execution details with query planner and query optimizer.
Problem
You want to understand the query execution plan.
Solution
Use the cursor.explain() command.
How It Works
Let’s follow the steps in this section to understand the query plan.
Step 1: Using explain() to Understand the Query Plan
Observe in this output that there is no parsedQuery in the query planner, and the winning plan says that it involves the complete collection scan.
Step 2: Understanding Different Modes in explain()
The explain() method accepts queryPlanner, executionStats, or allPlansExecution as the operating mode. The default mode is queryPlanner if we don’t specify otherwise.
"executionSuccess" : true: This shows whether the query is successfully executed or not.
"nReturned" : 5: This is the number of rows returned.
"executionTimeMillis" : 0: This is the execution time in milliseconds.
If the executionTimeMillis value is more, then it could be considered a slow query and we must rewrite the query to optimize the executiuon time by adding the required filters to retrieve only the required data. We could also consider using an index on keys for better performance.