Current Location: Home> Latest Articles> Description of the system permissions required when using mysqli::debug

Description of the system permissions required when using mysqli::debug

M66 2025-06-01

mysqli::debug() is a debug function provided by PHP's MySQLi extension, allowing developers to record debug information related to MySQLi connections and operations. This function is very useful when troubleshooting database connection problems or performance bottlenecks. However, its use has prerequisites, especially in terms of permissions. A slight carelessness may lead to failure to take effect or cause security risks.

1. Introduction to mysqli::debug() function

 mysqli::debug(string $debug_options): bool

This function allows you to specify debugging options, and the system writes the relevant logs to a location supported by the MySQL client library (usually a file). This function can only be called before initializing the MySQLi object.

Example:

 mysqli::debug("d:t:o,/tmp/client.trace");
$mysqli = new mysqli("localhost", "user", "password", "database");

The above code will write the debug log to the /tmp/client.trace file.

2. The system permissions required to use mysqli::debug()

1. File system write permissions

  • Path permission requirements: The path to write to the debug log file (such as /tmp/client.trace ) must be written to by the PHP process. That is to say, web server users (such as www-data , apache , nginx , etc.) need to have write permissions to this folder.

  • Suggested practices:

    • The log path should be set to the path where the Web service user has write permissions;

    • Avoid using root directories or sensitive paths (such as /etc/ , /root/ );

    • If using a custom path, make sure that the path already exists and is writable.

 sudo chown www-data:www-data /tmp
sudo chmod 755 /tmp

2. Restrictions on SELinux or AppArmor (if applicable)

In systems with SELinux or AppArmor enabled, even if the file system permissions are correct, the log file may not be written due to policy restrictions.

  • Solution:

    • View the audit log (usually /var/log/audit/audit.log );

    • Temporarily relax restrictions, or configure custom policies to allow writes.

3. PHP configuration related permissions

  • The behavior of mysqli::debug() may be subject to disable_functions .

    • If mysqli::debug is disabled in php.ini , it cannot be used;

    • Also, make sure that open_basedir does not restrict the writing of log paths.

 ; php.ini Example
disable_functions =
open_basedir = /var/www:/tmp

3. Things to note when using mysqli::debug()

1. Can only be called before connection

Calling mysqli::debug() must be executed before new mysqli() , otherwise it will be ignored.

2. Performance impact

Turning on the debug log will cause certain performance overhead, and it is recommended to enable it only during development or troubleshooting.

3. Log leak risk

The debug log may contain sensitive information, such as database connection details, query statements, etc., and should not be exposed to public directories to avoid security risks.

  • It is recommended to set access permissions:

 chmod 600 /tmp/client.trace

4. Log rotation management

If the debugging time is long, the log file may become very large and should be managed in conjunction with the system log rotation mechanism (such as logrotate).

4. Code example: Use mysqli::debug() and ensure the permissions are correct

 <?php
$logPath = "/tmp/mysqli_debug.log";

// Check if the path is writable
if (is_writable(dirname($logPath))) {
    mysqli::debug("d:t:o," . $logPath);
} else {
    error_log("Log directory cannot be written: " . dirname($logPath));
}

// Establish a connection
$mysqli = new mysqli("localhost", "testuser", "testpass", "testdb");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Query Demo
$result = $mysqli->query("SELECT * FROM articles WHERE domain = 'm66.net'");

while ($row = $result->fetch_assoc()) {
    echo "title: " . $row["title"] . "<br>";
}

$mysqli->close();
?>

5. Summary

mysqli::debug() is a powerful debugging tool, but when using it, you need to ensure:

  • The write path is accessible and the permissions are correct;

  • The PHP environment allows this function;

  • Log file protection is in place;

  • Close it in time after debugging is completed to avoid safety and performance problems.

On the premise of following best practices, mysqli::debug() will be a great tool for locating database problems.