记录一波,springboot学习中,最新bug搞半小时,新手就是新手~
java.sql.SQLException: Access denied for user 'root'@'192.168.1.1' (using password: NO)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~
ok:这个bug小意思嘛:凭借博主资深硬刚bug经验,不就是下面这样??(using password: NO这个没看到…千万不要学我!)
update user SET Host='%' WHERE User = 'root' and Host = '127.0.0.1';
FLUSH PRIVILEGES ;
一顿猛如虎的操作过后…
事实证明还是too young too navie…
实际上的原因:application.yaml文件
spring:
datasource:
username: root
data-password: root
url: jdbc:mysql://slave2:3306/cjal?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
改成
spring:
datasource:
username: root
password: root #这里...
url: jdbc:mysql://slave2:3306/cjal?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
眼神不好也是醉了,记录一下
2020-03-06 20:09 增加源码解析:springboot在自动装配的时候
//根据配置文件:装配的是DriverDataSource,调用DriverDataSource构造器
@Autowired
DataSource dataSource;
DriverDataSource构造器有一段部分代码如下:
public DriverDataSource(String jdbcUrl, String driverClassName, Properties properties, String username, String password) {
if (username != null) {
this.driverProperties.put("user", this.driverProperties.getProperty("user", username));
}
if (password != null) {
this.driverProperties.put("password", this.driverProperties.getProperty("password", password));
}
}
这里getProperty("password", password));通过配置文件读取password属性
当配置文件属性修改为 data-password 之后,dirverproperties的password为null
于是代码运行流程如下:
DriverDataSource 的 getConnection()方法
方法返回值调用dirver.connect,传参中cloned变量没有password,报以上错
验证:
spring:
datasource:
username: root
password: root1 #故意把数据库密码写错验证
url: jdbc:mysql://slave2:3306/cjal?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
bug成功再现! 问题解决!
大家可以一试