Docker로 MySQL 띄우고 IntelliJ 연결하기
by youngjun._.1. Docker 설치
Docker 홈페이지에 접속하여 자신의 OS에 맞는 Docker를 내려 받아 설치한다.
설치가 완료되면 다음 명령어를 실행하여 버전을 출력해 보자.
$ docker -v Docker version 20.10.16, build aa7e414
2. MySQL Docker 이미지 다운로드
다음 명령어로 MySQL Docker 이미지를 다운로드한다. 태그에 버전을 지정하지 않으면 최신 버전을 다운로드한다.
$ docker pull mysql Using default tag: latest latest: Pulling from library/mysql bb79b6b2107f: Pull complete 49e22f6fb9f7: Pull complete 842b1255668c: Pull complete 9f48d1f43000: Pull complete c693f0615bce: Pull complete 8a621b9dbed2: Pull complete 0807d32aef13: Pull complete a56aca0feb17: Pull complete de9d45fd0f07: Pull complete 1d68a49161cc: Pull complete d16d318b774e: Pull complete 49e112c55976: Pull complete Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
MySQL 버전을 지정하려면 태그에 버전을 지정한다. 다운로드할 수 있는 MySQL 버전은 docker hub에서 확인할 수 있다. 예를 들어, MySQL 8.0.22 버전을 다운로드하려면 다음과 같이 태그에 버전을 지정한다.
$ docker pull mysql:8.0.22 8.0.22: Pulling from library/mysql Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d Status: Downloaded newer image for mysql:8.0.22 docker.io/library/mysql:8.0.22
다음 명령어로 다운로드한 Docker 이미지를 확인한다.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0.22 db2b37ec6181 2 weeks ago 545MB mysql latest db2b37ec6181 2 weeks ago 545MB
2. MySQL Docker 컨테이너 생성 및 실행
$ docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=<password> -d -p 3306:3306 mysql:latest
3. Docker 컨테이너 리스트 출력
$ docker ps -a
4. MySQL Docker 컨테이너 시작/중지/재시작
# MySQL Docker 컨테이너 중지 $ docker stop mysql-container # MySQL Docker 컨테이너 시작 $ docker start mysql-container # MySQL Docker 컨테이너 재시작 $ docker restart mysql-container
5. MySQL Docker 컨테이너 접속
$ docker exec -it mysql-container bash root@dc557b92f573:/# 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.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> show databases; | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
6. MySQL 유저 생성
# Shell mysql -u root -p

이어서 MySQL 사용자를 생성해주면 된다.
# MySQL 접근 후 CREATE USER '아이디'@'%' IDENTIFIED BY '비밀번호';
host 를 '%' 로 주면 모든 외부 IP에서 접속할 수 있다.
특정 IP 대역에서만 접속하게 설정하려면 'IP.%' 로 주면 된다.
'172.168.%' -> IP 가 172.168.xxx.xxx 에서만 접속
7. DB 권한 부여
# DB 권한 부여 GRANT ALL PRIVILEGES ON {DB_NAME}.* TO {아이디}@localhost;
- PRIVILEGES ON * : 모든 DB에 권한을 준다
- PRIVILEGES ON {DB_NAME}.* : 특정 DB에만 권한 부여
- GRANT SELECT, INSERT, UPDATE ON ON : 특정 DB에 대한 SELECT, INSERT, UPDATE 권한 선택적으로 부여
# 최종 권한 적용시키기 FLUSH PRIVILEGES;
8. 유저 삭제하기
DROP USER {USER_NAME}@{SERVER_URL}; # DROP USER yj_park@localhost;
9. 외부 접속 확인

따란~
Reference
블로그의 정보
개발하는만두
youngjun._.