Skip to content

Fix this error

SQLSTATE[HY000] [1045] Access denied for user

Dasun Kalupahana 323 views

The error

Error

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

Why it happens

The database server is running, but it rejected the username or password, or that user is not allowed to connect from this host.

  • DB_USERNAME / DB_PASSWORD in .env are wrong.
  • The password contains # or spaces and is not quoted, so .env cuts it off.
  • On shared hosting (cPanel) the user name has a prefix such as account_user, or the user was never added to the database.
  • The config is cached with old values.

How to fix it

  1. Check the credentials in .env. Quote passwords that contain special characters: DB_PASSWORD="p#ss word".
  2. On cPanel, use the full prefixed names and add the user to the database (MySQL Databases → Add User To Database, all privileges).
  3. Run php artisan config:clear.
  4. If you manage MySQL yourself, create the user and grant access (below).
CREATE USER 'app'@'localhost' IDENTIFIED BY 'strong-password';
GRANT ALL PRIVILEGES ON laravel.* TO 'app'@'localhost';
FLUSH PRIVILEGES;
Still stuck? Send us the full error and what you tried. We add the answers here.

Learn more on the blog

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.