SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding MySQL Access Denied Errors in Laravel

The error SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES) is one of the most common stumbling blocks when configuring a new database connection. It often appears deceptively simple, suggesting a typo or a wrong password, but the root cause frequently lies in the mismatch between how your application connects and what your MySQL server permits.

In your specific scenario, you are running Laravel on an Ubuntu VM and attempting to connect to a remote database host at 45.55.88.57. Your initial configuration using root works perfectly. However, switching the username to b while keeping the same password results in an immediate failure. This behavior points directly to MySQL's user-host mapping mechanism rather than a generic password issue.

MySQL does not treat users as global entities; it treats them as specific combinations of a user name and a host. When you define a user like b@localhost, you are explicitly granting permissions for connections originating from the local machine where the database server is running, not from your application server. Conversely, when you use root with 45.55.88.57, you are leveraging a specific entry in the mysql.user table that allows access from that IP address.

Your SELECT User, Host FROM mysql.user output confirms this architecture. You can see distinct entries for root@localhost, root@127.0.0.1, and crucially, b@localhost. There is no entry listed for b@45.55.88.57. When your Laravel application sets DB_HOST=45.55.88.57 in the .env file, it attempts to establish a TCP connection from your Ubuntu VM to the database server. Upon receiving this request, the MySQL server looks for a user record matching b@45.55.88.57. Since that record does not exist, the server rejects the connection with an access denied error, regardless of whether the password you provide is correct for the existing b@localhost entry.

To resolve this, you must create a new user on the database server that explicitly permits connections from your application's IP address or a broad range if security permits. You cannot simply reuse the credentials for b@localhost. Connect to the MySQL server directly (likely via SSH tunneling or if you have direct access) and run the following command:

CREATE USER 'b'@'45.55.88.57' IDENTIFIED BY 'your_secure_password_here';
GRANT ALL PRIVILEGES ON b-prod.* TO 'b'@'45.55.88.57';
FLUSH PRIVILEGES;

Alternatively, if your application server's IP changes frequently or you want to allow any connection from that subnet, you can use a wildcard:

CREATE USER 'b'@'%' IDENTIFIED BY 'your_secure_password_here';
GRANT ALL PRIVILEGES ON b-prod.* TO 'b'@'%';
FLUSH PRIVILEGES;

It is vital to understand the security implications of using %. While it simplifies configuration, it allows any host in the world to connect if they know the password. In a production environment like b-prod, it is generally safer to restrict access to specific IP addresses or subnets rather than opening the port to the entire internet.

Once you have created this new user, update your .env file with the correct password for the b@45.55.88.57 account. Note that while root@localhost worked previously, it is often best practice in Laravel applications to avoid using the root user for application logic. Using a dedicated user like b provides an additional layer of security by limiting what queries the application can execute if credentials are compromised.

If you suspect the password is incorrect, do not guess blindly. You can test the connection directly from your Ubuntu VM using the MySQL client to verify the credentials before updating your Laravel configuration:

mysql -h 45.55.88.57 -u b -p

Enter the password when prompted. If this command connects successfully, you have confirmed that a valid user-host pair exists and the application's .env file simply needs to reflect those exact credentials. If the direct connection fails, you may need to reset the password for the specific b@45.55.88.57 user or create a new one with a known strong password.

Finally, ensure that your Laravel configuration does not rely on the UNIX_SOCKET. You have included UNIX_SOCKET=/var/run/mysqld/mysqld.sock in your .env file. This socket path is only valid if your application is running on the same machine as the MySQL server. Since you are connecting to a remote host (45.55.88.57), this configuration option is irrelevant and can sometimes cause confusion or connection failures depending on how the Laravel driver resolves the host. Remove the UNIX_SOCKET line from your .env file when connecting via TCP/IP over a network.

Our network