[Docker] MySQL 설치하는 방법.(이미지 다운로드, 컨테이너 생성/실행, 원격에서 MySQL 서버로 접속 …)
2024. 1. 17. 17:37ㆍTool/docker
728x90
728x90
Docker Server 환경 정보.
$ docker -v
Docker version 20.10.10, build b485636
$ docker info | grep "Docker Root Dir"
Docker Root Dir: /var/lib/docker
Docker MySQL8 이미지 다운로드.
$ docker pull mysql:8.0
8.0: Pulling from library/mysql
bce031bc522d: Pull complete
9d95fc125cf8: Pull complete
22412d6d690f: Pull complete
79d453aad0eb: Pull complete
1b6cca7a391d: Pull complete
f9e7ae943297: Pull complete
af3e28b094a2: Pull complete
db237bdcf1c0: Pull complete
4b3c3f5cb4f4: Pull complete
394dbd7e05eb: Pull complete
18a167e598b0: Pull complete
Digest: sha256:82b40099c780c15850690bea3a76c73fcc4834ce813313cd048468f1de7c2b06
Status: Downloaded newer image for mysql:8.0
docker.io/library/mysql:8.0
Docker Server(Local Server) 볼륨 생성.
$ docker volume create mysql8
볼륨이 생성되었는지 확인.
$ ll /var/lib/docker/volumes | grep mysql8
drwx-----x. 3 root root 18 1월 17 14:09 mysql8
MySQL 컨테이너 생성/실행.
- --default-authentication-plugin=mysql_native_password 옵션을 설정하는 이유는 링크를 참조한다.
# docker run -d --name [Container Name] -v [Local Dir]:[Container Dir] -e MYSQL_ROOT_PASSWORD=[PASSWD] -p [Local Port]:[Container Port] [Image]
$ docker run -d --name mysql8 -v mysql8:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=[PASSWD] -p 3305:3306 mysql:8.0 --default-authentication-plugin=mysql_native_password
199551da9fa6c070f89a63ed39afd73c28da62546cfb09bb040cf4ccb8f3c035
MySQL 컨테이너가 실행되었는지 확인.
$ docker ps | grep mysql8
199551da9fa6 mysql:8.0 "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3305->3306/tcp, :::3305->3306/tcp mysql8
MySQL 컨테이너로 접속.
$ docker exec -it mysql8 bash
MySQL CLI 접속.
$ mysql -uroot -p[PASSWD]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35 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.
mysql>
MySQL 데이터베이스 생성.
mysql> CREATE DATABASE mysql8db_gomu92;
Query OK, 1 row affected (0.18 sec)
MySQL 데이터베이스 조회.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysql8db_gomu92 |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.04 sec)
MySQL 계정 생성.
mysql> CREATE USER 'gomu92'@'%' IDENTIFIED BY [PASSWD];
Query OK, 0 rows affected (0.09 sec)
생성한 사용자에게 데이터베이스의 접근 권한을 부여.
mysql> GRANT ALL PRIVILEGES ON mysql8db_gomu92.* TO 'gomu92'@'%';
Query OK, 0 rows affected (0.08 sec)
부여한 권한을 적용.
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
원격 서버에서 Dbeaver를 사용하여, MySQL 서버로 접속.
DBeaver ?
- 다중 플랫폼 데이터베이스 관리 도구.
새로운 데이터베이스 연결 생성.
연결할 데이터베이스 플랫폼 설정.
위에서 생성한 Database, User 로 접속되는 것을 확인.
원격 서버에서 MySQL CLI를 사용하여, MySQL 서버로 접속.
$ mysql -V
mysql Ver 14.14 Distrib 5.7.13, for Linux (x86_64) using EditLine wrapper
$ mysql -h [Host Adress] -P 3305 -u gomu92 -p[PASSWD]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2016, 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> use mysql8db_gomu92
Database changed
mysql>
728x90
728x90