spelgubbe spelgubbe - 2 months ago 7
MySQL Question

Access denied with correct credentials, what is going on?

As root:

GRANT ALL PRIVILEGES ON * . * To 'JKR'@'%' IDENTIFIED BY "";
FLUSH PRIVILEGES;


In PHP:

$con = new PDO("mysql:host=127.0.0.1;dbname=jkr;charset=utf8", "JKR", "");


Error message:

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'jkr'' in C:\xampp\www\jkr\core\connect.php:6


Only works as root for some reason ...
Also how come I get
Access denied for user ''@'localhost'
?

Answer

Most likely you have an anonymous user ''@'localhost' on your system. An anonymous user is a user where the name is ignored.

If you connect to MySQL with JKR coming from localhost it will match you with the anonymous user.

You can check using SELECT User, Host from mysql.user;

See https://dev.mysql.com/doc/refman/5.7/en/connection-access.html for a detailed discussion about the way MySQL matches users and pemissions.