views:

961

answers:

2

What possible reasons could exist for MySQL giving the error “Access denied for user 'xxx'@'yyy'” when trying to access a database using PHP-mysqli and working fine when using the command-line mysql tool with exactly the same username, password, socket, database and host?
Update:
There were indeed three users in the mysql.user table, each one with a different host (but with the same hashed password), one was set to localhost, one to 127.0.0.1 and one to the machine’s host name. Deleting two of them and changing the host of the third to “%” had only one effect: now the access is denied using the command-line tool also. I did do a

 select user();

before that in the command line and it yielded the same xxx@yyy that were denied in php.

+4  A: 

Sometimes in php/mysql there is a difference between localhost and 127.0.0.1

In mysql you grant access based on the host name, for localusers this would be localhost. I have seen php trying to connect with 'myservername' instead of localhost allthough in the config 'localhost' was defined.

Try to grant access in mysql for 127.0.0.1 and connect in php over 127.0.0.1 port 3306.

HuibertGill
A: 

After I read your update I would suspect an error in/with the password. Are you using "strange" characters in your PW (something likely to cause utf-8/iso encoding problems)?

Using % in the Host field would allow the user to connect from any host. So the only thing that could be wrong would be the password.

Can you create another user. whith the "grant all on all for '...'@'%' identiefied by 'somesimplepw'" syntax, and try to connect with that user? Don't forget to 'flush privelidges'

For info on how to create a new user klick here

HuibertGill