Knowledge

SQLSTATE[HY000] [1045] Access denied for user

#Databases

This MySQL error means the username, password or host your application uses to connect is wrong, or the user has no privileges on the database. It is the most common database connection error there is.

Published by Mark van Eijk on June 23, 2026 · 1 minute read

  1. About the error
  2. Why do I see this error
  3. Solution

About the error

In a Laravel application the full message reads something like:

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

using password: YES means a password was sent but rejected. using password: NO means no password was sent at all, often a sign the value is empty in your config.

Why do I see this error

MySQL refused the login. There are only a handful of reasons:

  • The username or password is wrong.
  • The user exists but is not allowed to connect from this host ('user'@'localhost' versus 'user'@'%').
  • The user has no privileges granted on the target database.
  • Your application is reading the wrong credentials, an old .env, a cached config, or the wrong environment.

Solution

First, confirm the credentials in your .env are correct:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_app
DB_USERNAME=forge
DB_PASSWORD=secret

Test those exact values directly against MySQL to rule out the application entirely:

mysql -u forge -p -h 127.0.0.1 my_app

If that fails too, the problem is in MySQL, not Laravel. Create or fix the user and grant it access:

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

Note the host part. A user created as 'forge'@'localhost' cannot connect over TCP to 127.0.0.1, MySQL treats those as different hosts. If your app connects via 127.0.0.1, grant access to 'forge'@'127.0.0.1' (or '%' for any host).

If the direct mysql login works but Laravel still fails, your application is reading stale config. Clear it:

php artisan config:clear

A cached config (php artisan config:cache) freezes whatever values were present at cache time, so re-run it after changing the .env. See environment variables in Laravel for how that loading works.

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

Stream MySQL backup directly to S3 bucket

This MySQL error means the username, password or host your application uses to connect is wrong, or the user has no privileges on the database. It is the most common database connection error there is.

Read more →

Export MySQL database using command line

This MySQL error means the username, password or host your application uses to connect is wrong, or the user has no privileges on the database. It is the most common database connection error there is.

Read more →