Access denied for user ''@'localhost' (using password: NO)

来源:4-5 商品服务编码(上)

白平衡

2018-10-26

真是奇怪,在我创建测试类之前还可以连接数据库不报错,创建测试类后就报错了

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81) ~[spring-jdbc-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319) ~[spring-jdbc-5.0.0.RC3.jar:5.0.0.RC3]
	... 61 common frames omitted
Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2198) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2229) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_121]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_121]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.43.jar:5.1.43]
	at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:112) ~[HikariCP-2.6.3.jar:na]
	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-2.6.3.jar:na]
	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-2.6.3.jar:na]

在我最早看师兄springboot项目时也报同样错误,后来网上说是我的密码是数字给我解析成数字什么的我加上双引号之后果断可以了,但是现在无论加不加都报上图错误,application.yml如下:

spring:
  application:
    name: product
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: "0821"
    url: jdbc:mysql://127.0.0.1/SpringCloud_Sell?CharacterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

不知老师遇到过没还请老师指点

写回答

2回答

白平衡

提问者

2018-10-30

应该是配置的问题,我之前的yml配置是这样的

 datasource:
   driver-class-name: com.mysql.jdbc.Driver
   data-username: root
   data-password: "0821"
后来将username,password 前面的data-去掉就好了

0
1
廖师兄
要仔细哦,确实是不要加data的,可以对比我的源码
2018-11-01
共1条回复

廖师兄

2018-10-26

  1. 你的数据密码是【"0821"】还是【0821】啊

  2. 用mysql客户端去连一下,看是否可以连上

0
1
白平衡
是0821,不过写了0821会报同样的错,后来配置时加上双引号就可以了,不过今天晚上开始还能连接数据库启动正常,建了测试类后再启动就报这个错了,把测试类删了还不行,尝试把配置换成我的密码0821不加双引号也不行?
2018-10-26
共1条回复

SpringCloud Finchley(M2+RELEASE+SR2)微服务实战

SpringCloud组件实现微服务,【已升级Finchley.Release】

5668 学习 · 2489 问题

查看课程