Resolving mysqli_real_connect() Permission Denied in PHPMyAdmin on WSL
Encountering the error mysqli_real_connect(): (HY000/2002): Permission denied when launching PHPMyAdmin is a classic misconfiguration issue rather than a fundamental bug. Based on the symptoms describedâwhere you can access MySQL via sudo -u root -p in the command line but hit a wall in the web interfaceâthe root cause lies in how Apache attempts to communicate with the database server versus how the user accounts are defined.
In a standard WSL setup running Apache and MySQL, there is often a disconnect between the root@localhost account you use from the terminal and the controluser account expected by PHPMyAdmin. The error message specifically indicates that the connection for the controluser as defined in your configuration failed. This usually points to a mismatch in privileges or an incorrect authentication mechanism within the MySQL instance configured for web access.
When you run sudo -u root -p, you are bypassing the specific security layers that PHPMyAdmin relies on. The web server process (typically running as www-data) attempts to connect to MySQL using credentials specified in /etc/mysql/mariadb.conf.d/50-mysql.cnf or similar configuration files. If this connection fails, PHPMyAdmin cannot initialize, resulting in the mysqli_real_connect() error.
To resolve this, you must ensure that the user account used for the web server connection has the correct host privileges and password. The most robust approach is to define a dedicated control user or ensure the root account accepts connections from localhost with the specific authentication plugin required by your MySQL version.
First, verify the current user configuration in your MySQL configuration file. Look for a section defining controluser. If this directive exists but points to a non-existent user, you need to create it:
sudo mysql -u root -p
Inside the MySQL prompt, ensure that the root user can connect from localhost and that the necessary plugins are enabled. In older versions of MySQL (prior to 8.0), the mysql_native_password plugin was standard. However, newer installations default to caching_sha2_password, which may not be recognized by legacy PHPMyAdmin configurations or Apache setups if the client library is outdated.
You should explicitly set a password for the root user that matches what you intend for the web connection:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_secure_password';
FLUSH PRIVILEGES;
EXIT;
If your configuration file specifies a controluser that is not root, you must create that specific user. For example, if your config requires phpmyadmin as the control user:
CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL ON *.* TO 'phpmyadmin'@'localhost';
FLUSH PRIVILEGES;
EXIT;
After updating the credentials, you must restart both Apache and MySQL to apply the changes:
sudo systemctl restart apache2
sudo systemctl restart mysql
A critical pitfall in WSL environments is file permissions. Even if the database accepts the login, Apache might be unable to read the configuration files due to restricted ownership. Ensure that the MySQL data directory and configuration files are accessible by the www-data user. If your configuration relies on a specific socket file (like /var/run/mysqld/mysqld.sock), ensure the permissions allow the web server process to access it:
sudo chown -R www-data:mysql /var/run/mysqld
Finally, verify that PHPMyAdmin itself is configured to use these credentials. If you are using the default installation, check the config.inc.php file for the $cfg['Servers'][$i]['controluser'] and $cfg['Servers'][$i]['controlpass'] settings. These must match the user and password created in the MySQL instance.
By aligning the database authentication method with the web server's expectations and ensuring proper file permissions, you eliminate the Permission denied error without needing to grant root-level access to untrusted web processes.