Laravel cannot log in to MySQL: Access denied for user
SQLSTATE[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.
Fix it fast
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 -p2Match what you see, then jump to its fix
- 1
using password: YES, and the mysql client fails with the same messageThe password or user in .env does not match the MySQL accountFix - 2
using password: NOMySQL got no password at allFix - 3
The .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
The 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
- 1
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:showConfirmed by dev.mysql.com
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' .env2Fix
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:showConfirmed by laravel.com
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:cache3Verify
config:show now prints the username and host from .env.
php artisan config:show database.connections.mysqlConfirmed by laravel.com
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
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:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Back end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fixBack end
Laravel: distinct() and count() give the total instead of the unique count
select('city')->distinct()->count() ignores the select
Laravel builds the count from the column you pass to count(), not from select().
2 causes, each with a fixBack end
Laravel Eloquent query for a column that is X or NULL
where('column', null) returns no rows
Wrap the pair in a closure: where(function ($q) { $q->where('col', $x)->orWhereNull('col'); }).
3 causes, each with a fixBack end
Laravel insert fails with Invalid datetime format 1292 Incorrect datetime value
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
MySQL ran in strict mode and refused a date or time value that it cannot store.
3 causes, each with a fixBack 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