도커 환경에서 MySQL 설치 및 접속

macOS 기준으로 도커 환경에서 MySQL 설치 및 접속에 대한 가이드를 제공한다.

1. 도커 설치

다운로드 및 설치

버전 확인

  • 설치가 완료되면 버전을 출력해 정상적으로 출력되는지 확인한다.

$ docker -v 
Docker version 24.0.2, build cb74dfc

2. MySQL 이미지 다운로드

  • docker pull 명령어를 사용해 MySQL 도커 이미지를 다운로드 한다.

  • 태그에 버전을 명시하지 않으면 자동으로 최신 버전을 다운로드 한다.

$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
1817bc1e6309: Pull complete 
740bd54462bc: Pull complete 
a7e5ed4e69b3: Pull complete 
8fdf88d7bbb7: Pull complete 
a1a5f8560950: Pull complete 
82b514ba21a5: Pull complete 
d6a4cb36f5f9: Pull complete 
3392803e2ec5: Pull complete 
dcc1a15c36b9: Pull complete 
3e1c4ca2fc97: Pull complete 
Digest: sha256:6a5dbd2819e36048669639811461f27fee48da1e22039e5d31f4273a20d542f6
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
  • docker images 명령어를 사용해 설치되어 있는 최신 버전과 지정한 8.0.34 버전을 확인할 수 있다.

$ docker images
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
mysql                  latest    de7c37d1d5ac   3 days ago    599MB
mysql                  8.0.34    2031a058d3ba   12 days ago   599MB

3. MySQL 도커 이미지로 컨테이너 생성 및 실행

  • docker run 명령어를 사용해 다운로드 받은 이미지로 컨테이너를 실행할 수 있다.

  • 생성 및 실행되는 컨테이너 이름은 mysql-container이다.

  • MySQL 접속시 사용되는 루트 패스워드는 <password>에 지정해주면 된다.

$ docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=<password> -d -p 3306:3306 mysql:latest

4. 실행중인 도커 컨테이너 리스트 출력

  • docker ps -a 명령어로 컨테이너 생성 및 실행 후 정상적으로 실행되고 있는지 확인할 수 있다.

$ docker ps -a
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                               NAMES
70c1ef9b8e8c   772571a08c67                  "docker-entrypoint.s…"   56 minutes ago   Up 50 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-container

실행중인 MySQL 도커 컨테이너 시작/중지/재시작 하려면 다음과 같은 명령어를 사용해야 한다.

# MySQL 도커 컨테이너 중지

$ docker stop mysql-container

# MySQL Docker 컨테이너 시작

$ docker start mysql-container # MySQL 도커 컨테이너 재시작

$ docker restart mysql-container

5. 실행중인 MySQL 도커 컨테이너 접속

  • docker exec 명령어를 사용하여 도커 Docker 컨테이너를 정상적으로 실행했으면 컨테이너에 접속한다.

  • 컨테이너에 접속해 MySQL 에도 접속하여 원하는 작업을 수행할 수 있다.

$ docker exec -it mysql-container bash
bash-4.4# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| library            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> 

Last updated