はじめに
今回も@Sekky0905Ksさんのコミットを遡らせていただき、使われている技術一つ一つをまとめさせていただきました。
サーバーサイドは勉強中の身なので間違い等ありましたらご報告いただけると嬉しいです。それでは始めていきましょう。
Mysqlのセットアップ
Dockerコンテナ上にMysqlをセットアップしていきたいと思います。まず最初にmysql/my.cnf
を作成しましょう。
[mysqld] # Setting for mysql server.
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci # Ignore lower/upper case of alphabet.
[client] # Setting for mysql client tool.
default-character-set=utf8mb4
次に、mysql/init/setup.sql
上に、初期状態のデータベースとテーブルを作成するためのコードを記述します。InnoDBについて興味がある方は知って得するInnoDBセカンダリインデックス活用術!もどうぞ。
USE nuxt_go_template;
/*
Create users table. It has 'id' which has a unique identity,
'name' with the length of 30 characters, 'session' id with the
length of 36 characters, 'password' with the length of 64 characters,
created time and updated time. Primary key is 'id'.
*/
CREATE TABLE IF NOT EXISTS users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
session_id VARCHAR(36) NOT NULL,
password VARCHAR(64) NOT NULL,
created_at DATETIME DEFAULT NULL,
updated_at DATETIME DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*
Create sessions table. It has 'id' with the length
of 36 characters, 'user id', created time, and
updated time. Primary key is 'id'.
*/
CREATE TABLE IF NOT EXISTS sessions (
id VARCHAR(36) NOT NULL,
user_id INT UNSIGNED NOT NULL,
created_at DATETIME DEFAULT NULL,
updated_at DATETIME DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*
Create threads table. It has 'id' which has
a unique identity, 'time' with the length of
20 characters, user id and created time,
updated time. Primary key is 'id'.
*/
CREATE TABLE IF NOT EXISTS threads (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(20) NOT NULL,
user_id INT UNSIGNED NOT NULL,
created_at DATETIME DEFAULT NULL,
updated_at DATETIME DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY (title)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*
Create comments. It has 'id' which has unique
character, thread id, user id, content with the
length of 200 characters, created time, and
updated time. Primary key is 'id'.
*/
CREATE TABLE IF NOT EXISTS comments (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
thread_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
content VARCHAR(200) NOT NULL,
created_at DATETIME DEFAULT NULL,
updated_at DATETIME DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Dockerの設定に関しては、docker-compose.yaml
にnvgdb
イメージを作成していきます。
MySQL8.0より認証方式が変わったので、DockerでMySQL8.0の環境構築 & 認証方式変更を参考にしつつ、パスワードを用いた認証方法に変更すると、sequel-proなどのGUIツールからDockerコンテナ内で起動したMySQLサーバにアクセスすることができます。
volumesの部分で行っている永続化に関してはdocker-compose+MySQL5.7(8.0も)+初期化+永続化を参考にしていただけるとわかりやすいかと思います。
version: '3'
services:
nvgdb:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: "nuxt_go_template"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
volumes:
- "./mysql:/etc/mysql/conf.d"
- "./mysql/data:/var/lib/mysql"
- "./mysql/init:/docker-entrypoint-initdb.d"
container_name: gvdb
ports:
- "3306:3306"
app:
build:
context: ./
dockerfile: docker/Dockerfile
volumes:
- ./:/go/src/github.com/hideUW/nuxt_go_template
command: bash -c 'cd /go/src/github.com/hideUW/nuxt_go_template/server && go run *.go'
ports:
- "8080:8080"
container_name: app
動いているかどうか確認する
docker-compose start
でコンテナを立ち上げ、MySQlサーバーが立ち上がっているか、実際にデータベースが初期化されているかを確認します。
ターミナルからDockerコンテナ内のMySQLサーバにアクセスします。
$ mysql -u root -p -h 127.0.0.1 -P 3306
以上の画像より、無事サーバの立ち上げと初期化が行われていることを確認しました。
参考
@Sekky0905Ksさん - nuxt-vue-go-chat
Chris Chuck - How to Create a MySql Instance with Docker Compose