Guru Karthi R
Guru Karthi R

Reputation: 320

Spring boot : MySQLNonTransientConnectionException: Could not create connection

I'm trying to connect my spring boot application with MySQL server.

I have followed the documentation properly but still, I'm getting some issue in connection Following exception is throwing every time

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Upvotes: 2

Views: 8761

Answers (2)

dedjer
dedjer

Reputation: 41

In order for me to get around this error I have to log into MySql with the username & password stored in my application.properties file using MySql Workbench. After doing so the error no longer occurs. This only happens when trying to Run my spring boot project after restarting MySql server.

UPDATE: Adding "&allowPublicKeyRetrieval=true" to my spring.datasource.url in the application.properties file solved the problem. I figured this out after I noticed another error in the logs: "MySQL Public Key Retrieval is not allowed"

spring.datasource.url=jdbc:mysql://localhost:3306/db_example?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true

Upvotes: 4

Nuwan Samarasinghe
Nuwan Samarasinghe

Reputation: 153

First of all restart MySQL server and retry again... If it is not...

Did you add application.properties details and JPA, MySQL dependency? Please Could you show pom.xml file and application.properties file.

appication.properties

spring.mvc.view.prefix=/WEB-INF/JSP/ #jsp file path
spring.mvc.view.suffix=.jsp

#Hibernate
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.generate-ddl=true

#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

pom.xml

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Thank you...!

Upvotes: 1

Related Questions