SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) . DB_HOST set to localhost SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) . DB_HOST set to localhost php php

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) . DB_HOST set to localhost


To be honest, while working on Laravel 6, I also faced this issue many times and probably was unable to figure out the solution. I tried commands likephp artisan config:cache and php artisan config:clear, but these two commands didn't help.

To come over this issue, you have to execute all the below commands:

php artisan route:cachephp artisan route:clearphp artisan config:cachephp artisan config:clearphp artisan optimize

Note: Make sure in .env you have add db_password and it is not null and also check if your db_password uses any special character. Always enclose them in "".

Example:

DB_PASSWORD="%123456%"

To debug this issue you can also create a test route and then dump the .env variable there to check if they have the correct values or not.

Route::get('/test/env', function () {    dd(env('DB_DATABASE')); // Dump 'db' variable value one by one});

Note: Please make sure to restart your server.


Make sure your database credentials and database host are set correctly:

DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE="your_database_name"DB_USERNAME="put_db_user_name _here"DB_PASSWORD="put_db_password_here_if_have_set"

If you have not set any database password then add:

DB_PASSWORD=""


The problem is that the project made a cache file. 'config.php' must be deleted in order to allow the system to restart and you can do so by locate this file and deleting it:

bootstrap/cache/config.php