Skip to content

docker mysql persistent data

bohyeon edited this page Dec 12, 2019 · 1 revision

docker-mysql 데이터 보존

data가 저장되는 장소는?

스크린샷 2019-12-11 오후 11.15.00.png

어디에 저장하면 좋을까?

  • /my/own/datadir:/var/lib/mysql

    • 지정된 디렉토리 (디렉토리 생성 후 접근권한도 줘야한다고 한다.)
  • my_data:/var/lib/mysql

    • docker volume 이용
    • docker-compose.yml내에 volumes 항목을 작성해줘야함
  • Let Docker manage the storage of your database data by writing the database files to disk on the host system using its own internal volume management . This is the default and is easy and fairly transparent to the user. The downside is that the files may be hard to locate for tools and applications that run directly on the host system, i.e. outside containers.

  • Create a data directory on the host system (outside the container) and mount this to a directory visible from inside the container . This places the database files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly.

docker-compose.yml

    db:
        build:
            context: ./mysql
            dockerfile: ../docker/mysql.Dockerfile
        environment:
        ports:
            - "3306:3306"
        networks:
            - backend
        volumes:
            - mysql_data:/var/lib/mysql

volumes:
    mysql_data:

기타

Creating database dumps

$ docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

# docker container에서 직접 명령을 내린다면
$ mysqldump --databases mydatabase -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

docker-compose를 down하면서 사용하던 volume도 같이 삭제

$ docker-compose down --volumes
$ docker-compose down -v

쓰지 않는 docker volume, docker image, docker container 삭제

# volume
$ docker volume prune

# image
$ docker image prune

# container
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]

참고

#docker #mysql #docker-compose

Clone this wiki locally