Product/Docker

[Docker] MariaDB 올리기

Codit Develop 2021. 5. 13. 17:02
반응형

MariaDB Docker Image 생성 및 실행

1. 폴더 구조

2. Source

Dockerfile

FROM mariadb

ENV MYSQL_ROOT_PASSWORD qwer1234

WORKDIR /etc/mysql
RUN mkdir setDB
WORKDIR /etc/mysql/setDB

//초기 세팅 SQL 파일 Docker로 Copy
COPY ./Config/setDB.sql /etc/mysql/setDB/setDB.sql

//MariaDB Config Setting (table 소문자, 한국 시간, 한글 깨짐 수정 등)
RUN echo lower_case_table_names=1 >> /etc/mysql/conf.d/docker.cnf
RUN echo default-time-zone='+9:00' >> /etc/mysql/conf.d/docker.cnf
RUN echo collation-server = utf8mb4_unicode_ci >> /etc/mysql/conf.d/docker.cnf
RUN echo collation-server = utf8mb4_0900_ai_ci >> /etc/mysql/conf.d/docker.cnf
RUN echo character-set-server = utf8mb4 >> /etc/mysql/conf.d/docker.cnf
RUN echo skip-character-set-client-handshake >> /etc/mysql/conf.d/docker.cnf
  • ROOT 비밀번호 설정
    • ENV MYSQL_ROOT_PASSWORD [Password]

Docker-compose

version: '3'
services:
  MariaDB:
    container_name: MariaDB
    restart: always
    build: 
      context: .
      dockerfile: ./DockerFile/MariaDB_Dockerfile
      args: 
        buildno: 1.0
    ports :
      - "35000:3306"
  • 외부 접속 시 Port 35000 으로 접속할 수 있게 포워딩.
  • 컨테이너/이미지 이름 : MariaDB

Set DB SQL

CREATE DATABASE DB_CREATEDB ;

create user 'admin'@'%' identified by 'qwer1234';
grant all privileges on DB_CREATEDB.* to 'admin'@'%' identified by 'qwer1234';
flush privileges;
  • DB_CREATEDB 생성
  • admin/qwer1234 유저 생성 후 DB_CREATEDB의 모든 권한 부여

Set DB Script (Windows)

@echo off

set CONTAINER_NAME=MariaDB
set SET_SQL_PATH=/etc/mysql/setDB/setDB.sql
set SET_SQL_DELAY=10
set DB_ROOT_UID=root
set DB_ROOT_PASSWORD=system32


echo.
echo.
echo [RUN] 1. Remove Container '%CONTAINER_NAME%' 
echo.
docker stop %CONTAINER_NAME%
docker rm %CONTAINER_NAME%
docker rmi %CONTAINER_NAME%
echo.
echo [RUN] 2. Run Container
echo.
docker-compose up -d
echo.
echo [RUN] 3. Set DB
echo Wait for %SET_SQL_DELAY% second
timeout %SET_SQL_DELAY%
echo.
docker exec %CONTAINER_NAME% sh -c "mysql -u%DB_ROOT_UID% -p%DB_ROOT_PASSWORD% < %SET_SQL_PATH%"
echo. 
echo [RUN COMPLETE]
echo.
echo.
  • Dockerfile 에서 설정한 ROOT 계정 비밀번호를 DB_ROOT_PASSWORD에 입력
  • Docker-compose 에서 설정한 컨테이너 이름을 CONTAINER_NAME에 입력

Set DB Script (Linux)

CONTAINER_NAME=MariaDB
SET_SQL_PATH=/etc/mysql/setDB/setDB.sql
SET_SQL_DELAY=10
DB_ROOT_UID=root
DB_ROOT_PASSWORD=system32


echo ""
echo ""
echo "[RUN] 1. Remove Container '$CONTAINER_NAME' "
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
docker rmi $CONTAINER_NAME
echo ""
echo "[RUN] 2. Run Container"
docker-compose up -d
echo ""
echo "[RUN] 3. Set DB"
echo "Wait for 5 second"
sleep $SET_SQL_DELAY s
docker exec $CONTAINER_NAME sh -c "mysql -u$DB_ROOT_UID -p$DB_ROOT_PASSWORD < $SET_SQL_PATH"
echo ""
echo "[RUN COMPLETE]"
echo ""
echo ""
  • Dockerfile 에서 설정한 ROOT 계정 비밀번호를 DB_ROOT_PASSWORD에 입력
  • Docker-compose 에서 설정한 컨테이너 이름을 CONTAINER_NAME에 입력

3. 실행

# Window
./setDB.bat

# Linux
sh ./setDB.sh

4. 예제 파일

MariaDB.zip
0.00MB

 

반응형