인프런 커뮤니티 질문&답변

송우석님의 프로필 이미지

작성한 질문수

자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]

13강. Spring에서 Database 사용하기

Spring과 MySQL연동 실패 ..

24.03.26 14:00 작성

·

1.1K

0

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java:8.3.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

위와 같이 dependencies를 설정했고

밑은 application.yml입니다.

spring:
datasource:
url: "jdbc:mysql://loca lhost/library"
username: "root"
password: "비밀번호"
driver-class-name: com.mysql.cj.jdbc.Driver

비밀번호는 알아서 설정했습니다.
오류가 생겨서 dependencies에서 runtimeOnly 'mysql:mysql-connector-java:8.3.0'로 설정했습니다.

밑에는 에러 표시입니다.

Execution failed for task ':LibraryAppApplication.main()'.

> Could not resolve all files for configuration ':runtimeClasspath'.

> Could not find mysql:mysql-connector-java:8.3.0.

Required by:

project :

Possible solution:

- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

 

> Task :LibraryAppApplication.main() FAILED

Execution optimizations have been disabled for task ':LibraryAppApplication.main()' to ensure correctness due to the following reasons:

- Type 'org.gradle.api.tasks.JavaExec' property 'classpath' cannot be resolved: Could not resolve all files for configuration ':runtimeClasspath'. Reason: An input file collection couldn't be resolved, making it impossible to determine task inputs. Please refer to https://docs.gradle.org/7.5/userguide/validation_problems.html#unresolvable_input for more details about this problem.

Execution failed for task ':LibraryAppApplication.main()'.

> Could not resolve all files for configuration ':runtimeClasspath'.

> Could not find mysql:mysql-connector-java:8.3.0.

Required by:

project :

Possible solution:

- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

 

Could not find mysql:mysql-connector-java:8.3.0.

Required by:

project :

Search in build.gradle files

뭐가 에러인가요..?

만약 Runtimeonly뒤에 version을 따로 기입하지 않는다면,
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

라는 에러가 뜹니다.

답변 3

0

송우석님의 프로필 이미지
송우석
질문자

2024. 03. 26. 15:13

아.. 이고.. 이게 뭐라고 하루동안 검색을 하고 씨름을 했네요. 그냥 에러문구를 복사해서 구글에서 바로 찾는 것보다 에러를 해석해 가면서 찾는게 제일 중요하네요 진짜 감사합니다 선생님 강의 잘 따라가보겠습니다!

최태현님의 프로필 이미지
최태현
지식공유자

2024. 03. 26. 15:40

아이고 고생 많으셨습니다 🥺 이렇게 고생하신 것들이 모두 모여 우석님 실력이 될거에요! 👍

강의 들으시다가 막히는 부분 있으시면, 편하게 질문 남겨주세요! 감사합니다. 🙏

0

송우석님의 프로필 이미지
송우석
질문자

2024. 03. 26. 14:38

스크린샷 2024-03-26 오후 2.35.29.pngMySQL은 잘 켜지는 것 같습니다. stack trace라는것을 잘 이해못해서 일단 전체 에러를 올려보았습니다.

최태현님의 프로필 이미지
최태현
지식공유자

2024. 03. 26. 14:43

공유해 주셔서 감사합니다! 🙏 저기 빨간색 경고표시(?)가 여러개 이쓴 것 중 제일 위에 있는 것을 눌러보시면, 빨간색 글씨들이 나오는 오른쪽 창에 이제 에러기록들이 쭉 나올거에요!

 

다행히 보내주신 화면에서도 에러기록들이 일부 나오는데요! 예를 들면,

image위의 목록을 보고 다음과 같은 내용을 유추해볼 수 있어요!

  • 1차 에러 : Communication link failure

  • 2차 에러 (1차 에러가 발생한 이유) : loca lhost: nodename nor servername provided, or not known

이러한 에러 목록을 stack trace라고도 부릅니다! 🙂

 

제가 이걸 보고 위에 올려주신 설정을 함께 보니 여기 localhost 라고 적어야 할 부분에 loca lhost 라고 "공백"이 있더라고요! 이 공백을 제거해보시면 잘 될 것 같습니다.

  • url: "jdbc:mysql://loca lhost/library"

 

잘 해결되셨으면 좋겠습니다. 감사합니다! 🙏

0

최태현님의 프로필 이미지
최태현
지식공유자

2024. 03. 26. 14:08

안녕하세요 우석님! 질문 주셔서 감사합니다. 🙂

프로그램을 설치하는 것만큼, 초기 설정을 하는 것도 역시나 어려운 문제인데요! 😢

 

먼저, 8.3.0 이라고 버전을 명시했을 때 나오는 "Could not find mysql:mysql-connector-java:8.3.0" 에러는 해당 버전을 갖는 mysql-collector-java 라는 모듈을 찾을 수 없다라는 에러입니다!

따라서 올바른 버전 혹은 올바른 모듈 이름으로 변경해주셔야 하는데요! 제공해드리는 build.gradle을 그대로 사용하고 계시면, 원본 build.gradle에 나온 그대로 사용하셔도 문제 없을 거에요!

 

다음으로

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

라는 에러는 우선 표면적으로 봤을 때, DB와 스프링 사이의 연결이 불가능하다! 라는 에러인데요! 관련한 에러로 검색해보니,

https://velog.io/@quarara01/Communications-link-failure-%EC%97%90%EB%9F%AC-%ED%95%B4%EA%B2%B0

처럼 MySQL이 제대로 켜져 있지 않아 발생하는 경우가 많은 것 같습니다.

따라서 MySQL이 잘 켜져 있는지 확인해보시면 좋을 것 같아요.

 

혹시나 MySQL을 정상적으로 실행시켰는데도 여전히 동일한 에러가 나온다면, stack trace 라고 불리는 전체 에러를 보여주시면 더 한 번 찾아보겠습니다.

 

꼭 해결되셨으면 좋겠습니다. 감사합니다! 🙏