PDOException::(“SQLSTATE[HY000] [1045] Access denied for user ‘root’@’localhost’ (using password: YES)”)

Issue Encountered:

I cloned a fresh repository  of Laravel reporisoty on my local machine. Now that I have to run it on my localhost, first thing I need to configure is the database. I did that and triend to run the migration  with php artisan serve  command from my terminal. But this issue occurred frequently:

PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)")

Due to this issue I am now unable to run the project. I can do php artisan serve  and run the project but whenever it comes to fetching data, the database migration is not done. Hence I will be stuck.

Resolutions I Tried

  • I added a new user test  and configured the database, but it didn’t work
  • I uninstalled mysql from my Ubtuntu. Purged the mysql-server mysql-sclient mysql-common and reinstalled mysql. this somehow helped i.e. took me one step closer but again dind’t work

Solution

I find out that my mysql user didn’t have privileges on objects in the database.

Before that I changed my database password just to make the password characted easier to remember using following command:

SET PASSWORD FOR 'test'@'localhost' = PASSWORD('myeasypassword')

After changing the password, I provided privileges on objects in the database by following command:

GRANT SELECT ON jobs.* TO 'test'@'localhost' 

This will do the job.

Now this user test has privilege to the objects in the database. But sometimes you will face this issue repeatedly because of the mysql authentication method. Mysql authentication are done either via mysql_native_password or auth_socket.  If still you are facing the issue then check the link below to change the Mysql authentication method

URL:  How to Change Authentication Method in MySQL?