티스토리 뷰

Backend/Docker

Docker. mysql container 만들기

out of coding 2020. 4. 30. 15:51

docker를 이용하여 MySQL의 Container를 띄우고 접근하는 방법까지만 정리하여 보려고 합니다.

 

MySQL docker image download

이곳에 가서 자신이 사용하고 싶은 버전을 가져옵니다.

https://hub.docker.com/_/mysql

자신이 원하는 적당한 버전을 받아서 사용하도록 합니다.

$ sudo docker pull mysql:[version]

뒤에 버전을 넣지 않고 그냥 mysql 까지만 넣게 되면 마지막 버전을 가져오게 됩니다.

$ docker pull mysql

이미지가 받아졌는지 확인 ( 솔찍히 하면서 프로그래스 막 지나가면서 complete 보이기는 합니다. )

$ docker images

 

docker run를 이용하는 방법

$ docker run -d -p 3306:3306 \
	-e MYSQL_ROOT_PASSWORD=password \
	--name mysql-example \
	-v /data:/var/lib/mysql mysql:8.0.17 \
	--character-set-server=utf8mb4 \
	--collation-server=utf8mb4_unicode_ci

그냥 바로 실행을 하는건데 3306 포트로 받아서 3306으로 넣어주는 방법입니다.

저는 mysql 버전은 8.0.17을 사용하였습니다.

뒤에 --character 부터는 한글에 대한 문제가 발생 할 수 있어서 넣어준 부분입니다.

 

docker-compose를 이용하는 방법

docker-compose.yml 파일을 생성하여 줍니다. 이 방법이 조금 더 아름답기 때문에 저는 이걸 추천합니다.

version: "3"
services:
  mysqlexample:
    image: mysql:8.0.17
    container_name: mysql-example
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "password"
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    volumes:
      - /data:/var/lib/mysql

위에 적은 부분들과 1:1로 매칭되므로 따로 정리하지 않습니다.

실행 방법

$ docker-compose up -d

 

docker container list 출력

$ docker ps -a

 

 

MySQL container bash shell connect

$ docker exec -it mysql-example bash

 

MySQL Server 접속하기

root@da6eab402727:/# 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.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>
mysql>
mysql>

local에 설치하고 접속하는 방법과 동일하게 하여 줍니다. password는 이미 위에 docker container를 만들때 설정하여 주었습니다.

(password)

 

사용자 생성 및 권한 부여

mysql> CREATE USER 'player'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'player'@'%';
Query OK, 0 rows affected (0.02 sec)

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

mysql> quit
Bye
root@da6eab402727:/#

player라는 계정을 만들고 %를 이용하여서 외부에서 들어올수 있도록 만들었습니다.

패스워드는 예제라 password로 지정하였습니다. :D

 

테스트

DBeaver 같은걸로 테스트 해보면 됩니다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함