Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

When you encounter mysqli_connect() throwing an HY000/1045 error, the message is explicit: the MySQL server has rejected the credentials provided. The specific suffix (using password: YES) is often the most confusing part for developers because it implies a password was sent, yet the code snippet might define $DB_PASSWORD as an empty string.

In the scenario described, the configuration defines DB_USER as root and DB_PASSWORD as an empty string. However, the error states that a password was used. This discrepancy usually points to one of two underlying issues: either the application is inadvertently passing a variable containing a value instead of the constant, or the MySQL server has been reconfigured to require authentication for the root user, effectively breaking legacy configurations that relied on no password.

If you previously accessed MySQL without a password and it stopped working after a server restart or an update, the most common culprit is a change in the root user's privileges. Modern MySQL versions (5.7+) often default to requiring a strong password for the root account. If your environment was reset or if you recently switched from XAMPP or WAMP to a different setup where the default root password is now set, your empty string configuration will fail immediately.

To resolve this without guessing passwords, you must inspect the actual state of the mysql.user table within MySQL itself. The error log or the command line can reveal the expected password hash. Run the following command in your terminal to check the current authentication plugin and status:

mysql -u root -e "SELECT user, host, plugin FROM mysql.user WHERE user='root';"

If the plugin column shows mysql_native_password but you are passing an empty string, the server expects a hash that doesn't match an empty string. If the server requires a password but your code expects none, you have two options: set a new password for root or grant access without one (though this is discouraged in production).

If you need to grant access without a password again, connect via the command line with any known credentials and execute:

ALTER USER 'root'@'localhost' IDENTIFIED BY '';
FLUSH PRIVILEGES;

However, be aware that some MySQL configurations map localhost connections to a specific socket file (typically /tmp/mysql.sock) rather than TCP/IP. If your PHP application is configured to connect via TCP (127.0.0.1) but the user was only granted privileges for the Unix socket, you might see access denied errors even with correct credentials. Ensure your DB_HOST matches how the user was created. If your code uses localhost, verify that the user exists for that host:

SELECT user, host FROM mysql.user;

If the user only exists as root@localhost and your application connects via IP address, you may need to create a matching user entry or update the connection string in your config file.

Another subtle issue arises when using XAMPP or similar environments where the default database might be named differently than expected, or where the root password was set during an initial setup wizard. If you recently installed XAMPP and then tried to run an older script, the new installation likely requires a password that wasn't defined in your legacy config.

It is also worth checking if any firewall or antivirus software on Windows is intercepting the localhost connection and modifying the request, though this is less common with TCP/IP connections within the same machine.

Finally, consider using prepared statements for your database interactions once you have resolved the connection issue. While mysqli_connect handles the handshake, managing credentials securely in a .env file rather than hardcoding them into config.php prevents accidental exposure and makes debugging credential mismatches easier. Tools like Laravel's environment management or simple PHP dotenv libraries can help enforce this practice without adding significant overhead to your application logic.

Our network