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

 

How to Exclude Files and Folder when Make a tar archive

If you want to make an archive of your files and folders, but you want to exclude some files or folders, this tutorial explains how to do this.

In this tutorial, using the tar command, we want to archive all the files and folders in the directory where we are. But “wp” folders and files with the “tar.gz” extension are excluded.

The procedure is very simple, in general, if you want to archive all the contents of your folder, we run the following command:

$ tar -zcvf /backup/filename.tgz .

In the example above, -zcvf is to apply the command settings, “/backup/filename.tgz” is to specify the name and path of the archive storage. And “.” To specify the folder we want to archive.

But since we want to exclude the “wp” folder, adding exclude is done as follows:

$ tar --exclude='./wp' -zcvf /backup/filename.tgz .

If we want to add more than one template to the set of exceptions, we do the following:

$ tar --exclude='./wp' --exclude='./upload/folder' -zcvf /backup/filename.tgz .

 

If we want to add more than one template to the set of exceptions, we do the following:

$ tar --exclude='./wp' --exclude='*.tar.gz' -zcvf /backup/filename.tgz .

 

Attention :

Make sure to put --exclude before the source and destination items.