无法从Springboot 1.5.9版本连接到mysql RDS

时间:2019-01-07 09:08:45

标签: java mysql spring spring-boot amazon-rds

我正在使用AWS RDS mysql 5.7.23版本。我正在尝试从配置类建立mysql连接 使用下面的代码。

 @Configuration
    public class DbConnection{
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    @Bean
        public Connection getConnection() {
            Connection con = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String jdbc_url = "jdbc:mysql://myhost:3306/myDB?autoReconnect=true&useSSL=false&user=user123&password=user123";
                con = DriverManager.getConnection(jdbc_url);
                return con;
            } catch (Exception e) {
                logger.info("ERROR " + e.getMessage(), e);
            }
            return con;
        }
    }   

我已在apps.properties文件中将日志记录设置为logging.level=ERROR,INFO,DEBUG,WARN。  当我启动springboot应用程序时,它的抛出异常

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: java.sql.SQLException: Access denied for user 'user123'@'myhostIP' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3996)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2287)

我使用的是Dependecy,并且也没有任何版本,因此springboot本身选择了一个版本。

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

我可以使用相同的详细信息通过独立的Java程序连接相同的实例。

对此有任何帮助,为什么我无法连接springboot应用程序。

1 个答案:

答案 0 :(得分:1)

Delete your DbConfiguration class and use Spring Boot's auto configuration https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database by adding the following to your application.properties:

spring.datasource.url=jdbc:mysql://myhost/myDB
spring.datasource.username=user123
spring.datasource.password=user123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver