1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Boot で Rest API 構築 その3 - Docker で MySQL 構築

Last updated at Posted at 2019-10-06

Outline

前回確認したエラーの対策として、Docker で MySQL を構築する

※タイトルに Spring Boot とありますが、今回の話には一切出てきません

環境

Docker Desktop for Mac を使用

191006-0001.png

docker のバージョン

$ docker version
Client: Docker Engine - Community
 Version:           19.03.2
 API version:       1.40
 Go version:        go1.12.8
 Git commit:        6a30dfc
 Built:             Thu Aug 29 05:26:49 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.2
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.8
  Git commit:       6a30dfc
  Built:            Thu Aug 29 05:32:21 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

docker-compose のバージョン

$ docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j  20 Nov 2018

構築

docker-compose を使って MySQL 環境を構築する

ディレクトリ構成

docker
├── README.md
├── docker-compose.yml
└── mysql
    ├── Dockerfile
    └── conf.d
        └── my.cnf

Dockerfile

Dockerfile
FROM mysql:5.7

# MySQLの設定
COPY ./conf.d/my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]

conf.d/my.cnf

my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

[client]
default-character-set=utf8mb4

docker-compose.yml

docker-compose.yml
version: '3'

services:
  mysql:
    build: ./mysql
    container_name: rest-api-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: rest_api
      MYSQL_USER: root
      MYSQL_PASSWORD: mysql
      TZ: 'Asia/Tokyo'
    volumes:
      - rest-api-mysql-data:/var/lib/mysql
    ports:
      - 3306:3306

volumes:
  rest-api-mysql-data:
    driver: local

Docker の MySQL 起動

起動コマンド

$ docker-compose up -d

コンテナの確認

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d45e26ad12fc        docker_mysql        "docker-entrypoint.s…"   2 minutes ago       Up 11 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   rest-api-mysql

シェルから Docker の MySQL に接続する

説明を簡略化するためにコマンドで直接パスワードを指定していますが、セキュリティ上良くないのでやめましょう

$ docker exec -it rest-api-mysql mysql -uroot rest_api -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

続きの予告

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?