java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)

今天当我吧项目打包好后将war包放在Linux上运行时出现这样一个bug,Linux下的MySQL用户权限问题:

java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)

百度后说修改权限就可以,SQL语句如下,可在SQL管理工具中执行,也可在SQL控制条执行:

grant all privileges on *.* to [email protected]'%' identified by '******' //***表示数据库连接密码,修改权限

flush privileges;//让权限立即生效

SQL控制台执行方式(盗图):

java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)

java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)

我的执行过程和此图是一样的。

但是我修改权限后依然不行,项目启动后依然报此错误,无法访问数据库:

于是我拿出了第二种方案,将项目中数据库的ip地址更改为了Linux系统的ip地址,保存打包启动服务器,访问成功。

原因:

mysql.user中有多条root用户的权限记录,其中有些绑定了ip或域名(host字段)。

解决方法:

user中有多条root记录时,mysql会优先判断是否使用了绑定的ip,所以将localhost设置成对应的Ip 就可以,也可以把user表中绑定固定Ip的记录删除,只保留host字段为localhost或%的记录。