본문 바로가기

CS/Spring

h2 DB에서 mysql로 변경하기

반응형
h2 DB에서 mysql로 변경하기

h2 DB를 기본적으로 사용하다가 RDS를 사용하기 위해서 mysql로 변경해야 하는 일이 생겨 변경 도중 오류가 발생하고 이를 해결하는 내용이다.

 

기존 yml 코드

spring:
  h2:
    console:
      enabled: true
      path: /h2-console
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb
    username: sa
    password:

아주 문제없이 잘 작동되는 코드

 

변경 후 yml 코드

spring:
  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: create
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://---.rds.amazonaws.com:3306
    username: -----
    password: -----

yml 코드를 변경 후 애플리케이션을 실행하니 

Hibernate: 
    
    alter table tb_address 
       drop 
       foreign key FKhx6ab79y261wkrfa11h092ft
2022-12-02 20:49:26.516  WARN 17170 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "

다음과 같은 오류를 볼 수 있었다... 심지어 저거 하나만 나온 게 아니라 alter table실행마다 같은 오류가 반복되는 것을 확인할 수 있었다. (오류가 너무 많이 나와 터미널에서 다 확인할 수 없어 터미널 설정도 바꿔줬다.)

이를 해결하기 위해서 많은 방법을 사용해 보았다.

 

  1. 처음에 jpa의 generate-ddl의 default 값이 false여서 true로 변경을 해주었다. (실패)
  2. ddl-auto 부분을 update로 변경해 보았다. (실패)

위 두 개를 적용시키니 오류 코드가 변경되었다. (뭔가 된 줄..)

만들어지는데 한번 더 만든다.. 뭘까..? 모든 테이블을 2번씩 만든다..

Hibernate: 
    
    create table tb_address (
       address_id bigint not null auto_increment,
        create_by datetime,
        modify_by datetime,
        address_name varchar(255) not null,
        region_depth_1 varchar(255) not null,
        region_depth_2 varchar(255) not null,
        user_id bigint,
        primary key (address_id)
    ) engine=InnoDB
2022-12-02 21:07:15.939  WARN 17404 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "
    create table tb_address (
       address_id bigint not null auto_increment,
        create_by datetime,
        modify_by datetime,
        address_name varchar(255) not null,
        region_depth_1 varchar(255) not null,
        region_depth_2 varchar(255) not null,
        user_id bigint,
        primary key (address_id)
    ) engine=InnoDB" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "

 

한참을 헤매다가 결국 찾아냈다! 로그를 위만 봐서 문제를 해결하지 못한 것이었다!!

- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "

이 강력해 보이는 오류 때문에 밑에 로그에 적혀있던 문장을 볼 생각을 안 하고 구글에 계속 ExceptionHandlerLoggedImpl 만 검색하고 있었다.

java.sql.SQLException: No database selected라는 오류 로그를 밑에서 발견했는데 이 오류는 yml 파일 안 url이 잘못되어 사용할 DB를 찾지 못해 발생한 오류였다.

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/numble
    username: root
    password: 12341234

위와 같이 url 부분이 jdbc:mysql://localhost:3306/numble(db명) 있어야 했는데 jdbc:mysql://localhost:3306로 구성되어 있어서 DB를 찾지 못한 것이었다!

 

url을 맞춰 넣어주니 table이 자동으로 잘 생성되는 것을 확인할 수 있었다.

한참을 고생했는데 막상 알고 보니 멍청한 실수로 이런 고생을 했다.. 

반응형

'CS > Spring' 카테고리의 다른 글

Spring Bean 알아보기  (0) 2024.05.31
DataSource 알아보기  (1) 2024.03.31
Connection Pool 이해하기  (4) 2024.03.23
Spring MVC과 관심사 분리  (1) 2024.03.17
의존관계 주입 방법 4가지  (1) 2022.09.28