SQLSTATE[HY000] [1045] Access denied for user ‘username’@’localhost’ (using password: YES)
I am just running localhost on my own computer. Have not run into this error before. I am able to log in directly to mysql with both root and the user in question. Via php connection I can log in to the web page successfully if I use the root user credentials but if I switch them to the regular user I get that error message.
I have reviewed similar threads with this error but not found specific answer that will work for me.
Advertisement
Answer
First check if both users are present in mysql.user
and have localhost
assigned as allowed host
:
select user, host from mysql.user where user in ('root', 'username'); +----------+-----------+ | user | host | +----------+-----------+ | root | localhost | | username | localhost | +----------+-----------+ 2 rows in set (0.00 sec)
If that is the case check the privileges for username@localhost
:
show grants for username@localhost; +------------------------------------------------------------+ | Grants for username@localhost | +------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'username'@'localhost' | | GRANT ALL PRIVILEGES ON `mydb`.* TO 'username'@'localhost' | +------------------------------------------------------------+ 2 rows in set (0.03 sec)
Make sure that your database (in this example mydb
) is in the list.
Next you could check if the passwords that you try to log in with from your app and on the commandline are the same.
mysql -u username -h localhost -p <password-copied-from-php-code>