今日はなにの日。

気になったこと勉強になったことのメモ。

今日は、MySQLのマイナーバージョンアップのコンテナを準備の日。

目次

とある日

MySQLのマイナーバージョンアップ(8.0.26)が来ました。

マイナーバージョンアップが来るたびに、環境をどうしようかと毎回悩んでいます。

そこでdocker-composeを使用して、コード化すれば毎回コンテナを新しく起動すれば、バージョンの異なる環境がすぐに構築できると考えました。

はじめに

  1. docker-composeをインストール
  2. docker-compose.ymlでMySQLコンテナ指定
  3. コンテナ起動
  4. おまけ

上記の順番で進めていこうかなと思います。

おまけは、MySQLコマンドを実行して接続をシェルスクリプトにするだけです。

他にも、構築中に起きたエラーについても少しだけまとめてます。

Docker

dockerは、WSL2にすでにインストール済み。

PS C:\Users\xxx> docker version
Client:
 Cloud integration: 1.0.17
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.16.4
 Git commit:        f0df350
 Built:             Wed Jun  2 12:00:56 2021
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:58 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker-compose install

qiita.com

上記の記事を参考にしてインストールしました。

docs.docker.jp

インストールコマンドは、公式のものを参考にしました。

Invoke-WebRequest "https://github.com/docker/compose/releases/download/$dockerComposeVersion/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\docker\docker-compose.exe
PS C:\Users\xxx>docker-compose version
Docker Compose version v2.0.0-beta.6

beta版ですが、特に問題ないと判断してインストールしました。

MySQL docker-compose.yml

問題なくインストールが行えたので、yamlを記述していきます。

Directory

 C:\Users\xxx\Documents\GitHub\Docker\Docker-compose\MySQL のディレクトリ

2021/08/01  18:47    <DIR>          .
2021/08/01  18:47    <DIR>          ..
2021/08/01  18:40               507 docker-compose.yml

docker-compose.yml

imageのバージョンを変えれば8.0.xx任意のバージョンの環境を自由に作ることが可能。

container_nameも重複しないように、変更する必要あり。

vloumesmy.cnfmylogin.shを追加。

version: "3"

services:
  db:
    image: mysql:8.0.26
    container_name: mysql_ver_8.0.26
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_database
      MYSQL_USER: docker
      MYSQL_PASSWORD: docker
      TZ: "Asia/Tokyo"
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - 3306:3306
    volumes:
      - ../../MySQL/settings/my.cnf:/etc/mysql/conf.d/my.cnf
      - ../../MySQL/settings/mylogin.sh:/root/mylogin.sh

my.cnf

my.cnfも毎回変更する必要がないようにdocker-composeで指定して追加します。

# MySQLサーバーへの設定
[mysqld]
# 文字コード/照合順序の設定
character-set-server = utf8mb4
collation-server = utf8mb4_bin

# タイムゾーンの設定
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# デフォルト認証プラグインの設定
default-authentication-plugin = mysql_native_password

# スロークエリログの設定
slow_query_log = 1
long_query_time = 5.0
log_queries_not_using_indexes = 0

# 実行ログの設定
general_log = 1

# mysqlオプションの設定
[mysql]
# 文字コードの設定
default-character-set = utf8mb4

# mysqlクライアントツールの設定
[client]
# 文字コードの設定
default-character-set = utf8mb4

mylogin.sh

mysql ログインするためのシェルスクリプトファイルです。

毎回コマンドうつのめんどくさいので。

mysql -uroot -proot

使用例

root@e9d41c625ee1:/# pwd 
/
root@e9d41c625ee1:/# cd root/
root@e9d41c625ee1:~# ll
bash: ll: command not found
root@e9d41c625ee1:~# ls -l
total 0
-rwxrwxrwx 1 root root 19 Aug  1 19:47 mylogin.sh
root@e9d41c625ee1:~# pwd
/root
root@e9d41c625ee1:~# ./mylogin.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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>

command

上記の環境が構築できたら、下記のコマンドを実行してコンテナを立ち上げる。

  • コンテナ起動
    • docker-compose up -d
  • コンテナ起動確認
    • docker-compose ps
  • コンテナ内部でbash起動
    • docker-compose exec db bash
  • MySQLログイン
    • cd /root
    • ./mylogin.sh

コンテナでbashで接続せずに、そのままMySQLに接続してもいいのですが、そうなるとMySQLのコマンド実行することになり、パスワードとか覚えておかないといけないので...。

これで、MySQL8.0.26を使う環境がすぐに整いました。

今後新しいバージョンが来たら、バージョン指定を変えるだけで新しいバージョンのコンテナが起動します。

コンテナは楽でいいですね。

構築中のエラー

mysql: [Warning] World-writable config file '/etc/mysql/conf.d/my.cnf' is ignored.

mysql: [Warning] World-writable config file '/etc/mysql/conf.d/my.cnf' is ignored.

はじめdockerfileを使用していたので、dockerfileでVolumeを指定するとパーミッションの問題が発生するらしいです。

そもそも、dockerfile必要なのかを考えた結果不必要な判断になったので、dockerfileを削除して問題解決。

[ERROR] [MY-010187] [Server] Could not open file '/var/log/mysql/mysql-error.log' for error logging: No such file or directory

C:\Users\xxx\Documents\GitHub\Docker\Docker-compose\MySQL>docker-compose logs 
mysql_host  | 2021-08-01 18:40:46+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
mysql_host  | 2021-08-01 18:40:46+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_host  | 2021-08-01 18:40:46+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
mysql_host  | 2021-08-01 18:40:46+09:00 [Note] [Entrypoint]: Initializing database files
mysql_host  | 2021-08-01T18:40:46.450828+09:00 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 
8.0.26) initializing of server in progress as process 43
mysql_host  | 2021-08-01T18:40:46.452107+09:00 0 [ERROR] [MY-010187] [Server] Could not open file '/var/log/mysql/mysql-error.log' for error logging: No such file or directory
mysql_host  | 2021-08-01T18:40:46.452248+09:00 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
mysql_host  | 2021-08-01T18:40:46.452270+09:00 0 [ERROR] [MY-010119] [Server] Aborting
mysql_host  | 2021-08-01T18:40:46.452652+09:00 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.

my.cnfに、slow_query_log_fileを記載していたので、そのファイルが存在しないためエラーが起きていたので、その記述を削除。

特に、エラークエリ等を管理するようなコンテナ使用はしない予定です。

MySQL 8.0.26のマイナーバージョンアップで、特に気になった機能はありませんでしたが、

GIS機能を久しぶり触ってみようかなと思う今日のこのごろ。