본문으로 바로가기

Spring Boot & MySQL 연동하기

category 언어/SpringBoot 2022. 1. 28. 14:38
728x90

 

1. 아래 링크를 통해 버전에 맞는 MySQL 다운로드

 

https://dev.mysql.com/downloads/mysql/

 

MySQL :: Download MySQL Community Server

Select Operating System: Select Operating System… Microsoft Windows Ubuntu Linux Debian Linux SUSE Linux Enterprise Server Red Hat Enterprise Linux / Oracle Linux Fedora Linux - Generic Oracle Solaris macOS Source Code Select OS Version: All Windows (x86

dev.mysql.com

 

 

2. MySQL Schema 생성

 

이때, 중요한 점은 root권한에서 실행해야 한다는 점이다.

 

 

3. MySQL User 생성 & 권한 설정하기

 

 

 

 

 

4. Schema와 User 연동

 

Schema Privileges 탭으로 이동해 스키마와 권한 설정

 

 

 

5. Connection Name, Username, Password 설정

 

MySQL WorkBench 홈에서 +버튼을 통해 MySQL Connections 클릭

 

 

 

6. build.gradle에 의존성 추가 (gradle 기준)

 

implementation 'mysql:mysql-connector-java'

 

7. application.yml 생성

 

# MySQL 설정

server:
  address: localhost
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/{schema name}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&useSSL=true
    username: {username}
    password: {password}

  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        show_sql: true
        format_sql: true

# createDatabaseIfNotExist: 데이터베이스가 존재하지 않으면 자동으로 생성
# useUnicode: 유니코드 사용 여부 설정
# characterEncoding: 문자열 인코딩 종류 설정
# characterSetResult: 결과값의 인코딩 종류 설정
# useSSL: SSL 사용여부 설정

# spring.jpa.properties.hibernate.show_sql : 하이버네이트가 실행한 모든 SQL문을 콘솔로 출력
# spring.jpa.properties.hibernate.format_sql : SQL문을 가독성 있게 표현
728x90