Spring Boot / MySql :: SQLException: 用户 'user1'@'dhcp-etc' 的访问被拒绝(使用密码:YES)

Spring Boot / MySql :: SQLException: Access denied for user 'user1'@'dhcp-etc' (using password: YES)

看了类似帖子的答案,试了下,很想问这个问题。我的问题与迁移到 spring boot 2.0.0 有关吗?

在本地或 RDS AWS 创建任何 MySql 数据库后,我无法将我的 Spring Boot 2.0.0 应用程序连接到它。我仍然看到异常:... 用户访问被拒绝...(使用密码:是)。

重要:使用以下简单的 Java 程序我可以!!访问 AWS RDS MySql 数据库。我还可以创建表格等。

Class.forName("com.mysql.jdbc.Driver");
// Username and password created via MySQL workbench
String username = "dbuser";
String password = "dbpassword";
String jdbcUrl = "jdbc:mysql://myhostname:3306/mydatabase?user=" + username + "&password=" + password;
Connection con = DriverManager.getConnection(jdbcUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(selectStmt);

使用简单的 Spring Boot 2.0.0 应用程序,我无法连接。首先我得到这个错误:

2018-12-25 10:36:59.670 ERROR 7184 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

java.sql.SQLException: Access denied for user 'dbuser'@'dhcp-etc' (using password: YES)

再往下我得到这个错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory Caused by:

javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory Caused by:

org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution Caused by: java.sql.SQLException: Access denied for user 'dbsetup'@'dhcp-ipaddress-etc-.nl' (using password: YES)

并且:

org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'dbuser'@'dhcp-etc' (using password: YES)

这是我的 application.properties 文件:

spring.datasource.url=jdbc:mysql://myhostname:3306/mydatabase
spring.datasource.username=dbuser
spring.datasource.password=dbpassword
#REMOVED: spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

将此添加到连接字符串没有任何效果:?useSSL=false

我的 pom.xml 包含:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/>
</parent>

// anything related to databases: 

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

// I also tried version 5.1.40
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.11</version> <!-- without this version, same results -->
</dependency>

从我的 PC 正确连接后(使用 Spring 引导)我将继续使用 EC2 实例进行开发。

AWS RDS MySQL 版本:5.6.41。

让父加载其兼容的 mysql 连接器库,因此尝试不使用版本..

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

仔细检查您的 application.properties 文件,您的数据库用户名或密码后不应有任何 space。 或者,你可以这样写:
spring.datasource.url=jdbc:mysql://localhost:3306/dbname?user=dbuser&password=dbpassword