I am trying to fetch records from database. but I am facing this access denied issue. I tried the other solutions mentioned on Stack Overflow like granting privilege to the
Try it:
mysql --no-defaults --force --user=root --host=localhost --database=mysql
Change a new password:
UPDATE user SET Password=PASSWORD('NEWPASSWORD') where USER='root';
FLUSH PRIVILEGES;
Start mysql client in the console and execute this query: select Host, User from mysql.user;
. You MUST have a row like this:
+----------------+------------------+ | Host | User | +----------------+------------------+ | localhost | root | +----------------+------------------+
a row with "localhost" in Host and "root" in User. If you don't have it that's the cause of your problem (it doesn't matter if you have other rows with "root" in User)
If you don't have such row, add a new user with this:
CREATE USER 'appUser'@'localhost' IDENTIFIED BY 'appPassword';
Change 'appUser' by 'root' if you want, but I strongly suggest to use another user. Then add permissions to your new user by executing this in the mysql client:
GRANT ALL PRIVILEGES ON employees.* TO 'appUser'@'localhost';
(again, change 'appUser' by 'root' if you want)
The problem was the permissions granted to root in the information.schema
table. 'root'@'%' didnt have any permissions.. And as I was using 127.0.0.1
as my connection address, so it was giving the access denied error.. %
is the wildcard for an ip address. so mysql considers root@127.0.0.1
as any other ip address but not localhost
. so just granting it permission solved my problem.. Try using some Mysql client like SQLYog
etc.. it is easy to grant the privileges and also to view the privileges with the user.