Wednesday, January 07, 2009

MySQL error 1045(28000): Access denied for user ...

Problem
I have a database called hibernate in my MySQL server and I grant privileges to a user using this command
    > grant all on hibernate.* to 'testuser'@'%' identified by 'testuser';
Wildchar % means no matter where the user logs in mysql server from, the access should be allowed for further checking/verification.
When I tried to log in as the newly created user from local host, I got this error:
    ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES)
Also I tried some suggested solutions:
   > FLUSH PRIVILEGES:
   > SET PASSWORD FOR 'testuser'@'%'='testuser';
None of them works.

My solution:
Using following two commands:
   > grant all on hibernate.* to 'testuser'@'%' identified by 'testuser';
   > grant all on hibernate.* to 'testuser'@'localhost' identified by 'testuser';

Theoretically, wildchar % should have included all hosts. But it may not cover localhost. Who knows.

11 comments:

Santiago said...

Thanks a lots!! It was driving me crazy. I did need an additional user and I could not log in.

Unknown said...

Thanks Gerald this was a problem for myself as well.

Unknown said...

That really helped me out. Any idea why it works compared to what I had been using before?

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON name.* TO username@127.0.0.1 IDENTIFIED BY 'password';

Gerald Guo said...

Alex:
127.0.0.1 and localhost are different although 127.0.0.1 refers to local host.
You can log in mysql using command
mysql -h 127.0.0.1 -u user_name -p.
Of course, it must be executed on the same machine where mysql server runs.

Unknown said...

Dude, you are the man! Glad I found this, it worked for me. I scoured the net for a while before I found a solution. Thanks!

RodrigoCarvajal said...

Hi,

You need to create the SAME user at 'localhost' and '%'. It is related to the way MySQL sorts user's table.

Full explanation of why you need permissions at 'localhost' and '%' can be found at:

http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

jbrkeith said...

you are awesome!...been working on this for hours...

Unknown said...

Another reason for the error could be permission issue, click here for full details

siuli said...

Amazing solution.....you are a champ.would like to see more such posts from you :)

kingsoldier said...

my liver dried out....you pour water in it.....thanks man......:D

assasinfanassasin said...

Thanks a lot man like for that error if we press enter key without entering the password it would take us to the mysql terminal but still we would have problems when using java code to connect to databse.
But ur solution was very very good da