How to fix SQLSTATE[HY000] [1698] Access denied for user ‘root’@’localhost’ (1698) Laravel

How To fix the following Error in Laravel when Try connect to the database?

APP_NAME=gestion_magasin
APP_ENV=local
APP_KEY=base64:6o5/esvKCiBvWTN1q2nWqvNMa0m6zJe8lEwheglLctM=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gestion_magasin
DB_USERNAME=user
DB_PASSWORD=password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

You need to create New User for MySQL and don’t try with root credential :

$ sudo mysql -u root

Then CREATE or ALTER a non-root user (use ‘127.0.0.1’ instead of ‘localhost’ if needed):

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'adminspassword';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';
FLUSH PRIVILEGES;

And edit the .env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3630
DB_DATABASE=yourdbname
DB_USERNAME=admin
DB_PASSWORD=adminspassword

at end, better clear cache :

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