Docker Compose に mysql を設定

docker というのはやはりとても難解ですが、一度作ってしまえば移植はなんとかなるんでしょう。

docker compose というのがあって、これは docker よりは扱いやすいとのことなので、docker compose に mysql を設定してみました。

各ファイルの配置


docmysql
└── docker-mysql
    ├── docker-compose.yml
    └── mysql
        ├── Dockerfile
        └── my.cnf

各ファイルの内容

docker-compose.yml

version: "3"
services:
  mysql:
    build: ./mysql/     
    volumes:
      - ./mysql/db:/docker-entrypoint-initdb.d
    image: original_mysql_world
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
Dockerfile

FROM mysql
EXPOSE 3306
ADD ./my.cnf /etc/mysql/conf.d/my.cnf
CMD ["mysqld"]
my.cnf

[mysqld]
character-set-server=utf8
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8

ビルドと起動


cd ~/docmysql/docker-mysql

docker-compose build
docker-compose up -d

docker コンテナに入って操作


docker exec -it docker-mysql_mysql_1 bash

パスワードを設定していないので、mysql にそのままログインできます。
パスワードを設定。


mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass';

ネットワーク上のサーバーの mysql にログイン。


root@d56de0b8c986:/# mysql -h 192.168.0.102 -u heno -p
Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.7.37-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

理解できてはいませんが、何だかすごい。