Access denied for user 'homestead'@'localhost' (using password: YES) Access denied for user 'homestead'@'localhost' (using password: YES) php php

Access denied for user 'homestead'@'localhost' (using password: YES)


The reason of Access denied for user ‘homestead’@’localhost’ laravel 5 error is caching-issue of the .env.php file cause Laravel 5 is using environment based configuration in your .env file.

1. Go to your application root directory and open .env file (In ubuntu may be it’s hidden so press ctrl+h to show hidden files & if you are in terminal then type : ls -a to show hidden files) in your editor and change database configuration setting. then save your .env file

DB_HOST=localhostDB_DATABASE=laraveluDB_USERNAME=rootDB_PASSWORD=''

2. then restart your apache server/web server. and refresh your page and you have done

3. If still issue try to run below command to clear the old configuration cache file.

php artisan config:clear

Now you are done with the error


TLDR: You need to stop the server Ctrl + c and start again using php artisan serve


Details:

If you are using Laravel, and have started local dev server already by php artisan serve

And after having above server already running, you change your database server related stuff in .env file. Like moving from MySQL to SQLite or something. You need to make sure that you stop above process i.e. Ctrcl C or anything which stop the process. And then restart Artisan Serve again i.e. y php artisan serve and refresh your browser and your issue related to database will be fixed. This is what worked for me for Laravel 5.3


Two way to solve it

First way (Not recommended)

Open your database config file (laravel_root/config/database.php) & search for the below code block.

        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE', 'blog'),        'username'  => env('DB_USERNAME', 'root'),        'password'  => env('DB_PASSWORD', ''),

Change the code block as below

        'host'      => 'yourHostName',        'database'  => 'YourDatabastName',        'username'  => 'YoutDatabaseUsername',        'password'  => 'YourDatabasePassword',

Second way (Recommended by Laravel)

Check your Laravel root there have a file call .env if not exist, look for .env.example, copy/rename it as .env after that the file looks blow !

APP_ENV=localAPP_DEBUG=trueAPP_KEY=someRandomNumberDB_HOST=localhostDB_DATABASE=homesteadDB_USERNAME=homesteadDB_PASSWORD=secretCACHE_DRIVER=fileSESSION_DRIVER=fileQUEUE_DRIVER=syncMAIL_DRIVER=smtpMAIL_HOST=mailtrap.ioMAIL_PORT=2525MAIL_USERNAME=nullMAIL_PASSWORD=null

Modify the below block as follow

DB_HOST=yourHostNameDB_DATABASE=yourDatabaseNameDB_USERNAME=yourDatabaseUsernameDB_PASSWORD=youPassword

Now it will work fine.