Docker

Docker 설치 mysql 설치 및 실행하기 Ubuntu 18.04

pepega 2021. 10. 22. 15:45

현재 서버에는 mysql 없음

 

ko@ko-MS-16F1:~$ mysql

Command 'mysql' not found, but can be installed with:

sudo apt install mysql-client-core-5.7
sudo apt install mariadb-client-core-10.1

 

설치 시작

Docker 저장소 설정

sudo apt-get update
# 패키지 다운로드
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
# Docker GPG 키 추가
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Docker GPG 키 등록 확인
sudo apt-key fingerprint 0EBFCD88
# Docker 저장소 등록
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

 

Docker Engine 설치

sudo apt-get update

# Docker 최신버전 설치
sudo apt-get install docker-ce docker-ce-cli containerd.io 

# Docker 버전 확인
docker -v

 

Docker Compose 설치

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

 

Docker의 일반사용자 권한 추가 & 제거

sudo usermod -aG docker [your-user] # [your-user] 사용자에게 권한주기
your-user는 서버(ubuntu)상의 계정명을 적으면 됨


sudo deluser [your-user] docker # [your-user] 사용자의 docker 권한 제거

 

Docker 로그인하기

sudo docker login

 

Docker 이미지 확인하기

sudo docker images

ko@ko-MS-16F1:~$ sudo docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

 

Docker 에서 mysql 가져오기(pull)

~$ sudo docker pull mysql

Using default tag: latest
latest: Pulling from library/mysql
ac2522cc7269: Pull complete
3ff4d5966d00: Pull complete
c8a8e9739c9e: Pull complete
8fd773bacd12: Pull complete
adbceeecb7f3: Pull complete
6b53011e5d04: Pull complete
32ae8cea62cb: Pull complete
cf09f068015c: Pull complete
41d80f48621d: Pull complete
fa9d48194f31: Pull complete
8ea01c86a52b: Pull complete
69e9db22376c: Pull complete
Digest: sha256:00b627abbd6e91d5d0e4be786869930497b9759bf40db3f15408bb894daf5263
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

 

Docker 이미지 다시 확인

sudo docker images

ko@ko-MS-16F1:~$ sudo docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    d757c3ddbb3b   3 days ago   546MB

 

Docker mysql 이미지 실행하기

sudo docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password --name my_mysql_server mysql

ko@ko-MS-16F1:~$ sudo docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password --name my_mysql_server mysql
a726803f3917a62314ac1056b80849392187323aef7cf5842880b860cdb86669

-d : 컨테이너 백그라운드 실행

-p : 포트번호 설정 (3306 포트번호로 들어가면 3306 포트번호로 들어가짐)

아마 13306:3306 식으로 해도 괜찮을 듯(포트포워딩 느낌나네)

-e MYSQL_ROOT_PASSWORD : 환경변수(-e) 에 root 비번 설정

--name : 컨테이너 이름 지정

mysql : 이미지 이름

 

 

현재 진행중인 Docker process 확인

sudo docker ps -a

ko@ko-MS-16F1:~$ sudo docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS       PORTS                               NAMES
a726803f3917   mysql     "docker-entrypoint.s…"   11 seconds ago   Up 8 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp   my_mysql_server

 

실행 중인 Docker mysql 서버에 접속

sudo docker exec -i -t my_mysql_server bash

-i, -t : 쉘로 들어가서 mysql 작동(interactive, tty)

 

root@a726803f3917:~# mysql -u root -h

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

 

Docker mysql 서버를 나와서 (exit 명령어)

3306 포트가 열렸는지 확인

root@a726803f3917:/# exit
exit

ko@ko-MS-16F1:~$ netstat -nltp

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State                                                                                             PID/Program name
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN                                                                                            -
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN                                                                                            -
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN                                                                                            -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN                                                                                            -
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN                                                                                            -
tcp6       0      0 :::111                  :::*                    LISTEN                                                                                            -
tcp6       0      0 :::22                   :::*                    LISTEN                                                                                            -
tcp6       0      0 ::1:631                 :::*                    LISTEN                                                                                            -

잘 열려있다

 

윈도우 HeidiSQL 으로도 잘 들어와짐

 

실행 중인 Docker mysql 이미지 끄기

ko@ko-MS-16F1:~$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
a726803f3917   mysql     "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   my_mysql_server
ko@ko-MS-16F1:~$ sudo docker stop a726803f3917
a726803f3917
ko@ko-MS-16F1:~$ netstat -nltp

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp6       0      0 :::111                  :::*                    LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
tcp6       0      0 ::1:631                 :::*                    LISTEN      -

3306 포트가 사라졌다 (꺼짐)

 

종료된 Docker mysql 다시 켜기

sudo docker container start a726803f3917

a726803f3917 : 생성된 mysql 이미지 이름(name)

 

 

다시 실행

sudo docker exec -i -t my_mysql_server bash


root@a726803f3917:/# mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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.

 

ko@ko-MS-16F1:~/docker_test$ mysql

Command 'mysql' not found, but can be installed with:

sudo apt install mysql-client-core-5.7
sudo apt install mariadb-client-core-10.1

여전히 mysql 이 설치 되어있지 않음

무조건 docker를 활용해 들어가야함

 

출처(설치)

https://medium.com/@shaul1991/%EC%B4%88%EB%B3%B4%EA%B0%9C%EB%B0%9C%EC%9E%90-%EC%9D%BC%EC%A7%80-docker-%EC%84%A4%EC%B9%98%ED%95%B4%EB%B3%B4%EC%9E%90-d3d6a11ea098

 

[초보개발자 일지] Docker 설치해보자

Ubuntu 18.04에 Docker 설치해보자.

shaul1991.medium.com