IT/Container

[Docker] mysql docker로 실행하기..

louky 2021. 3. 4. 15:13
반응형

 

먼저 Docker를 설치 한다. 

2019/07/17 - [IT/Container] - [Docker] docker install

 

docker가 설치 되어 있다면 docker hub에서 사용하고자 하는 mysql  버전의 docker image를 다운로드 한다. 

### 필자는 최신버전으로 설치하였다. 

# docker pull mysql:latest                     
latest: Pulling from library/mysql
45b42c59be33: Pull complete
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Pull complete
790ac54f4c47: Pull complete
b0ddd5d1b543: Pull complete
1aefd67cb33d: Pull complete
Digest: sha256:7706e4c382be813b58ef514f2bdac747cd463a6866c6c81165d42a1d0e4fe947
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

Download가 잘되었는지 확인한다. 

# docker image ls mysql
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    8457e9155715   5 days ago   546MB

 

Docker를 실행한다. 

 

USAGE) docker run -d -p 외부포트:Docker내부포트 -e MYSQL_ROOT_PASSWORD=사용할DB패스워드 --name Docker_실행이름 mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

sh-4.2# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysql.1234 --name mysql8 mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
90e995884173014b15ff0db0035de80eaf62965f5631d35e71870564068e2c4e


## Docker 실행 여부 확인 
sh-4.2# docker ps -f name=mysql8
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
90e995884173   mysql:latest   "docker-entrypoint.s…"   23 seconds ago   Up 23 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql8

docker 에 접속하여 정상적으로 mysql이 실행 되었는지 확인한다. 

sh-4.2# docker exec -it mysql8 bash
root@90e995884173:/#

 

Docker 내 mysql 실행 여부 확인 

root@90e995884173:/# mysql -uroot -p
Enter password:
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> exit


Container외부에서 확인 하는 방법(호스트 머신)

### 실행중인 mysql container의 PID를 확인 한다. 

sh-4.2# docker inspect -f '{{.State.Pid}}' mysql8
986


### 확인된 PID를 이용하여 netstat으로  port listen상태를 확인한다. 

sh-4.2# nsenter -t 986 -n netstat -tupln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 :::33060                :::*                    LISTEN      986/mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      986/mysqld
sh-4.2#

 

 container 외부에서 접근하고자 할 경우  접근 권한을 설정해야 한다. 

 

- 접근 권한 설정 전 = 아래와 같이 Error 가 발생하면서 접근이 불가능하다.

sh-4.2# mysql -h 0 -p 3306 -umysql -p
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'mysql'@'172.17.0.1' (using password: YES)

 접근 권한 설정  = Container로 접속하여 권한 설정을 한다. 

root@90e995884173:/# mysql -uroot -p
Enter password:
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> 

mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

반응형