Using mysqli::debug() is a very practical method in debugging MySQLi connections and execution. It can record underlying calls, connection details, execution statements and other information into log files, helping to analyze problems. But sometimes, developers may encounter a confusing phenomenon: after calling mysqli::debug() , they do not see any output in the expected log file, and the log is empty. So, what may be the cause of this?
Before analyzing the problem, we first confirm whether mysqli::debug() is called correctly. The basic usage is as follows:
mysqli::debug("d:t:o,/tmp/client.trace");
$mysqli = new mysqli("localhost", "username", "password", "database");
Explain the debugging options:
d : debug dispatcher (general debug tracing)
t : Record tracking information (tracing)
o,/tmp/client.trace : output the log to the /tmp/client.trace file
??Precautions:
Mysqli::debug() must be called before creating the connection to take effect.
mysqli::debug() is a static method that has nothing to do with the specific connection instance.
Here are some common reasons that may cause the log information you see to be empty:
The mysqli::debug() function relies on the MySQL client library (libmysqlclient or mysqlnd). Mysqli::debug() is only supported when using mysqlnd (MySQL Native Driver). If your PHP is using libmysqlclient , the debugging function may not take effect.
You can confirm by:
echo mysqli_get_client_stats() ? 'mysqlnd' : 'libmysqlclient';
or:
phpinfo(); // Find mysqlnd Whether to enable
If you are using libmysqlclient , it is recommended to switch to mysqlnd .
If the specified log path (such as /tmp/client.trace ) cannot be written to PHP, the log cannot be generated or empty. Ensure that this path has write permissions to users running PHP (such as www-data, apache, nginx).
You can test the permissions:
sudo touch /tmp/client.trace
sudo chown www-data:www-data /tmp/client.trace
sudo chmod 664 /tmp/client.trace
If mysqli::debug() is called after the connection is created, it will not take effect. Be sure to make sure it is called before new mysqli() !
Error demonstration:
$mysqli = new mysqli("localhost", "username", "password", "database");
mysqli::debug("d:t:o,/tmp/client.trace"); // invalid!
Correct writing:
mysqli::debug("d:t:o,/tmp/client.trace");
$mysqli = new mysqli("localhost", "username", "password", "database");
In some cases, if you are using a persistent connection in the form of p:host , debugging may not log information as expected. You can try troubleshooting with non-persistent connections.
If no query is performed after the connection is established, there may be no information in the log. You can try adding a simple query test:
mysqli::debug("d:t:o,/tmp/client.trace");
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->query("SELECT * FROM users");
Use an absolute path to output the log, such as /var/log/mysqli.trace , and set appropriate permissions.
Use independent scripts to test debugging functions whenever possible to avoid being interfered with by complex business logic.
It is recommended not to place the log path in the public network to access the directory to avoid information leakage.
<?php
// Set the debug log path
mysqli::debug("d:t:o,/tmp/mysqli_debug.log");
// Establish a database connection
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
// Execute a query
$result = $mysqli->query("SELECT * FROM users");
// Show results
while ($row = $result->fetch_assoc()) {
echo $row['username'] . "\n";
}
?>
You can view the log output in /tmp/mysqli_debug.log . Remember to set write permissions!
Official document (replaced with m66.net domain name)