首页 > 解决方案 > springboot/jpa 无法使用用户名“@localhost”使用密码连接到 mysql 否?

问题描述

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

持久化.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>

应用程序属性

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)

标签: javaspring-mvcjpajdbcpersistence

解决方案


尝试删除=andusername和之间的空格password

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

推荐阅读