βοΈ Devops/π³ Docker
[Docker] MySQL μ€μΉνκΈ°
iseunghan
2022. 5. 23. 22:24
λ°μν
docker-compose λ€μ΄λ‘λ
- μλμμ μ΅μ λ²μ νμΈ!
sudo curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- κΆν μ€μ
sudo chmod +x /usr/local/bin/docker-compose
- μ€μΉ νμΈ
docker-compose --version
docker-compose λλ ν 리 μμ±
μ΅μ’ μ μΈ λλ ν 리 ꡬ쑰
docker-mysql
βββ docker-compose.yml
βββ .env
βββ mysql
βββ conf.d
β βββ my.cnf
βββ data
βββ initdb.d
βββ create_table.sql
βββ load_data.sql
4 directories, 4 files
- docker-compose.yml : 컨ν μ΄λ μ€νν λ, λμ± μ½κ² μ€μ νκΈ° μν νμΌ
- conf.d : MySQL μ€μ νμΌμ΄ μμΉ
- data : MySQL λ°μ΄ν°κ° μ μ₯λλ μμΉ
- initdb.d : Docker 컨ν
μ΄λκ° μ΅μ΄ μ€ν μ λΆλ¬μ¬
*.sh
,*.sql
λ± νμΌ μ΄λ¦μ μ¬μ μμλλ‘ μ€νλ¨.
1. μμλ‘ docker-mysql
μ΄λΌλ μμ
λλ ν 리 μμ±
mkdir docker-mysql && cd docker-mysql
2. DB κ³μ μ 보 μμ±
.env
μμ±
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD={root_pw}
MYSQL_DATABASE={dbλͺ
}
MYSQL_USER=user
MYSQL_PASSWORD={user_pw}
3. κ° ν΄λ λ° νμΌ μμ±
mkdir -p mysql/conf.d
mkdir -p mysql/data
mkdir -p mysql/initdb.d
conf.d
μμλ my.cnf
λ₯Ό μλμ κ°μ΄ μμ±ν©λλ€.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init.d
μμλ create.sql
κ³Ό load_data.sql
λ₯Ό μμ±ν©λλ€.
- create.sql (μμ)
CREATE TABLE user( username VARCHAR(25) PRIMARY KEY, password VARCHAR (50) not null );
- load_data.sql (μμ)
INSERT INTO user(username, password) VALUES ('user', 'pass');
4. docker-compose.yml
version: "3"
services:
db:
container_name: docker-mysql
image: mysql
ports:
- 3306:3306
volumes:
- ./mysql/conf.d:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
- ./mysql/initdb.d:/docker-entrypoint-initdb.d
env_file: .env
environment:
TZ: Asia/Seoul
restart: always
# μ νν μμ±λ²μ μλ λ§ν¬ μ°Έμ‘°
# https://docs.docker.com/compose/compose-file/
image
: mysql latest λ²μ μ¬μ© (νΉμ λ²μ μ¬μ©νκ³ μΆμΌλ©΄ → mysql:0.0.1 μ΄λ°μμΌλ‘ μμ±)ports
: 컨ν μ΄λ ν¬νΈ 3306 κ°λ°©volumes
: Docker 컨ν μ΄λμ νμΌμ μ¬μ©μ μμ€ν μ νμΌμ μ°κ²°TZ
: DB Time Zone μ€μ
docker-composeλ‘ μ€ν!
μ€ν
$ docker-compose -f docker-compose.yml up -d
-f
: λ³λμ docker-compose νμΌμ μ¬μ©νκ³ μΆμ λ-d
: λ°±κ·ΈλΌμ΄λλ‘ μ€ν
μ’ λ£
$ docker-compose down
REFERENCE
λ°μν