상황
프로젝트를 작성하고, MySQL과 연동하는 도중 발생한 에러
MySQL과 연동이 되지 않는 경우, 가능성이 있는 문제점들은 아래와 같다.
각 문제점에 대한 해결방법은 그 아래와 같다.
정상적인 연동설정
: 아래 해결방안을 확인하기 전에, 먼저 세팅을 정확히 했는지부터 확인하자
1. build.gradle : sourceCompatibility확인 및 MySQL 디펜던시 확인 필수
plugins {
id 'org.springframework.boot' version '2.7.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id "org.asciidoctor.jvm.convert" version "3.3.2"
id 'java'
}
group = 'com.codestates'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11' // 프로젝트 프로퍼티 및 빌드설정의 JDK 설정 관련
repositories {
mavenCentral()
}
ext {
set('snippetsDir', file("build/generated-snippets"))
}
configurations {
asciidoctorExtensions
}
dependencies {
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.mapstruct:mapstruct:1.5.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.1.Final'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'com.google.code.gson:gson'
implementation 'mysql:mysql-connector-java' // MySQL 디펜던시
}
tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
}
tasks.named('asciidoctor') {
configurations "asciidoctorExtensions"
inputs.dir snippetsDir
dependsOn test
}
task copyDocument(type: Copy) {
dependsOn asciidoctor
println "asciidoctor output: ${asciidoctor.outputDir}"
from file("${asciidoctor.outputDir}")
into file("src/main/resources/static/docs")
}
build {
dependsOn copyDocument
}
bootJar {
dependsOn copyDocument
from ("${asciidoctor.outputDir}") {
into 'static/docs'
}
}
2. application-server.yml : url 경로, password, database-platform 확인하기
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?severTimezone=Asia/Seoul # URL은 jdbc:mysql://localhost:3306/스키마이름 까지만 해줘도 된다.
username: root
password: qqqq #!와 같은 특수기호가 들어간다면, ''로 감싸서 String으로 만들어 줘야한다.
jpa:
database : mysql
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect # MySQL에서 지정해주는 경로를 사용해야한다
open-in-view: false
show-sql: true
hibernate:
format_sql: true
ddl-auto: create
#여기부턴 선택사항
logging:
level:
org:
hibernate:
SQL: DEBUG
type:
descriptor:
sql:
BasicBinder: TRACE
3. edit configuration : 프로파일명을 확인하자 (active=지정할 yml db경로명)
4. MySQL에서 스키마를 만들어놓자 : 필자는 Workbench를 사용한다
application-server.yml의 아래 경로에 넣어줄 이름으로 만들면 된다
url: jdbc:mysql://localhost:3306/스키마_이름?
오류코드
: 다음과 같은 코드만으로는 유추하기 어렵다
... finished with non-zero exit value 1
문제점
1. 코드의 오탈자가 존재
2. application.yml에 기재한 URL 경로 문제
3. MySQL 비밀번호 문제
4. 로컬 포트문제
5. IntelliJ의 JDK 인식문제
6. IntelliJ에 너무 많은 버전의 JDK를 설치했을 경우
해결방법
1. 코드의 오탈자가 존재
: 오탈자를 찾아보는 수밖에...
2. application.yml에 기재한 URL 경로 문제
: 가장 기본적인 경로로 넣어보자
: 혹은 스키마를 안만들었는지 확인해보자
url: jdbc:mysql://localhost:3306/스키마_이름?
3. MySQL 비밀번호 문제
: application-server.yml부분에 들어갈 비밀번호의 특수문자를 조심해야 한다.
: 필자의 경우, !를 맨 앞에 사용하는 바람에 자바어 특성상 부정의 의미로 컴파일되어 password오류가 났다
: 특수문자가 있는 비밀번호의 경우, ''로 감싸서 String형식으로 바꿔주면 정상 동작한다.
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?severTimezone=Asia/Seoul
username: root
password: '!qqqq' #!와 같은 특수기호가 들어간다면, ''로 감싸서 String으로 만들어 줘야한다.
4. 로컬 포트문제
: 본인도 모르게 로컬포트(Local:8080)이 동작하고 있다면, 오류가 난다.
: cmd를 열어 netstat -a -o를 입력 => 8080포트가 사용중인지 확인한다
: 사용중이라면, 해당 포트의 pid 뒤의 번호를 기억하고, cmd를 새로열어 => taskkill /f /pid 해당_pid_번호 입력
: 애플리케이션을 재실행해본다
이미지 설명 링크 : https://radpro.tistory.com/211
5. IntelliJ의 JDK 인식문제
: ctrl + alt + s를 눌러 설정으로 들어간다 => build, Execution, Development => Build tools => Gradle
=> Gradle JVM을 프로젝트의 JDK버젼과 맞춰준다.
: 본인의 JDK 버전확인은 cmd에 java -version 을 입력한다
: 프로젝트 JDK설정은 build.gradle에서 했다
...
sourceCompatibility = '11' // 프로젝트 프로퍼티 및 빌드설정의 JDK 설정 관련. 여기선 11버전이다
...
: 그래도 되지 않는다면, 디폴트 빌드 설정으로 되어있는 Gradle을 IntelliJ로 바꾸고 실행해본다
이미지 설명 링크 : https://radpro.tistory.com/193
6. IntelliJ에 너무 많은 버전의 JDK를 설치했을 경우
: 등록된 JDK들을 다 없애고 IntelliJ를 재시작해서 하나만 설정한다
: 위의 JDK 설정창과 Project structure에서 지우면 된다
'Java & Spring > Error' 카테고리의 다른 글
[SpringSecurity] java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null” 에러 (0) | 2022.09.21 |
---|---|
[Windows/WSL2] '가상 머신 플랫폼 Windows 기능을 사용하도록 설정하고 BIOS에서 가상화를 사용하도록 설정되어 있는지 확인하세요.' 해결방법 (0) | 2022.09.20 |
[Gradle] API 문서 build 에러 (0) | 2022.09.15 |
[우분투] 우분투 설치 오류 해결 (0) | 2022.09.02 |
[Gradle] 잘못된 gradle jvm 구성을 발견했습니다 (0) | 2022.09.02 |