데브옵스(DevOps)/Docker

[Docker] 도커(Docker)를 통해 MySQL 설치 및 접속하기

잇트루 2023. 6. 21. 00:07
반응형

Docker 설치하기

Docker 공식 홈페이지에서 자신의 OS 환경에 맞는 Docker를 설치한다.

설치가 끝나면 터미널에서 다음 명령어를 통해 버전을 출력할 수 있다.

$ docker -v
Docker version 20.10.21, build baeda1f

 

 

MySQL 도커 이미지 pull 받기

Docker CLI를 통해 MySQL 이미지를 pull 받을 수 있다. TAG에 버전을 지정하여 특정 버전을 다운로드할 수 있으며, 버전을 지정하지 않을 시 최신 버전을 다운로드한다.

$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
3e0c3751e648: Pull complete
7914193c6f0e: Pull complete
fe4b3f820487: Pull complete
63683b304e3d: Pull complete
6ad9069836bd: Pull complete
de90cd4c0e5d: Pull complete
892e565e2cf0: Pull complete
73057d123da0: Pull complete
af1a3c0ec34e: Pull complete
62fe8dc4ffe9: Pull complete
8807488ae889: Pull complete
Digest: sha256:4bae98614cd6ad1aecbdd32ff1b37b93fb0ee22b069469e7bc9679bacef1abd2
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

 

특정 버전을 pull 받고 싶다면 다음과 같이 TAG를 지정할 수 있다.

$ docker pull mysql:8.0.33
8.0.33: Pulling from library/mysql
Digest: sha256:4bae98614cd6ad1aecbdd32ff1b37b93fb0ee22b069469e7bc9679bacef1abd2
Status: Image is up to date for mysql:8.0.33
docker.io/library/mysql:8.0.33

 

다음 명령어를 통해 다운로드한 이미지를 확인할 수 있다.

$ docker images
REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
mysql                    8.0.33    c71276df4a87   10 days ago    565MB
mysql                    latest    c71276df4a87   10 days ago    565MB

 

 

MySQL 도커 컨테이너 생성 및 실행하기

$ docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=admin -d -p 3306:3306 mysql:8.0.33
  • —name mysql-container : mysql-container라는 이름의 컨테이너 생성
  • -e MYSQL_ROOT_PASSWORD=admin : 패스워드 admin으로 설정
  • -d : 백그라운드 실행
  • -p 3306:3306 : 포트 설정
  • mysql:8.0.33 : 8.0.33 버전 실행

 

 

도커 컨테이너 리스트 출력하는 방법

모든 컨테이너 출력하기

$ docker ps -a

 

실행 중인 컨테이너 출력하기

$ docker ps

 

 

도커 컨테이너 종료 및 실행하기

컨테이너 실행하기

$ docker start mysql-container

 

컨테이너 종료하기

$ docker stop mysql-container
  • mysql-container라는 이름의 컨테이너 실행 종료

 

컨테이너 재시작하기

$ docker restart mysql-container

 

 

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 8
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.
  • docker exec -it mysql-container bash : 터미널로 mysql-container 컨테이너에 접속
  • mysql -u root -p : Mysql에 접속하기

 

접속 후 mysql 명령어를 사용할 수 있다.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
반응형

'데브옵스(DevOps) > Docker' 카테고리의 다른 글

[Docker] 도커(Docker)란 무엇인가?  (0) 2023.06.19