Juhan
Juhan

Reputation: 1291

Unable to connect to mysql RDS from Springboot 1.5.9 version

I'm using AWS RDS mysql 5.7.23 version. I'm trying to establish the mysql connection from a configuration class using the code below.

 @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;
        }
    }   

I have set up the logging as logging.level=ERROR,INFO,DEBUG,WARN in applications.properties file. When I start the springboot application its throwing exception

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)

I'm using the dependecy and also without any version so that springboot itself chose a version.

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

I can Connect the same instance by standalone java program using the same details.

Any help on this why I'm unable to connect with springboot application.

Upvotes: 0

Views: 776

Answers (1)

Heuriskos
Heuriskos

Reputation: 532

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

Upvotes: 1

Related Questions