Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel cannot log in to MySQL: Access denied for user

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

Short answer

Error 1045 means MySQL received the login and rejected it. Read the message: the user name, the host and YES or NO after 'using password' tell you which setting is wrong. Test the same credentials in the mysql client first. Then check the cached config, a stale system variable, and the user's host.

  • Applies to Laravel 12, PHP 8.3, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log in with the exact user, host and password from your .env file, outside Laravel. If this fails too, the problem is in MySQL, not in your app.

    mysql -h 127.0.0.1 -u YOUR_DB_USERNAME -p
  2. 2Match what you see, then jump to its fix

    1. 1using password: YES, and the mysql client fails with the same messageThe password or user in .env does not match the MySQL accountFix
    2. 2using password: NOMySQL got no password at allFix
    3. 3The .env file is correct and the login works in the mysql client, but Laravel still failsA cached config or a server-level variable is overriding .envFix
    4. 4The host in the error (for example 'app'@'172.18.0.3') is not a host you created the user forThe account exists for a different host than the one you connect fromFix

What does the error look like?

  • SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: YES)
  • SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)
  • mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

Cause 1 of 4

The password or user in .env does not match the MySQL account

1Check

Compare DB_USERNAME and DB_PASSWORD with what the server knows. The error text shows the account MySQL tried to match, as user and client host.

grep -E '^DB_(HOST|PORT|USERNAME|DATABASE)=' .env
mysql -u root -p -e "SELECT User, Host FROM mysql.user;"

2Fix

Set the password again on the account that matches the error message, then put the same value in .env.

-- inside the mysql client, as an admin user
ALTER USER 'app'@'localhost' IDENTIFIED BY 'new-strong-password';

3Verify

The mysql client logs in, then Laravel reaches the database.

mysql -u app -p -e "SELECT 1;"
php artisan db:show

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

MySQL got no password at all

1Check

Laravel sent an empty password. Either DB_PASSWORD is empty or missing in .env, or the value is not being read.

grep -n '^DB_PASSWORD' .env

2Fix

Set DB_PASSWORD. If the value contains spaces, wrap it in double quotes. A fresh MySQL install has a root password assigned already, so an empty one will not work.

DB_PASSWORD="my secret pass"

3Verify

Clear cached config and run a query.

php artisan config:clear
php artisan db:show

Confirmed by laravel.com

Did this fix it?

Cause 3 of 4

A cached config or a server-level variable is overriding .env

1Check

After php artisan config:cache, Laravel stops loading .env, so old values stay in the cache. A real environment variable on the server also wins over .env. php artisan config:show prints what Laravel actually uses.

php artisan config:show database.connections.mysql
env | grep '^DB_'

2Fix

Clear the cache, remove or correct the stray system variable, and cache again only after the values are right. Call env() only inside config files.

php artisan config:clear
# after fixing .env, on production only:
php artisan config:cache

3Verify

config:show now prints the username and host from .env.

php artisan config:show database.connections.mysql

Confirmed by laravel.com

Did this fix it?

Cause 4 of 4

The account exists for a different host than the one you connect from

1Check

MySQL accounts are a pair of user and host. 'app'@'localhost' and 'app'@'%' are separate rows. List the rows for your user.

mysql -u root -p -e "SELECT User, Host FROM mysql.user WHERE User='app';"

2Fix

Create the account for the host shown in the error message, or point DB_HOST at the host the account allows.

CREATE USER 'app'@'172.18.0.3' IDENTIFIED BY 'new-strong-password';
GRANT ALL ON mydb.* TO 'app'@'172.18.0.3';

3Verify

Log in from the application host with the new account.

mysql -h DB_HOST_VALUE -u app -p -e "SELECT 1;"

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Give each app its own MySQL user with rights on one database only, never root.
  • Run php artisan config:clear as the first step of any database debugging, and config:cache as the last step of a deploy.
  • Keep a .env.example with every DB_ key, so a new server cannot miss one.
  • Quote any .env value that has spaces.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

php artisan migrate stops with could not find driver

could not find driver (Connection: mysql, SQL: select * from information_schema.tables ...)

Laravel talks to the database through a PDO driver, and this error means the PHP that runs artisan has none for your DB_CONNECTION.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.