LoginSignup
0
1

DockerComposeを使用してMySQL環境の作成

Posted at

docker-composeを使用して簡単にMySQL環境を作成します

実装

docker-compose.ymlファイルにmysqlのコンテナを作るよう記述をします。

version: '3'

services:
  mysql:
    image: mysql:8
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_DATABASE: "db_name"
    volumes:
      - ./mysql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql

補足

イメージはMySQLのバージョン8を使用してます。
ポートは3306を使用し、環境はルートユーザーのパスワードとデータベースだけ準備します

MYSQL_ROOT_PASSWORD
This variable is mandatory and specifies the password that will be set for the MySQL root superuser account. In the above example, it was set to my-secret-pw.

MYSQL_DATABASE
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

和訳

MYSQL_ROOT_PASSWORD
この変数は必須で、MySQL root スーパーユーザーアカウントに設定されるパスワードを指定する。上記の例では、my-secret-pw に設定されている。

MYSQL_DATABASE
この変数はオプションで、イメージ起動時に作成されるデータベースの名前を指定できます。ユーザー/パスワードが与えられた場合(以下を参照)、そのユーザーにはこのデータベースへのスーパーユーザー権限(GRANT ALLに対応)が与えられます。

volumesに指定しているファイルはMySQL起動時に自動で実行されるダンプファイルを指定してます。
今回はuserテーブルを作成してます

USE db_name;

CREATE TABLE `user` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `email` VARCHAR(255) NOT NULL,
    `password` VARCHAR(255) NOT NULL,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
);

参考

0
1
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
0
1