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

时间:2022-12-28 19:16:38

在java项目中使用连接串设为 localhost 时报以下错误,但在服务器中使用mysql -u root却可以登录

2014-08-29 09:05:29,181 WARN  com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1841) - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@67618412 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (10). Last acquisition attempt exception: 

java.sql.SQLException: Access denied for user 'root'@'127.0.0.1' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)

原因:

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

解决方法:

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