mysql Access denied for user 'root'@'localhost' (using password: YES)

时间:2021-10-11 02:51:15

【现象说明】

C/S程序远程访问正常,本地访问报以下异常

MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'@'localhost' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'root'@'localhost' (using password: YES)

配置文件如下:

<add name="RemoteConnString" connectionString="Database='mealbooker';Data Source='localhost';User Id='root';Password='123456';charset='gb2312';pooling=true" providerName="MySql.Data.MySqlClient"/>


【分析】

权限问题吧,查看下mysql数据库中的user表

mysql Access denied for user 'root'@'localhost' (using password: YES)

发现localhost下面值为空,于是connection string中的localhost 改为 127.0.0.1,测试通过~~~

【解决】

mysql Access denied for user 'root'@'localhost' (using password: YES)

配置文件中改为localhost,访问数据库正常;同时mysql command line工具输入密码后一闪而过问题同时解决。

重启之后又访问不了,继续上面的思路修改:

mysql Access denied for user 'root'@'localhost' (using password: YES)


另,mysql command line工具一闪而过,方法参见这里


另一个权限相关问题:

Error Code: 1370 execute command denied to user 'root'@'localhost' for routine

使用grant execute on procedure/function dbname.fname TO 'root'@'localhost';

mysql Access denied for user 'root'@'localhost' (using password: YES)


另一密码相关问题(调试机放几天后就没法远程访问了,root密码被改动,不明原因):

 Authentication to host '10.10.123.6' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'@'10.10.123.5' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'root'@'10.10.123.5' (using password: YES)

修改password语句如下:

mysql Access denied for user 'root'@'localhost' (using password: YES)

然后还要给远程用户非配权限

grant all privileges on *.* to 'root'@'%' with grant option;