728x90
반응형
docker-compose를 사용해서 Mysql을 설치 진행을 해보겠습니다.
파일 구조는 아래와 같습니다.
파일구조
$ tree db
db root directory
|-- docker-compose.yml // mysql 빌드 파일
`-- env.mysql // 환경 파일
mysql 환경 파일 (env.mysql)
MYSQL_DATABASE=mysql8
MYSQL_ROOT_PASSWORD=admin
MYSQL_USER=testuser
MYSQL_PASSWORD=userpasswd
mysql compose 파일(docker-compose.yml)
version: '3.8'
services:
mysql_db: ## Service Name
container_name: mysql-8 ## Container Name
image: mysql:8.0
env_file:
- ./env.mysql
ports:
- 23306:3306
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
volumes:
- ./mysql_data:/var/lib/mysql
mysql 설치 (docker-compose up -d)
$ docker-compose up -d --build
Creating network "db_default" with the default driver
Pulling mysql_db (mysql:8.0)...
8.0: Pulling from library/mysql
f003217c5aae: Pull complete
65d94f01a09f: Pull complete
43d78aaa6078: Pull complete
a0f91ffbdf69: Pull complete
59ee9e07e12f: Pull complete
04d82978082c: Pull complete
70f46ebb971a: Pull complete
db6ea71d471d: Pull complete
c2920c795b25: Pull complete
26c3bdf75ff5: Pull complete
9ec1f1f78b0e: Pull complete
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195
Status: Downloaded newer image for mysql:8.0
Creating mysql-8 ... done
Creating network "db_default" with the default driver
Pulling mysql_db (mysql:8.0)...
8.0: Pulling from library/mysql
f003217c5aae: Pull complete
65d94f01a09f: Pull complete
43d78aaa6078: Pull complete
a0f91ffbdf69: Pull complete
59ee9e07e12f: Pull complete
04d82978082c: Pull complete
70f46ebb971a: Pull complete
db6ea71d471d: Pull complete
c2920c795b25: Pull complete
26c3bdf75ff5: Pull complete
9ec1f1f78b0e: Pull complete
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195
Status: Downloaded newer image for mysql:8.0
Creating mysql-8 ... done
mysql 상태 확인
- docker image ls 명령을 통해 mysql 이미지가 생성된 것을 확인할 수 있습니다.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 667ee8fb158e 37 hours ago 521MB
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 667ee8fb158e 37 hours ago 521MB
- docker-compose ps 명령을 통해 컨테이너 상태를 확인할 수 있습니다.
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------
mysql-8 docker-entrypoint.sh --cha ... Up 0.0.0.0:23306->3306/tcp,:::23306->3306/tcp, 33060/tcp
Name Command State Ports
--------------------------------------------------------------------------------------------------------
mysql-8 docker-entrypoint.sh --cha ... Up 0.0.0.0:23306->3306/tcp,:::23306->3306/tcp, 33060/tcp
mysql Data file 확인
도커 설치 후 mysql_data 디렉터리가 생성이 되었고 해당 디렉터리에 데이터 파일이 생성됩니다.
docker-compose 파일에 volume 설정을 하면 호스트 디렉터리에 mysql_data 디렉터리가 없더라도 빌드 과정에서
자동으로 생성 되게 됩니다.
volumes:
- ./mysql_data:/var/lib/mysql => [host volume]:[container volume]
$ tree db
db
|-- docker-compose.yml
|-- env.mysql
`-- mysql_data
|-- #ib_16384_0.dblwr
|-- #ib_16384_1.dblwr
|-- #innodb_temp [error opening dir]
|-- auto.cnf
|-- binlog.000001
|-- binlog.000002
|-- binlog.000003
|-- binlog.index
|-- ca-key.pem
|-- ca.pem
|-- client-cert.pem
|-- client-key.pem
|-- ib_buffer_pool
|-- ib_logfile0
|-- ib_logfile1
|-- ibdata1
|-- ibtmp1
|-- mysql [error opening dir]
|-- mysql.ibd
|-- mysql8 [error opening dir]
|-- performance_schema [error opening dir]
|-- private_key.pem
|-- public_key.pem
|-- server-cert.pem
|-- server-key.pem
|-- sys [error opening dir]
|-- undo_001
`-- undo_002
db
|-- docker-compose.yml
|-- env.mysql
`-- mysql_data
|-- #ib_16384_0.dblwr
|-- #ib_16384_1.dblwr
|-- #innodb_temp [error opening dir]
|-- auto.cnf
|-- binlog.000001
|-- binlog.000002
|-- binlog.000003
|-- binlog.index
|-- ca-key.pem
|-- ca.pem
|-- client-cert.pem
|-- client-key.pem
|-- ib_buffer_pool
|-- ib_logfile0
|-- ib_logfile1
|-- ibdata1
|-- ibtmp1
|-- mysql [error opening dir]
|-- mysql.ibd
|-- mysql8 [error opening dir]
|-- performance_schema [error opening dir]
|-- private_key.pem
|-- public_key.pem
|-- server-cert.pem
|-- server-key.pem
|-- sys [error opening dir]
|-- undo_001
`-- undo_002
mysql 컨테이너 접속 및 실습
- docker-compose exec mysql_db /bin/bash 명령을 통해 컨테이너에 접속을 합니다.
- mysql -u testuser -p --host 127.0.0.1 접속 명령을 통해 mysql에 접속합니다.
- MYSQL_USER=testuser
- MYSQL_PASSWORD=userpasswd
- show database 명령을 통해 접속 db를 확인합니다.
- MYSQL_DATABASE=mysql8
- t_board 테이블을 생성합니다.
- show tables 명령을 통해 생성된 테이블을 확인합니다.
- t_board 테이블에 데이터를 입력합니다.
- 입력한 데이터를 조회합니다.
Sample data
CREATE TABLE t_board (
board_idx INT(11) NOT NULL AUTO_INCREMENT COMMENT ' ',
title VARCHAR(300) NOT NULL COMMENT '',
contents TEXT NOT NULL COMMENT '',
hit_cnt SMALLINT(100) NOT NULL DEFAULT '0' COMMENT '',
created_datetime DATETIME NOT NULL COMMENT '',
creator_id VARCHAR(50) NOT NULL COMMENT '',
updated_datetime DATETIME DEFAULT NULL COMMENT '',
updater_id VARCHAR(50) DEFAULT NULL COMMENT '',
deleted_yn CHAR(1) NOT NULL DEFAULT 'N' COMMENT ' ',
PRIMARY KEY (board_idx)
);
insert into t_board(board_idx,title, contents, hit_cnt,created_datetime, creator_id, deleted_yn)
values(1,'제목','내용 작성',1,SYSDATE(),'tester','N');
select * from t_board;
- 테스트 테스트 진행
$ docker-compose exec mysql_db /bin/bash
root@24a15adb0cb3:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@24a15adb0cb3:/var/lib/mysql# mysql -u testuser -p --host 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql8 |
+--------------------+
2 rows in set (0.01 sec)
mysql> use mysql8
Database changed
mysql> CREATE TABLE t_board (
-> board_idx INT(11) NOT NULL AUTO_INCREMENT COMMENT ' ',
-> title VARCHAR(300) NOT NULL COMMENT '',
-> contents TEXT NOT NULL COMMENT '',
-> hit_cnt SMALLINT(100) NOT NULL DEFAULT '0' COMMENT '',
-> created_datetime DATETIME NOT NULL COMMENT '',
-> creator_id VARCHAR(50) NOT NULL COMMENT '',
-> updated_datetime DATETIME DEFAULT NULL COMMENT '',
-> updater_id VARCHAR(50) DEFAULT NULL COMMENT '',
-> deleted_yn CHAR(1) NOT NULL DEFAULT 'N' COMMENT ' ',
-> PRIMARY KEY (board_idx)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> show tables;
+------------------+
| Tables_in_mysql8 |
+------------------+
| t_board |
+------------------+
1 row in set (0.01 sec)
mysql> insert into t_board(board_idx,title, contents, hit_cnt,created_datetime, creator_id, deleted_yn)
-> values(1,'',' ',1,SYSDATE(),'tester','N');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_board;
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
| board_idx | title | contents | hit_cnt | created_datetime | creator_id | updated_datetime | updater_id | deleted_yn |
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
| 1 | | | 1 | 2022-03-31 07:34:14 | tester | NULL | NULL | N |
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
1 row in set (0.00 sec)
root@24a15adb0cb3:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@24a15adb0cb3:/var/lib/mysql# mysql -u testuser -p --host 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql8 |
+--------------------+
2 rows in set (0.01 sec)
mysql> use mysql8
Database changed
mysql> CREATE TABLE t_board (
-> board_idx INT(11) NOT NULL AUTO_INCREMENT COMMENT ' ',
-> title VARCHAR(300) NOT NULL COMMENT '',
-> contents TEXT NOT NULL COMMENT '',
-> hit_cnt SMALLINT(100) NOT NULL DEFAULT '0' COMMENT '',
-> created_datetime DATETIME NOT NULL COMMENT '',
-> creator_id VARCHAR(50) NOT NULL COMMENT '',
-> updated_datetime DATETIME DEFAULT NULL COMMENT '',
-> updater_id VARCHAR(50) DEFAULT NULL COMMENT '',
-> deleted_yn CHAR(1) NOT NULL DEFAULT 'N' COMMENT ' ',
-> PRIMARY KEY (board_idx)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> show tables;
+------------------+
| Tables_in_mysql8 |
+------------------+
| t_board |
+------------------+
1 row in set (0.01 sec)
mysql> insert into t_board(board_idx,title, contents, hit_cnt,created_datetime, creator_id, deleted_yn)
-> values(1,'',' ',1,SYSDATE(),'tester','N');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_board;
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
| board_idx | title | contents | hit_cnt | created_datetime | creator_id | updated_datetime | updater_id | deleted_yn |
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
| 1 | | | 1 | 2022-03-31 07:34:14 | tester | NULL | NULL | N |
+-----------+-------+----------+---------+---------------------+------------+------------------+------------+------------+
1 row in set (0.00 sec)
728x90
반응형
'02.Docker > docker-compose' 카테고리의 다른 글
[docker-compose] docker-compose 를 이용한 이미지 빌드 (0) | 2022.03.31 |
---|---|
docker-compose 를 이용한 Oracle 19c 설치 (0) | 2022.03.22 |
Docker-compose 설치 (0) | 2022.03.21 |