Access denied for user 'root'@'localhost'

耗尽温柔 提交于 2019-11-27 05:32:55

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!