[Spring Boot] MySQL를 이용한 간단한 API 제작

2020. 12. 22. 17:10Spring/Spring Boot

1. MySQL

서버의 OS 정보를 확인하고, 그에 맞는 rpm 패키지 링크를 사이트(dev.mysql.com/downloads/mysql/)에서 복사한다.

grep . /etc/*-release

MySQL rpm

// MySQL Repo 설치 및 확인
sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo yum repolist enabled | grep "mysql.*-community.*"


// MySQL 설치
sudo amazon-linux-extras install epel -y
sudo yum install -y mysql-community-server


// MySQL DBMS 환경 설정
sudo vim /etc/my.cnf
----------------------------------------------
[mysqld]

...

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip-character-set-client-handshake

log_bin_trust_function_creators=1
----------------------------------------------
// MySQL 실행
sudo systemctl enable mysqld
sudo systemctl start mysqld


// root 임시 비밀번호 획득 
sudo grep 'temporary password' /var/log/mysqld.log


// 새로운 비밀번호 입력 및 보안 설정 (대문자,숫자,특수문자)
sudo mysql_secure_installation -p
mysql> CREATE DATABASE test;

mysql> CREATE USER root@'%' IDENTIFIED BY '[비밀번호]';
mysql> GRANT ALL PRIVILEGES ON [DB명].* TO 'root'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

모든 설정이 완료되면, 접속 후 'status' 명령으로 character set을 확인한다.

 

 

2. MySQL 설정

1) build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'org.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'junit:junit:4.12'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

//test {
//    useJUnitPlatform()
//}

2) application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useSSL=false
    username: root
    password: [비밀번호]
    hikari:
      initialization-fail-timeout: 0
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    generate-ddl: true
    show-sql: true
    hibernate:
      ddl-auto: create

3) 간단한 API 제작

API 제작 방법은 기존의 방식과 동일하다.


[참고] github.com/ozofweird/SpringBoot_MySQLTest

728x90