[问题] java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)

当在springboot中使用mybatis-plus 和 Druid数据源时,出现如下错误:

2022-11-29 09:06:02.098  INFO 3920 --- [           main] com.heima.TestMybatisplusApplicationTests  : Started TestMybatisplusApplicationTests in 2.214 seconds (JVM running for 2.982)
2022-11-29 09:06:02.514 ERROR 3920 --- [eate-1738561265] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://localhost:3306/数据库名字?useSSL=false&serverTimezone=UTC, errorCode 1045, state 28000

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1657) ~[druid-1.2.6.jar:1.2.6]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1723) ~[druid-1.2.6.jar:1.2.6]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2801) ~[druid-1.2.6.jar:1.2.6]

发现,问题出在:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)。那就去看配置文件 application.yml 中的内容:

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/数据库名字?useSSL=false&serverTimezone=UTC
      username: root
      password: 0127

此时,你可以试试使用上面的用户名、密码: username: rootpassword: 1234 可不可以在cmd中连接mysql成功。

  • 如果可以,那就很有可能是 springboot 把你上面配置文件的 密码当做八进制的数处理了,结果可想而知,肯定出错了。你应该如何解决呢?
    答:可以手动显式的告诉springboot我的密码就是我输入的这个字符串,不要给我转换成其他东西了。即手动显式的将密码用"" 包含起来:"0127" 。这样就可以了

  • 如果连接不成功,那就说明你的 用户名、密码 可能是错的, 那就改成正确的输入就行了。

你可能感兴趣的:(Java,#,问题,java,sql,mybatis)