laravelconfig

Laravel uses wrong MySQL user despite .env configuration (SQLSTATE[HY000] [1698])


I am working on a Laravel project and trying to configure my database connection. I have set the correct MySQL credentials in my .env file, but Laravel is still trying to connect using the root user instead of the one I specified.

My Setup

My .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=react_breeze
DB_USERNAME=other_username_than_root
DB_PASSWORD=password

Issue:

When I run php artisan migrate or php artisan cache:clear, I get the following error:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (Connection: mysql, SQL: delete from cache)

When I try I get same problem

rm -rf bootstrap/cache/config.php

php artisan config:clear
php artisan cache:clear
php artisan config:cache

Solution

    1. Laravel is still using the old cached configuration If you recently updated your .env file, Laravel may still be using cached settings.

    Solution: Run the following commands to clear and refresh the configuration:

    php artisan config:clear
    php artisan cache:clear
    php artisan config:cache
    
    1. MySQL User Permission Issue Your MySQL user (other_username_than_root) might not have sufficient privileges on the react_breeze database.

    Solution: Log in to MySQL using root (if you have access) and grant the necessary privileges:

    sudo mysql -u root -p
    

    Then, execute:

      GRANT ALL PRIVILEGES ON react_breeze.* TO 
     'other_username_than_root'@'localhost' IDENTIFIED BY 'password';
      FLUSH PRIVILEGES;
      EXIT;
    

    Try restarting MySQL:

     sudo systemctl restart mysql