How to Configure MongoDB Database Profiling Using PHP
Introduction
- This tutorial will explain how to configure MongoDB database profiling using PHP. The MongoDB Profiler is a built-in tool that will provide you with actual query-level insights and allow you to examine all the queries that are being run on your system. The MongoDB Profiler is able to capture various levels of system performance information, depending on how you configure the profiler, and this can be executed using either the Mongo shell or with PHP.
Prerequisites
Before you begin, confirm that MongoDB and the MongoDB PHP driver is properly configured.
You can use the following command to see if you have the MongoDB driver installed:
1 | pecl search mongo |
- The system should return something like this:
1 2 3 | Package Stable/(Latest) Local mongo 1.6.16 (stable) MongoDB database driver mongodb 1.6.0alpha1 (alpha) 1.5.3 MongoDB driver for PHP |
- Use the following command to see what PHP version is installed on your system:
1 | php --version |
- The system should return something like this:
1 2 3 4 | PHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.15-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies |
Starting the MongoDB Database Daemon
- First, open your terminal by pressing the Ctrl + Alt + T keys.
- Next, start the MongoDB service and check the status using the below command.
- You can use
sudo
command to avoid permission-related issues while starting the MongoDB service.
1 2 | sudo systemctl start mongod sudo systemctl status mongod |
- The system should return something like this:
1 2 3 4 5 6 7 8 9 10 | mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: Active: active (running) since Mon 2019-04-22 19:27:02 PST; 8min ago Docs: https://docs.mongodb.org/manual Main PID: 6598 (mongod) CGroup: /system.slice/mongod.service /usr/bin/mongod --config /etc/mongod.conf Apr 22 19:27:02 teamsolo systemd[1]: Started MongoDB Database Server. lines 1-9/9 (END) |
What is the MongoDB Database Profile?
- The MongoDB profiler gathers information on the performance of the interactions with the database. The monitoring of logs can be adjusted with the profiler, to a certain level as shown below, depending on the needed information.
Setting the MongoDB Profiling Level
Level | Description |
---|---|
Level 0 | At this level the profiler will not log any data |
Level 1 | At this level the profiler will only log slow operations that are above a certain threshold or limit. |
Level 2 | At this level the profiler will log all the operations |
MongoDB Database Profiling Using Mongo Shell
Checking the Profile with Mongo Shell
- Run the following command in the terminal window to check on the current status of the database profile:
1 2 3 4 5 6 7 8 9 | > show dbs abi_db 0.000GB db 0.000GB local 0.000GB > use abi_db switched to db abi_db > db.getProfilingStatus() |
show dbs
— will show all available MongoDB database(s).use abi_db
— will use the specified database.db.getProfilingStatus()
— displays the current database profile.
- The result should resemble something like this:
1 | { "was" : 0, "slowms" : 100, "sampleRate" : 1 } |
This indicates the profiler is “off” and will not log any data.
Use the following command to change the profile level:
1 | >db.setProfilingLevel(2) |
- This will set the profile to Level 2 and enable MongoDB to log all operations.
MongoDB Database Profiling Using PHP
The previous section showed you how to configure profiling with Mongo Shell. This section will show you how to configure profiling using PHP.
The following script will set the profile to “2”.
1 2 3 4 | $con = new MongoDB\Client("mongodb://localhost:27017"); $demoDB = $con->abi_db; $demoDB->command(['profile' => 2]); |
- To verify the results, run the below command in Mongo shell.
1 | db.getProfilingStatus() |
- The result should resemble something like this:
1 | { "was" : 2, "slowms" : 100, "sampleRate" : 1 } |
- or
1 | db.getProfilingLevel |
Conclusion
Mongo Profiler is a very handy application you can use to get an overview of how the database processes queries. In a production environment, you can configure MongoDB database profiling using PHP to impact the database throughput, meaning the number of items passing through a system, provided you have enabled the logging of all operations. Remember these key points:
- Confirm that MongoDB and the MongoDB PHP driver are compatible and properly configured.
- The monitoring of logs can be adjusted with the profiler, depending on the information you want to capture.
- Setting the MongoDB Profiling Level can be done with either Mongo or PHP.
- You must set the profile to Level 2 if you want the profiler to log all operations.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started