MongoDB Administration
Introduction
Nowadays, Usage of MongoDB has grown up rapidly, but the main question is ??? How efficiently are you running MongoDB on your environment ???
Planning your environment and setting things up for success is great. In most of the cases, Administration and Monitoring of Mongo is “Taken with a grain of salt”.
Monitoring Tools & Technique
-
Log File – mongod.log
The basic tool for monitoring activities in mongo is using its log file. Most basic insights can be collected through a mongod.log file. When mongo feels something is off it logs it into the log file. The location of the log file may vary depending upon the operating system used. I am using Ubuntu 16.04 for demonstration, and my log file location is /var/log/mongodb/mongod.log
You can watch or tail the logs and see the output using below listed command.
root@ubuntu-OptiPlex-330:~# tail -f /var/log/mongodb/mongod.log
2017-09-06T09:38:12.436+0000 [initandlisten] connection accepted from 127.0.0.1:40025 #90 (27 connections now open) 2017-09-06T09:40:15.105+0000 [clientcursormon] mem (MB) res:200 virt:4564 2017-09-06T09:40:15.105+0000 [clientcursormon] mapped (incl journal view):4320 2017-09-06T09:40:15.105+0000 [clientcursormon] connections:27 2017-09-06T09:40:54.580+0000 [initandlisten] connection accepted from 127.0.0.1:40030 #91 (28 connections now open) 2017-09-06T09:41:04.890+0000 [initandlisten] connection accepted from 127.0.0.1:40035 #92 (29 connections now open) 2017-09-06T09:41:32.973+0000 [initandlisten] connection accepted from 127.0.0.1:40037 #93 (30 connections now open) 2017-09-06T09:41:50.899+0000 [conn12] end connection 127.0.0.1:39856 (29 connections now open)
Mongo has several logging levels. The log verbosity level can range from 0 to 5, 0 is the quietest and 5 is the most verbose level. More details regarding this can be found here.
-
Query Profiler
The process of fetching and logging slow queries is known as profiling. Another tool for enhancing performance in MongoDB is Query Profiler. Mongo has the facility to log slow queries and these queries can be reviewed later.
Mongo has 3 profiling levels (0,1 and 2).
Level 0 – No profiling data available
Level 1 – Slow operations are logged
Level 2 – All Operations are logged
To see the current profiling level, command db.getProfilingLevel() is executed. Firstly, switch to mongo CLI using command, mongo
root@ubuntu-OptiPlex-330:~# mongo
Execute the below command to get the current profiling status.
> db.getProfilingLevel()
Output
> db.getProfilingLevel() 0 >
Current profiling level is 0, we are going to change it to profiling level 2.
> db.setProfilingLevel(2)
Output
> db.setProfilingLevel(2) { "was" : 0, "slowms" : 100, "ok" : 1 }
Mongo prints the old profiling level and says OK, which means profiling level is turned to 2 now. This can be verified by re-running command db.getProfilingLevel()
> db.getProfilingLevel() 2 >
Fetching Data from Query Profiler
The database profiler logs information about database operations in the system.profile collection. The profiling data is available for querying from this special collection named system.profile.
Below are the few examples for fetching data from collection system.profile.
To return the most recent 10 log entries in the system.profile collection, run a query similar to the following:
> db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()
To return all operations except command operations ($cmd), run a query similar to the following:
> db.system.profile.find( { op: { $ne : 'command' } } ).pretty()
To return operations for a particular collection, run a query similar to the following. This example returns operations in the mydb database’s test collection:
> db.system.profile.find( { ns : 'mydb.test' } ).pretty()
To return operations slower than 5 milliseconds, run a query similar to the following:
> db.system.profile.find( { millis : { $gt : 5 } } ).pretty()
-
mongostat
Mongostat is part of the distribution and provides metrics on several key things. It fetches runtime statistics from mongod servers and continuously pols and displays its values. Mongostat has the ability to display the stats of local mongo server as well as remote mongo server.
To connect to local mongo server
root@ubuntu-OptiPlex-330:~# mongostat
To connect to remote mongo server
root@ubuntu-OptiPlex-330:~# mongostat --host myserver --port 27017
Output of mongostat
root@ubuntu-OptiPlex-330:~# mongostat connected to: 127.0.0.1 insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time *0 *0 *0 *0 0 7|0 0 2.11g 9.83g 280m 0 test:0.0% 0 0|0 0|0 1k 6k 33 10:52:17 *0 *0 *0 *0 0 1|0 0 2.11g 9.83g 280m 0 test:0.0% 0 0|0 0|0 62b 4k 33 10:52:18 *0 *0 *0 *0 0 6|0 0 2.11g 9.83g 280m 0 test:0.0% 0 0|0 0|0 1k 5k 33 10:52:19 *0 *0 *0 *0 0 5|0 0 2.11g 9.83g 280m 0 test:0.0% 0 0|0 0|0 1k 5k 33 10:52:20
-
mongotop
montop gives you information about, where mongo is spending its most of time. It shows the most active collections and helps you focus on the time spent on those collections. Deeply, It shows the time spent doing read & writes in tracked most active collections of the database.
root@ubuntu-OptiPlex-330:~# mongotop
Output of mongotop
root@ubuntu-OptiPlex-330:~# mongotop connected to: 127.0.0.1 ns total read write 2017-09-06T11:10:18 demo.fisrt 368ms 367ms 1ms demo.second 225ms 224ms 1ms demo.third 2ms 2ms 0ms
Using the above tools, We believe Administration and Monitoring of MongoDB will become a “Piece Of cake.” Cheers!
Thanks for the tutorial, I found it useful.