Permission error when trying to access database

Asked

Viewed 30,736 times

6

I took a little project already ready to study, only that is giving this error, recently migrated to the Ubuntu and I do not know what to do.

Error: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost (using password: YES)

  • 2

    This error can have so many reasons... start by checking if you are using the correct password... where this little project stores the connection configuration to the database?

4 answers

5

This error occurs due to host. It may be the host that it listens to or permission to.

Try to change localhost for 127.0.0.1

If not resolve try to give root user permission to localhost

Sign in to mysql console by opening a terminal and run

mysql -h localhost -u root

If you have a password run like this

mysql -h localhost -u root -p

When enter execute the following command

GRANT ALL PRIVILEGES ON *.* TO root@'localhost'

The part after @ is the host that makes the connection, so you must give permission to all hosts that must make the connection. on the host you can specify a domain or even use the wildcard '%' to allow access to any host. Caution not to leave access to any host on production servers as it is a very big security failure.

If not, check if the password is correct.

4


I already faced this error. Is root configured to use a password even? Can you test this hypothesis by typing this:

mysql -h localhost -u root

Obs.: you don’t have the option -p to give the password

  • Mysql 5.7 if I am not mistaken, no longer accepts direct root access other than a terminal application with access to "sudo", a new user must be created. See this response on the Ubuntu website: https://askubuntu.com/a/763359/689807 . That’s just an addition to who’s lost and can’t find the answer.

3

This problem suggests that you are probably accessing Mysql without specifying the user and are giving a password to a user who is not root. When switching server, for some reason the default user became root, so the password must be wrong.

If that is the case, I recommend that you always explicitly connect to the database using a user and password instead of assuming the default connection.

3

This error happened because the password that was specified in the script, where the connection to the database is made, was different from the root password of my machine. Silly mistake for lack of attention but who gets of experience to help the brothers.

  • You can accept his answer then, if you think this is really the solution. If you’re just commenting, you’d better delete the answer and leave it as a comment. Answers should answer the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.