PHP cant connect locally, using XAMPP - "Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO)"

PHP cant connect locally, using XAMPP - "Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO)"

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding "Access Denied for user 'root'@'localhost' (using password: NO)" in XAMPP

When a fresh reinstall of XAMPP fails to connect to MySQL, the error Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO) is often misleading. The phrase "using password: NO" indicates that PHP successfully passed an empty string as the password argument to the mysqli constructor, but the server rejected the connection. This specific rejection usually stems from a configuration mismatch rather than a missing database or a forgotten root password.

The most common trigger for this behavior after a system reformat is the interaction between the MySQL root user privileges and the PHP environment's security settings. In many modern XAMPP installations, specifically those running on Windows 10/11 with the latest PHP versions (7.x and 8.x), the default my.ini file includes the directive:

[mysqld]
skip-grant-tables=OFF

When skip-grant-tables is enabled (or omitted, defaulting to OFF depending on the version), MySQL enforces password checks strictly. Even if you can log in via the command line (mysql -u root -p) by entering nothing, or via phpMyAdmin after changing the config file, the PHP application might fail. This discrepancy often arises because the user account exists but lacks the GRANT USAGE permissions on a specific host context, or the root user is restricted to local connections only (localhost) while the PHP code attempts to connect via a socket that isn't being recognized correctly in the new environment.

A critical detail emerges from the stack trace provided: the connection string uses 'localhost'. In XAMPP, MySQL runs as a service listening on a specific port (default 3306) and binds to 127.0.0.1 or ::1. When PHP connects to localhost, it attempts to use the TCP/IP socket if the Unix socket directory is not configured or if the permissions are wrong. However, the "using password: NO" error specifically points to a permission denial where the user exists but has no access rights for that specific host definition.

Consider the scenario where you create a new user, such as restapi, with a password and can log in via the command line:

mysql -u restapi -p -h localhost

If this succeeds but PHP throws an access denied error immediately upon connection, the issue is likely one of two things:

  1. Privilege Mismatch: The user restapi was created with privileges for localhost, but the application might be trying to connect via a different host identity (e.g., % or the machine name) due to how the ODBC driver or MySQL socket is resolved in PHP.
  2. SQL Mode Restrictions: If the server has strict SQL modes enabled, it might reject connections from users that don't match the exact host definition perfectly.

To resolve this without relying on skip-grant-tables (which disables security), you should verify the user's privileges explicitly within a working session:

-- Run inside MySQL command line or phpMyAdmin
SHOW GRANTS FOR 'root'@'localhost';
SHOW GRANTS FOR 'restapi'@'localhost';

If restapi shows no grants for pcss, you must execute the following in a privileged session (using root or an admin user):

GRANT ALL PRIVILEGES ON pcss.* TO 'restapi'@'localhost';
FLUSH PRIVILEGES;

Furthermore, ensure that the root user has the necessary permissions for the specific host context. Sometimes, a simple ALTER USER 'root'@'localhost' IDENTIFIED BY '' is insufficient if the host entry itself is missing or restricted. You may need to explicitly create the root account with local access:

CREATE USER 'root'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
FLUSH PRIVILEGES;

Finally, check your PHP configuration in php.ini. Ensure that mysqli.default_host and mysqli.default_port are not overriding your explicit connection parameters. If you are deploying to production later, remember that localhost implies a local socket or loopback IP, whereas % allows remote connections. For local development on XAMPP, sticking to the default credentials (root, empty password) often requires ensuring that the MySQL service is actually running and listening on port 3306, not port 80. Conflicting services, such as Apache listening on port 80 while trying to redirect traffic or bind incorrectly, can also disrupt the expected network stack for database connections.

For developers looking for a more robust setup that handles environment-specific configurations cleanly, Laravel provides excellent insights into managing database connections across different environments (local vs. production) using .env files and the config/database.php structure. Their approach to separating credentials from code is a best practice worth adopting even outside the Laravel framework.

Our network