Skip to content

도커 컴포즈를 사용한 MySQL 관리

  1. 작업 디렉토리 생성 및 docker-compose.yaml 파일 생성
    Terminal window
    mkdir ${WORKING_DIRECTORY}
    cd ${WORKING_DIRECTORY}
    touch docker-compose.yaml
  2. docker-compose.yaml 파일 편집
    docker-compose.yaml
    services:
    mysql:
    container_name: mysql
    image: mysql:latest
    restart: always
    ports:
    - "3306:3306"
    environment:
    MYSQL_ROOT_PASSWORD: ********
    MYSQL_USER: ********
    MYSQL_PASSWORD: ********
    volumes:
    - ./db/conf.d:/etc/mysql/conf.d
    - ./db/data:/var/lib/mysql
    - ./db/initdb.d:/docker-entrypoint-initdb.d
  3. MySQL 기동
    Terminal window
    docker-compose up -d
  1. MySQL 초기 설정은 도커 컨테이너에 접속해서 명령어를 입력해야 한다.
  2. MySQL 컨테이너의 bash 실행
    Terminal window
    docker-compose exec mysql bash
    $ docker-compose exec mysql bash
    bash-5.1#
  3. 관리자 권한으로 접속
    Terminal window
    mysql -u root -p mysql
    bash-5.1# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 20
    Server version: 9.5.0 MySQL Community Server - GPL
    Copyright (c) 2000, 2025, Oracle and/or its affiliates.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql>
  4. 연결 테스트
    Terminal window
    show databases;
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | sys |
    +--------------------+
    4 rows in set (0.001 sec)

MySQL 데이터베이스 및 사용자 생성

Section titled “MySQL 데이터베이스 및 사용자 생성”
  1. Database 생성
    create database ${DATABASE_NAME};
  2. 사용자 생성
    create user '${USER_NAME}'@'%' identified by '${PASSWORD}';
  3. 사용자 조회
    select * from user where user = '${USER_NAME}';
  4. 권한 설정
    grant all privileges on ${DATABASE_NAME}.* to '${USER_NAME}'@'%' with grant option;
  5. 모니터링 권한 설정
    grant process on *.* to ${USER_NAME};
    grant select on performance_schema.* to ${USER_NAME};
    grant show databases on *.* to ${USER_NAME};
  6. 설정내역 디스크에 저장
    flush privileges;
  1. 외부에서 데이터베이스 작업을 수행한다.
    1. 테이블 및 데이터 생성
    2. 인덱스 생성
  2. 데이터베이스를 사용하는 애플리케이션 개발이 가능하다.
    1. 웹 애플리케이션 개발
    2. 모바일 애플리케이션 개발