βš™οΈ Devops/🐳 Docker

[Docker] MySQL μ„€μΉ˜ν•˜κΈ°

iseunghan 2022. 5. 23. 22:24
λ°˜μ‘ν˜•

docker-compose λ‹€μš΄λ‘œλ“œ

sudo curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  • κΆŒν•œ μ„€μ •
sudo chmod +x /usr/local/bin/docker-compose
  • μ„€μΉ˜ 확인
docker-compose --version
 

Docker, Docker-Compose μ„€μΉ˜ν•˜κΈ° - Ubuntu 20.04 LTS

Docker μ„€μΉ˜ μ‹€ν–‰ν™˜κ²½: Ubuntu 20.04 LTS μ—…λ°μ΄νŠΈ 및 HTTP νŒ¨ν‚€μ§€ μ„€μΉ˜ $ sudo apt update $ sudo apt-get install -y ca-certificates \ curl \ software-properties-common \ apt-transport-https \ gnupg \ ls..

iseunghan.tistory.com

 

docker-compose 디렉토리 생성

μ΅œμ’…μ μΈ 디렉토리 ꡬ쑰

docker-mysql
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ .env
└── mysql
    β”œβ”€β”€ conf.d
    β”‚   └── my.cnf
    β”œβ”€β”€ data
    └── initdb.d
        β”œβ”€β”€ create_table.sql
        └── load_data.sql

4 directories, 4 files
  • docker-compose.yml : μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰ν•  λ•Œ, λ”μš± μ‰½κ²Œ μ„€μ •ν•˜κΈ° μœ„ν•œ 파일
  • conf.d : MySQL μ„€μ • 파일이 μœ„μΉ˜
  • data : MySQL 데이터가 μ €μž₯λ˜λŠ” μœ„μΉ˜
  • initdb.d : Docker μ»¨ν…Œμ΄λ„ˆκ°€ 졜초 μ‹€ν–‰ μ‹œ 뢈러올 *.sh , *.sql λ“± 파일 μ΄λ¦„μ˜ μ‚¬μ „μˆœμ„œλŒ€λ‘œ 싀행됨.

1. μž„μ˜λ‘œ docker-mysql μ΄λΌλŠ” μž‘μ—… 디렉토리 생성

mkdir docker-mysql && cd docker-mysql

2. DB 계정 정보 생성

.env 생성

MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD={root_pw}
MYSQL_DATABASE={dbλͺ…}
MYSQL_USER=user
MYSQL_PASSWORD={user_pw}

3. 각 폴더 및 파일 생성

mkdir -p mysql/conf.d
mkdir -p mysql/data
mkdir -p mysql/initdb.d

conf.d μ•ˆμ—λŠ” my.cnf λ₯Ό μ•„λž˜μ™€ 같이 μž‘μ„±ν•©λ‹ˆλ‹€.

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server           = utf8mb4
collation-server               = utf8mb4_unicode_ci

init.d μ•ˆμ—λŠ” create.sql κ³Ό load_data.sql λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

  • create.sql (μ˜ˆμ‹œ)
  • CREATE TABLE user( username VARCHAR(25) PRIMARY KEY, password VARCHAR (50) not null );
  • load_data.sql (μ˜ˆμ‹œ)
  • INSERT INTO user(username, password) VALUES ('user', 'pass');

4. docker-compose.yml

version: "3"

services:
  db:
    container_name: docker-mysql
    image: mysql
    ports:
      - 3306:3306
    volumes:
      - ./mysql/conf.d:/etc/mysql/conf.d
      - ./mysql/data:/var/lib/mysql
      - ./mysql/initdb.d:/docker-entrypoint-initdb.d
    env_file: .env
    environment:
      TZ: Asia/Seoul
    restart: always

# μ •ν™•ν•œ μž‘μ„±λ²•μ€ μ•„λž˜ 링크 μ°Έμ‘°
# https://docs.docker.com/compose/compose-file/
  • image: mysql latest 버전 μ‚¬μš© (νŠΉμ • 버전 μ‚¬μš©ν•˜κ³  μ‹ΆμœΌλ©΄ → mysql:0.0.1 μ΄λŸ°μ‹μœΌλ‘œ μž‘μ„±)
  • ports: μ»¨ν…Œμ΄λ„ˆ 포트 3306 개방
  • volumes: Docker μ»¨ν…Œμ΄λ„ˆμ˜ νŒŒμΌμ„ μ‚¬μš©μž μ‹œμŠ€ν…œμ˜ νŒŒμΌμ— μ—°κ²°
  • TZ: DB Time Zone μ„€μ •

docker-compose둜 μ‹€ν–‰!

μ‹€ν–‰

$ docker-compose -f docker-compose.yml up -d
  • -f : λ³„λ„μ˜ docker-compose νŒŒμΌμ„ μ‚¬μš©ν•˜κ³  싢을 λ•Œ
  • -d : λ°±κ·ΈλΌμš΄λ“œλ‘œ μ‹€ν–‰

μ’…λ£Œ

$ docker-compose down

REFERENCE

λ°˜μ‘ν˜•