概要
Docker環境にMySQLを構築し、SQLデータを流し込む
前提
Dockerがインストールされている状態
ディレクトリ構成
db_sample/
├ docker/
| └ mysql/
| ├ conf.d/
| | └ my.cnf
| ├ initdb.d/
| └ xxxxxx.sql
| └ Dockerfile
└ docker-compose.yml
ファイルの準備
docker-compose.yml作成
db_sample/docker-compose.yml
version: '3.3'
services:
db:
build: ./docker/mysql # Dockerfileの置き場所を指定
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: sample_db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
ports:
- "3314:3306"
volumes:
- ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d
- ./docker/mysql/conf.d:/etc/mysql/conf.d
- ./log/mysql:/var/log/mysql
Dockerfile作成
db_sample/docker/mysql/Dockerfile
FROM mysql:5.7
RUN touch /var/log/mysql/mysqld.log
my.conf作成
db_sample/docker/mysql/conf.d/my.conf
[mysqld]
character-set-server=utf8
explicit-defaults-for-timestamp=1
general-log=1
general-log-file=/var/log/mysql/mysqld.log
[client]
default-character-set=utf8
SQLファイルの配置
db_sample/docker/mysql/initdb.d/配下に置く
Dockerコマンド実行
db_sampleディレクトリの配下に移動
$ cd /../db_sample
docker-compose up -d
の実行
$ docker-compose up -d
Creating network "db_sample_default" with the default driver
Building db
Step 1/2 : FROM mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Pull complete
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
979b389fc71f: Pull complete
403f729b1bad: Pull complete
Digest: sha256:d4ca82cee68dce98aa72a1c48b5ef5ce9f1538265831132187871b78e768aed1
Status: Downloaded newer image for mysql:5.7
---> 697daaecf703
Step 2/2 : RUN touch /var/log/mysql/mysqld.log # 指定の場所にログを記録するファイルを作る
---> Running in 7fe949f8a39f
Removing intermediate container 7fe949f8a39f
---> f6323d72a74c
:
:
Successfully built f6323d72a74c
Successfully tagged mysql:5.7
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating db_sample_db_1 ... done
Creating db_sample_db_1 ... doneが表示されたら、作成完了
コンテナ起動確認
docker-compose ps
の実行
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------
db_sample_db_1 docker-entrypoint.sh Up 0.0.0.0:3314->3306/t
mysqld cp, 33060/tcp
コンテナ内のDB接続
コンテナ内にアクセス
$ docker exec -it db_sample_db_1 bash
root@55c55b394991:/#
MySQL内に入る
パスワードはdocker-compose.ymlのMYSQL_PASSWORDを指定
root@55c55b394991:/# mysql -u user -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32-log 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>
MySQLでDB選択とテーブル確認
使用するDBにsample_dbを選択後、テーブル一覧を表示する
mysql> use sample_db
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
# テーブル表示
mysql> show tables;
+------------------------------------------+
| Tables_in_sample_db |
+------------------------------------------+
| table_01 |
| table_02 |
: |
: |
| table_nn |
+------------------------------------------+
nn rows in set (0.01 sec)
データ確認
mysql> select * from table_01;
# table_01のリストが表示される
環境削除
コンテナの停止とネットワークの削除
$ docker-compose down
Stopping db_sample_db_1 ... done
Removing db_sample_db_1 ... done
Removing network db_sample_default
$
dockerイメージ削除
tagが5.7のとの2つが作られているのでIMAGE IDを指定して両方削除
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f6323d72a74c 6 hours ago 448MB
mysql <none> 697daaecf703 6 days ago 448MB
:
:
$ docker rmi f6323d72a74c
Untagged: mysql:5.7
Deleted: sha256:49....
Deleted: sha256:49....
:
$ docker rmi 697daaecf703
Untagged: mysql@sha256:d4...
Deleted: sha256:697d...
Deleted: sha256:521...
さいごに
今回は、Docker環境のコンテナにMySQLを構築し、指定場所にあるSQLファイルを実行してテーブル、データ情報を取り込んでみました。