Help us understand the problem. What is going on with this article?

最速で日本語環境のMySQL Dockerコンテナを建てる方法

More than 1 year has passed since last update.

はじめに

MySQLの公式Dockerイメージは文字コードがlatin1だったり、タイムゾーンがTSだったり、localeがPOSIXだったりと、日本語環境で使うにはかなり不自由な感じです。
以下のdocker-compose.ymlを使うことで日本語環境をすぐに建てることが可能です。
最速で日本語環境を使いたい人はこちらをベースにしてみてください。

ファイル

以下のような構成を想定しています。

C:.
├─Dockerfile-MySQL
├─docker-compose.yml
└─db
Dockerfile-MySQL
# Dockerfile_MySQL
FROM mysql:5.6

# Set debian default locale to ja_JP.UTF-8
RUN apt-get update && \
    apt-get install -y locales && \
    rm -rf /var/lib/apt/lists/* && \
    echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \
    locale-gen ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8

# Set MySQL character
RUN { \
    echo '[mysqld]'; \
    echo 'character-set-server=utf8mb4'; \
    echo 'collation-server=utf8mb4_general_ci'; \
    echo '[client]'; \
    echo 'default-character-set=utf8mb4'; \
} > /etc/mysql/conf.d/charset.cnf
docker-compose.yml
mysql:
  build: .
  dockerfile: Dockerfile-MySQL
  environment:
    MYSQL_ROOT_PASSWORD: root         #rootユーザ用のパスワード
    MYSQL_USER: user1                 #作業ユーザ名
    MYSQL_PASSWORD: user1password     #作業ユーザ用のパスワード
    MYSQL_DATABASE: development_db    #作成するDB名
    TZ: "Asia/Tokyo"
  ports:
    - 3306:3306
  volumes:
    - ./db:/var/lib/mysql

起動方法

docker-compose.ymlを置いたカレントディレクトリでビルドして、起動します。

> docker-compose build
〜(省略)〜
Successfully built ba1d6a842af7

> docker-compose up -d
Creating mysql_1

>docker-compose ps
     Name                   Command               State           Ports
--------------------------------------------------------------------------------
mysql_1   docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp

日本語環境のMySQLコンテナが出来上がりです。

お好みで

docker-compose.ymlに記載しているMYSQL_ROOT_PASSWORD,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DATABASEは使いたい内容に書き換えてみてください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした