How to Build a Visual MongoDB Slow Query Dashboard with PHP
This guide explains how to set up a PHP‑based web platform that collects MongoDB slow‑query logs via remote profiling, stores them in MySQL, and visualizes the data, including installation of required PHP extensions, database preparation, configuration, cron scheduling, and enabling profiling on MongoDB.
Overview
MongoDB stores profiling data for slow operations in the system.profile collection. This tool is a PHP web application that periodically extracts slow queries from MongoDB, stores them in a MySQL database, and presents them via a browser UI.
Prerequisites
PHP with php-pear, php-devel, php-mysql and the legacy mongo extension (or compatible driver).
MySQL server for result storage.
MongoDB version 3.2 or newer (2.x not tested).
Environment Setup
1. Install PHP‑MySQL driver
yum install -y php-pear php-devel php gcc openssl openssl-devel cyrus-sasl cyrus-sasl-devel httpd mysql php-mysql2. Install PHP‑Mongo driver
pecl install mongoAdd extension=mongo.so to /etc/php.ini and restart Apache:
service httpd restart3. Create a MongoDB monitoring user
The collector must be able to run db.runCommand(). Create a user with dbOwner role on the target database:
use yourdb
db.createUser({
user: "monitor_slowsql",
pwd: "123456",
roles: [{role: "dbOwner", db: "yourdb"}]
})Tool Deployment
Clone the repository and place it under the web server document root:
git clone https://github.com/hcymysql/mongo_slowquery.git /var/www/html/mongo_slowquery1. Import MySQL schema
mysql -uroot -p123456 < mongo_slowsql_schema.sql2. Register monitored MongoDB instance
INSERT INTO mongo_status_info
(ip, tag, user, pwd, port, dbname, threshold_slow_ms)
VALUES
('10.10.159.31','MongoDB Test 1','monitor_slowsql','123456','27017','yourdb',1000);Adjust fields to match your environment (IP, tag, credentials, port, authentication database, and slow‑query threshold in milliseconds).
3. Configure MySQL connection for the web UI
Edit /var/www/html/mongo_slowquery/conn.php:
$con = mysqli_connect("127.0.0.1","admin","123456","mongo_monitor","3306")
or die("Database connection error: " . mysqli_error());4. Schedule periodic collection
Add a cron entry to run the collector every ten minutes:
*/10 * * * * cd /var/www/html/mongo_slowquery && /usr/bin/php check_mongo_slowsql.php > /dev/null 2>&15. Access the UI
Open a browser and navigate to:
http://<em>yourIP</em>/mongo_slowquery/mongo_slowquery.phpThe dashboard shows aggregated slow‑query statistics for the last 31 days, allows filtering by tag, and provides detailed information (user, client IP, collection size, indexes, and explain plan) for each operation.
Enable Slow Query Logging in MongoDB
Set profiling level to capture operations longer than 1 second: db.setProfilingLevel(1, 1000); Verify the setting:
db.getProfilingStatus();Repository
Source code: https://github.com/hcymysql/mongo_slowquery
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
