본문 바로가기

Springboot

[Springboot] mysql 연결 과정과 에러 정리

개발환경

  • Java 8
  • Gradle 4.x
  • Spring Boot 2.1.x

 

application.properties 

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/db_test?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&autoReconnection=true&allowPublicKeyRetrieval=true&useSSL=false
//jdbc:mysql://localhost:포트번호(디폴트 3306)/디비이름?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&autoReconnection=true&allowPublicKeyRetrieval=true&useSSL=false

spring.datasource.username=설정한 유저이름 대부분 root

spring.datasource.password=설정한 비밀번호

spring.jpa.show_sql=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.format_sql=true

spring.session.jdbc.initialize-schema: always

 

build.gradle

아래 코드 추가하기

compile ('mysql:mysql-connector-java')
compile('org.springframework.boot:spring-boot-starter-data-jpa')

 

연결확인

테이블이 잘 생성된 것을 알 수 있다.

 

 

에러메시지

Caused by: java.sql.SQLSyntaxErrorException: Table 'db_test.spring_session' doesn't exist

다음 코드를 application.properties에 추가해주면 해결된다.

spring.session.jdbc.initialize-schema: always

 

그리고 추가적으로 연결이 실패했다는 에러가 계속 떴었는데

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure (이런 에러들..)

컴퓨터에서 mysql을 예전에 지운걸 까먹고 돌려서 그런거였다.. mysql 다시 깔아서 비밀번호 설정하고 db이름 설정한걸로 application.properties 수정하니 그 에러는 해결됨.

 

에러메시지

Execution failed for task ':Application.main()'. > Process 'command 'C:/Users/USER/.jdks/corretto-1.8.0_322/bin/java.exe'' finished with non-zero exit value 1

이 에러는 프로젝트 버전이 안맞아서 발생했던 에러였는데 jdk 버전을 15에서 8로 내리고 아래 링크에서 알려준 방법대로 설정했더니 해결되었다. 

https://milenote.tistory.com/63

 

[Intellij / 인텔리제이] 프로젝트 버전 안맞는 현상 ( Execution failed for task ':compileJava'. )

원인 - intellij를 설정할 때 기본 jdk 버전에 차이 때문에 빌드 에러가 발생했다. 집 - jdk 11 회사 - jdk 1.8 해결 1. File > Project Settings > Project > Project SDK SDK 버전을 새로 다운로드 받아서 버..

milenote.tistory.com

 

에러메시지

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

spring.datasource.url 뒤에 다음 옵션을 추가해주면 된다.

allowPublicKeyRetrieval=true&useSSL=false