Real-Time Monitoring of MySQL Queries
Monitoring live MySQL queries is crucial for performance optimization and troubleshooting. This article provides a simple method to trace queries on a Linux server in real time.
(Problem)
How can you view MySQL queries as they're executed on a Linux server? Whether you're interested in monitoring a single webpage request or all queries running on a production server, you can establish a listener to capture and inspect these queries.
(Solution)
MySQL offers a convenient way to log every query to a log file:
mysql> SHOW VARIABLES LIKE "general_log%"; +------------------+----------------------------+ | Variable_name | Value | +------------------+----------------------------+ | general_log | OFF | | general_log_file | /var/run/mysqld/mysqld.log | +------------------+----------------------------+ mysql> SET GLOBAL general_log = 'ON';
Once you've executed these commands, all queries will be logged to /var/run/mysqld/mysqld.log. You can use grep or another tool to examine the log file:
grep 'your_query_string' /var/run/mysqld/mysqld.log
Remember to disable logging once you're done to prevent performance degradation and disk space issues:
mysql> SET GLOBAL general_log = 'OFF';
The above is the detailed content of How Can I Monitor MySQL Queries in Real-Time on a Linux Server?. For more information, please follow other related articles on the PHP Chinese website!