springboot / jpa无法使用密码“ no”连接用户名“ @localhost”的mysql?

时间:2018-06-30 23:31:12

标签: java spring-mvc jpa jdbc persistence

我正在尝试连接到MySQL数据库。

persistent.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

<persistence-unit name="myApp">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>

  <property name="javax.persistence.jdbc.driver"  value="com.mysql.jdbc.Driver"/>
  <!-- TODO: Change file location to your H2 database ! -->
  <property name="javax.persistence.jdbc.url"     
     value="jdbc:mysql://localhost:3306/myDB"/>

  <property name="hibernate.dialect"value="org.hibernate.dialect.MySQLDialect"/>
  <property name="hibernate.hbm2ddl.auto"   value="update"/>
  <property name="hibernate.show_sql"       value="true"/>

</properties>
</persistence-unit>

</persistence>

application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/myDB?useSSL=false

# Username and password
spring.datasource.username = root
spring.datasource.password = root    

错误

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

2 个答案:

答案 0 :(得分:1)

尝试删除=usernamepassword之间的空间。

spring.datasource.username=root
spring.datasource.password=root

答案 1 :(得分:0)

看起来像问题出在驱动程序版本

现在我使用:

Mysql连接器:5.1.36 Hibernate-Entitymanager 5.2.10 Hibernante核心4.3.10 Application.properties已删除 不再使用任何spring-boot依赖项。

persistence.xml:

..
16->10.12,
17->10.13, 
18->10.14, 
..

现在一切都好。