Docker

[Docker] MySQLコンテナを5.5から5.6にバージョンアップしたら起動しなくなったので解決方法

今まで古いサーバーと同じ環境をと思ってMySQLの5.5を使っていましたが、5.6からはDAATETIMEにデフォルトでCURRENT_TIMESTAMPが使えるらしいのでバージョンアップしたら、起動しなくなったので、対処方法をまとめました。

更新日: 2018.7.17公開日: 2018.2.7

今まで古いサーバーと同じ環境をと思ってMySQLの5.5を使っていましたが、5.6からはDAATETIMEにデフォルトでCURRENT_TIMESTAMPが使えるらしいのでバージョンアップしたら、起動しなくなったので、対処方法をまとめました。

環境と現象

環境

  • OS: Core OS on Vagrant
  • Docker: Docker version 18.01.0-ce, build 03596f5

現象

使用するDockerイメージを5.5から5.6にアップデートして、コンテナを起動すると、いきなりコンテナがシャットダウンする現象が起きました。Docker logsでログを追ってみると、、

2018-02-07 09:33:23 16 [Note] InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
2018-02-07 09:33:23 16 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2018-02-07 09:33:23 16 [ERROR] Plugin 'InnoDB' init function returned error.
2018-02-07 09:33:23 16 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-02-07 09:33:23 16 [ERROR] Unknown/unsupported storage engine: InnoDB
2018-02-07 09:33:23 16 [ERROR] Aborting

メモリ周りで落ちているようです。

原因

調べてみると、どうやらSWAPがないことが問題の模様。

AmazonAWSで、MySQL がよく落ちる? そんな時はSWAP領域をチェック!

解決方法

my.cnfあたりでMySQLに割り当てるメモリを減らしたり、Core OSのメモリを増やしたりと他の方法もあるのですが、上記のページのと同じく一旦スワップを設定してしのぎます。

CoreOSにスワップを設定

基本的には、CoreOSの公式ドキュメントのままです。

Creating a swapfile
The following commands, run as root, will make a 1GiB file suitable for use as swap.

mkdir -p /var/vm
fallocate -l 1024m /var/vm/swapfile1
chmod 600 /var/vm/swapfile1
mkswap /var/vm/swapfile1
Creating the systemd unit file
The following systemd unit activates the swapfile we created. It should be written to /etc/systemd/system/var-vm-> swapfile1.swap.

Unit
Description=Turn on swap

Swap
What=/var/vm/swapfile1

Install
WantedBy=multi-user.target
Enable the unit and start using swap
Use systemctl to enable the unit once created. The swappiness value may be modified if desired.

$ systemctl enable –now var-vm-swapfile1.swap
Optionally
$ echo ‘vm.swappiness=10’ | sudo tee /etc/sysctl.d/80-swappiness.conf
$ systemctl restart systemd-sysctl
Swap has been enabled and will be started automatically on subsequent reboots. We can verify that the swap is activated by running swapon:

$ swapon
NAME TYPE SIZE USED PRIO
/var/vm/swapfile1 file 1024M 0B -1

あとは、freeコマンドでスワップが割り当てられてればOK。

MySQL5.6コンテナを再起動させると無事起動しました! ただし、MySQL5.5から5.6は非互換な部分があるので調整をすればOKです。


今回が応急処置的な対処方法で解決しましたが、コンテナ周りの設定やMySQLのメモリ管理などはパフォーマンスに直結するので、今度しっかりと設定を見直そうと思います。