Skip to content

[환경 설정 문제] Docker에서 실행 중인 MySQL과 Spring Boot 간의 시간대(Timezone) 차이 발생 #43

@wjkim9

Description

@wjkim9

📌 개요

현재 Spring Boot 애플리케이션과 MySQL 서버를 각각 Docker 컨테이너로 실행 중입니다.
Spring Boot 애플리케이션은 Asia/Seoul (KST, UTC+9)을 기준으로 동작하도록 설정했지만, MySQL은 기본적으로 UTC 기준으로 작동하고 있어 시간 차이로 인한 데이터 불일치 문제가 발생하고 있습니다.


⚠️ 문제 발생

Image

LocalDateTime.now()로 저장된 값과 NOW() 또는 CURDATE() 등 MySQL 함수 결과 간에 9시간 차이 발생

예: published_at >= CURDATE() 조건이 의도와 다르게 동작

자정 기준의 일간 작업(예: 어제 뉴스 필터링, 배치 처리 등)에서 날짜 기준 불일치 발생


🔍 원인 분석

Spring Boot 애플리케이션은 application.yml 또는 시스템 설정을 통해 Asia/Seoul로 명시 설정됨

MySQL Docker 컨테이너는 기본적으로 UTC를 사용하며, 명시적으로 타임존 설정이 되지 않음

서로 다른 시간대를 기준으로 날짜 및 시간 연산이 수행되면서 의도한 기준과 다른 결과가 발생


✅ 해결 방법

1. MySQL 컨테이너 타임존 설정

Dockerfile 또는 docker-compose.yml에 환경 변수를 설정:

services:
  mysql:
    image: mysql:8
    environment:
      - TZ=Asia/Seoul
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

또는 init.sql에 명시적으로 설정:

SET GLOBAL time_zone = '+9:00';

2. MySQL JDBC 연결 URL에 타임존 명시

Spring Boot application.yml 또는 application.properties에서 설정:

spring:
  datasource:
    url: jdbc:mysql://mysql:3306/db_name?serverTimezone=Asia/Seoul

3. MySQL 서버 내부에서 타임존 확인 및 변경

-- 현재 시간대 확인
SELECT @@global.time_zone, @@session.time_zone;

-- 변경 (세션 기준)
SET time_zone = 'Asia/Seoul';

📎 참고

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions