Nuxt와 Go에서 Software Architecture 실험 ~ 3 일째 MySql 편 ~

12303 단어 MySQLnuxt.js5DDD도커

소개



이번에도 @Sekky0905Ks 씨의 커밋을 거슬러 올라가 사용되고 있는 기술 하나 하나를 정리했습니다.

서버 사이드는 공부중의 몸이므로 실수등 있으면 보고 받을 수 있으면 기쁩니다. 그럼 시작하겠습니다.

Mysql 설정



Docker 컨테이너에 Mysql을 설정하고 싶습니다. 먼저 mysql/my.cnf를 만들어 보겠습니다.

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 보조 인덱스 활용술! 도 부디.

mysql/init/setup.sql
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.yamlnvgdb 이미지를 작성해 갑니다.
MySQL8.0 보다 인증 방식이 바뀌었으므로, Docker로 MySQL8.0의 환경 구축 및 인증 방식 변경 수 있습니다.

volumes 부분에서 하고 있는 영속화에 관해서는 docker-compose + MySQL5.7 (8.0도) + 초기화 + 영속화 를 참고해 주실 수 있으면 알기 쉬울까 생각합니다.

docker-compose.yaml
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

좋은 웹페이지 즐겨찾기