클라우드 + DevOps/Docker

Docker 컨테이너 구축 실습으로 docker 명령어 알아보기 (2): MySQL, MariaDB

gamjadori 2024. 2. 7. 11:19
728x90

<MySQL 컨테이너 구축>

  1. 이미지 불러오기
**ubuntu@host1:~/works$ docker image pull mysql:5.7-debian**
5.7-debian: Pulling from library/mysql
3240fe174df9: Pull complete 
ffaa8624d67f: Pull complete 
4d8328f9714e: Pull complete 
5eb1b5f179ac: Pull complete 
656a614f333c: Pull complete 
ad1320c6d0c3: Pull complete 
a28b57b8a949: Pull complete 
6a0cdfac1051: Pull complete 
c540032ab5fd: Pull complete 
2fe13c62198b: Pull complete 
c5129c0e04b1: Pull complete 
Digest: sha256:0821f3a5b3ecda79885d8bd40ec353f3d2cc1bc23586f9c367dfc76493737163
Status: Downloaded newer image for mysql:5.7-debian
docker.io/library/mysql:5.7-debian

 

2. 컨테이너 생성

  • -e: 환경변수 (비밀번호 지정)
**ubuntu@host1:~/works$ docker container run -it -e MYSQL_ROOT_PASSWORD=ubuntu(비밀번호 지정) mysql:5.7-debian /bin/bash**
root@d33ef0d2bad2:/# << 접속된 거 확인

 

3. MySQL 시작

  • /etc/init.d/mysql start
**root@d33ef0d2bad2:/# /etc/init.d/mysql start**
su: warning: cannot change directory to /home/mysql: No such file or directory
2024-01-11T02:51:05.106365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-01-11T02:51:05.287185Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-01-11T02:51:05.333053Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-01-11T02:51:05.401641Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4395fb40-b02c-11ee-ae51-0242ac110004.
2024-01-11T02:51:05.404320Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-01-11T02:51:05.766678Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-01-11T02:51:05.766699Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-01-11T02:51:05.767346Z 0 [Warning] CA certificate ca.pem is self signed.
2024-01-11T02:51:05.803058Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
su: warning: cannot change directory to /home/mysql: No such file or directory
..
[info] MySQL Community Server 5.7.42 is started.

 

4. MySQL 접속

**root@d33ef0d2bad2:/# mysql -u root -p**
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.42 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> show databases; // 기능 확인하기**
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

 

<MariaDB 컨테이너 구축>

<컨테이너 구축>

  • image pull 하지 않는 이유: 네트워크 서버 상태가 좋으면 하지 않아도 됨
ubuntu@host1:~/works$ docker container run --name mariadb -e MYSQL_ROOT_PASSWORD=ubuntu -e MARIADB_DATABASE=itme -d -p 3306:3306 mariadb:10.2

<상태 확인>

  • STATUS: Exitee (컨테이너 생성 시 명령어 입력하지 않아서 생성되자마자 꺼짐)

 

<컨테이너 활성화>

  • docker container exec
ubuntu@host1:~/works$ docker container exec -it mariadb /bin/bash