[Docker] Mysql 컨테이너를 Spring Boot에 연동하기

DaeHoon·2022년 6월 20일
0

Docker에 Mysql 컨테이너 구동 시 이점

  • 다른 어플리케이션과 분리된 환경이라 환경이 서로 혼합되지 않아, 유지보수가 더 쉬워진다.
  • 컨테이너를 실행함으로 반복 가능한 config와 인프라를 갖춘 코드를 개발하므로 개발 프로세스의 속도가 빨라진다. (By running a Docker MySQL Container you are developing code with repeatable configuration and infrastructure, thus speeding up the development process considerably.)

참고 : https://hevodata.com/learn/docker-mysql/#t3

yml

## docker-compose.yml
version: "3.9"
services:
  db:
    image: mysql:8.0.28
    platform: linux/x86_64
    restart: always
    ports:
      - "3333:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: user
      MYSQL_PASSWORD: 1234
      TZ: Asia/Seoul
    volumes:
      - ./db/mysql/data:/var/lib/mysql
      - ./db/mysql/config:/etc/mysql/conf.d
      - ./db/mysql/init:/docker-entrypoint-initdb.d
  • volume을 이용해 컨테이너의 데이터와 설정을 프로젝트로 migration한다.

volume?

  • Docker 컨테이너(container)에 쓰여진 데이터는 기본적으로 컨테이너가 삭제될 때 함께 사라지게 된다. Docker에서 돌아가는 많은 애플리케이션이 컨테이너의 생명 주기와 관계없이 데이터를 영속적으로 저장을 해야한다. 뿐만 아니라 많은 경우 여러 개의 Docker 컨테이너가 하나의 저장 공간을 공유해서 데이터를 읽거나 써야한다.

  • 이렇게 Docker 컨테이너의 생명 주기와 관계없이 데이터를 영속적으로 저장할 수 있도록 Docker는 두가지 옵션을 제공하는데, 첫번째는 Docker 볼륨(volume), 두번째는 바인드 마운트(bind mount)이다

참고 : https://www.daleseo.com/docker-volumes-bind-mounts

## application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3333/test_db?serverTimezone=UTC&characterEncoding=UTF-8
    username: user
    password: 1234
  • docker-compose에서 설정한 값들을 datasource에도 똑같이 적는다.

실행

  1. 터미널을 열고 'docker-compose up'
```docker-compose up
autotrading_db_1 is up-to-date
Attaching to autotrading_db_1
db_1  | 2022-06-19 14:21:40+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-19 14:21:40+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1  | 2022-06-19 14:21:40+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-19T05:21:40.993304Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1
db_1  | 2022-06-19T05:21:40.998897Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
db_1  | 2022-06-19T05:21:41.015861Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1  | 2022-06-19T05:21:41.534562Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1  | 2022-06-19T05:21:41.732211Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
db_1  | 2022-06-19T05:21:41.740205Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
db_1  | 2022-06-19T05:21:41.832344Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1  | 2022-06-19T05:21:41.832417Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1  | 2022-06-19T05:21:41.837216Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1  | 2022-06-19T05:21:41.882518Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1  | 2022-06-19T05:21:41.882535Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
db_1  | 2022-06-19T05:22:12.380782Z 8 [Warning] [MY-010055] [Server] IP address '172.26.0.1' could not be resolved: Name or service not known
db_1  | 2022-06-19 19:23:33+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-19 19:23:33+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1  | 2022-06-19 19:23:33+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-19T10:23:33.852184Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1
db_1  | 2022-06-19T10:23:33.860716Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
db_1  | 2022-06-19T10:23:33.884611Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1  | 2022-06-19T10:23:34.963729Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1  | 2022-06-19T10:23:35.172944Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
db_1  | 2022-06-19T10:23:35.182078Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
db_1  | 2022-06-19T10:23:35.280031Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1  | 2022-06-19T10:23:35.280108Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1  | 2022-06-19T10:23:35.286057Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1  | 2022-06-19T10:23:35.331768Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1  | 2022-06-19T10:23:35.331853Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
db_1  | 2022-06-19T10:27:10.762174Z 8 [Warning] [MY-010055] [Server] IP address '172.26.0.1' could not be resolved: Name or service not known
db_1  | 2022-06-20 08:59:45+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-20 08:59:45+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1  | 2022-06-20 08:59:45+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
db_1  | 2022-06-19T23:59:46.006127Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1
db_1  | 2022-06-19T23:59:46.011944Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
db_1  | 2022-06-19T23:59:46.035824Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1  | 2022-06-19T23:59:46.698011Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1  | 2022-06-19T23:59:46.894023Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
db_1  | 2022-06-19T23:59:46.902337Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
db_1  | 2022-06-19T23:59:46.996512Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1  | 2022-06-19T23:59:46.996588Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1  | 2022-06-19T23:59:47.001648Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1  | 2022-06-19T23:59:47.043859Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1  | 2022-06-19T23:59:47.044002Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
  1. 'docker ps' 명령어로 mysql 이미자가 컨테이너로 생성이 되었는지 확인한다.
 docker ps                            
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS          PORTS                               NAMES
47769f638134   mysql:8.0.28   "docker-entrypoint.s…"   19 hours ago   Up 53 seconds   33060/tcp, 0.0.0.0:3333->3306/tcp   autotrading_db_1
  1. 프로젝트 실행
profile
평범한 백엔드 개발자

0개의 댓글