본문 바로가기
프로젝트

GiftFunding) 인텔리제이 DB 연결 (Mysql)

by son_i 2023. 11. 21.
728x90

build.gradle에 의존성 추가

	runtimeOnly 'com.mysql:mysql-connector-j'

 

데이터베이스 생성

create database [db명];

 

계정 생성

create user '계정명'@'도메인 접속 권한' identified by '패스워드';
flush privilges;
 		ex) 'testdb1'@'%' identified by 1234;
       // % : 모든 IP 도메인에서 접속허용
       // localhost : 127.0.0.1에서만 접속허용

 

계정에 DB 접근 권한 부여

grant all privileges on DB명.* to '계정명'@'도메인 접속 권한';
flush privileges;

 

 

application.properties에 db연결을 위한 정보 입력

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/[db명]?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=[생성한 계정 명]
spring.datasource.password=[설정한 비밀번호]

 

기타 설정

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

 

 

spring.jpa.hibernate.ddl-auto=create-drop

: 테이블을 생성하고 종료시점에 drop
- spring.jpa.show-sql=true

: hibernate에서 실행하는 SQL문을 출력하도록 설정

- spring.jpa.properties.hibernate.format_sql=true

: 위의 show-sql에서 출력하는 쿼리문을 포매팅해서 가독성을 높이도록 설정.

 

참조

DB에 따른 application.properties 속성 (tistory.com)