PHP artisan returns access denied for database connection in Laravel

Viewed 2502

I transferred a Laravel project based on moving Laravel project between computers
Everything was fine at the first look. I could install the composer without any problems, then I set my environmental variables in my .env such as database name, database user and so on.


When I started using the following command,

php artisan cache: clear

I got these two errors,

In Connection.php line 664:
SQLSTATE[HY000] [1045] Acess denied for user 'root'@'localhost' (using password: Yes) (SQL: select * from information_schema.tables where tabale_schema = *** and table_name = ****)

In Connector.php line 67:
SQLSTATE [HY000] [1045] Access denied for user 'root'@'localhost' (using password: Yes)

Further Information:

  1. It seems .env has not been read by the application because when I browse the homepage of the app, I got Whoops error which shows environmental variable is empty.empty var
  2. I tested my database connection such as its username, password, and other parameters, I know they are working properly.

In the end, I attached a photo in order to elaborate on the issue.issue

  1. Interestingly, in my .env file, DB_DATABASE value is "nlp" and DB_USERNAME equals "Javad" but as you can see in the errors, they are not working, and the Artisan assumes the root as the user!
3 Answers

After you move a Laravel project to another location, The first thing you have to do is regenerate APP_KEY in the .env file by running the command from your application root directory:

php artisan key:generate

After that, you reconfigure your cache:

php artisan config:cache

Additionally, make sure all your database parameters in your .env file and config/database.php are correct and the same as your previous Laravel setup - You can change them manually and then reconfigure your cache as shown above. If not (for instance, the database name is changed), you may have to rerun your database migrations and seeders again:

php artisan migrate

and

php artisan db:seed

I think the configuration files are not writable by the apache user, considering you are using apache. Make sure you have given write permission to bootstrap/cache for the user. This directory contains the cached configuration files.If that is the case the congratulations will be loaded from here and your previously used configuration will be loaded which may be causing the issue.

Also you need to give write permission to the storage directory too. As there is the log file and apache user needs to write on that too.

Open the .env file and edit it. Just set up correct DB credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Your Database Username, If no username has been set then by default there is a root as a username
DB_PASSWORD=          // Your Database Password, If no password is set on the database, clear it  

After completion of .env edit please enter this command in your terminal for clear cache:

php artisan config:cache

If there is still the same error then try another possible solution.

I had also face the same problem, I tried all the possible answers in StackOverflow but nothing could help me, this solution worked on the first try:

If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after changing your .env file values.

I found this solution from here laravel.io (Read the solution of Byjml)

Related