3
0

More than 1 year has passed since last update.

DockerでMariaDBを構築する

Posted at

目的

Dockerを用いてMysqlを構築する時に、毎回webで調べてしまっているので、記事にまとめておこうと思う。

ディレクトリ構成

  • docker-compose.yml
  • db/
    • Dockerfile
    • sql.sql
    • my.cnf

構築

docker-compose.yml

docker-compose.yml
  db:
    container_name: db
    restart: always
    build:
      context: ./db
    environment:
      MARIADB_ROOT_PASSWORD: tmcit
      MARIADB_DATABASE: LESSON
      MARIADB_USER: tmcit
      MARIADB_PASSWORD: tmcit
      TZ: 'Asia/Tokyo'
    volumes:
      - ./db/data:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - 3306:3306

Dockerfile

FROM mariadb:latest
RUN touch /var/log/mysql/mysqld.log

ADD  my.cnf /etc/mysql/conf.d/my.cnf
ADD sql.sql /docker-entrypoint-initdb.d
RUN chmod 644 /etc/mysql/my.cnf

db/my.cnf

db/my.cnf
[mysqld]
# 文字コード、整合順序の設定
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

db/sql.sql

mariadbの起動時にデータベースの中身まで構築したい場合は以下のファイルを作成する。

db/sql.sql
create database if not exists LESSON;
use LESSON;
create table LESSON_2023(LESSON_ID int auto_increment, LESSON_NAME text, ACCEPTED_GRADE int, PRIMARY KEY (LESSON_ID));
insert into LESSON_2023(LESSON_NAME,ACCEPTED_GRADE)VALUES("国語",1);

ログイン

$ docker exec -it bf4 /bin/bash
$ mariadb -u root -h localhost -p
3
0
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
3
0