1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeで起動してるMySQLがうまく終了しなくなってたのを直した

Last updated at Posted at 2020-03-29

概要

私が頻繁に使っているローカル開発環境用の docker-compose.yml に含まれるMySQLが気づいたらちゃんと終了しなくなっていて、たまにデータをぶっ壊していたから直しました。

原因

mysqlコンテナを command: bash-c 'ほげほげ' で起動していたら、PID1で起動するもんで、終了時のkillをうまくハンドリングできずに、mysqlのプロセスまでkillが届かず落ちなくて、タイムアウトでホストから落とされていた(データ壊す危険)。

docker-compose.ymlにやったこと

initオプションを指定して /dev/ihit 越しに起動してあげればこいつがちゃんとハンドリングしてくれます。
3.5だったので、3.7へバージョン上げて、下記のように init: true を指定しました。

version: '3.7'
services:
  mysql:
    image: mysql:5.7
    init: true
    command: bash -c '
      touch /var/log/mysql/general.log &&
      chown mysql:mysql /var/log/mysql/general.log &&
      tail -f /var/log/mysql/general.log &
      /entrypoint.sh mysqld
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --general-log=true
      --general-log-file=/var/log/mysql/general.log
      --sql-mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
      --innodb-use-native-aio=0'

before

指定していなかった時代

root@4b13f5625011:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 05:53 ?        00:00:00 bash -c  touch /var/log/mysql/general.log && chown mysql:mysql /var/log/mysql/general.log && tail -f /var/log/mysql/general.log & /entrypoint.sh mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql
root         7     1  0 05:53 ?        00:00:00 bash -c  touch /var/log/mysql/general.log && chown mysql:mysql /var/log/mysql/general.log && tail -f /var/log/mysql/general.log & /entrypoint.sh mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql
mysql        8     1  5 05:53 ?        00:00:00 mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql/general.log --sql-mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --innodb-use-native-aio=0
root        14     7  0 05:53 ?        00:00:00 tail -f /var/log/mysql/general.log
root        65     0  0 05:53 pts/0    00:00:00 bash
root        71    65  0 05:53 pts/0    00:00:00 ps -ef

after

指定した時代

root@8771d0333475:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 06:00 ?        00:00:00 /sbin/docker-init -- docker-entrypoint.sh bash -c  touch /var/log/mysql/general.log && chown mysql:mysql /var/log/mysql/general.log && tail -f /var/log/mysql/general.log & /entrypoint.sh mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-
root         7     1  0 06:00 ?        00:00:00 bash -c  touch /var/log/mysql/general.log && chown mysql:mysql /var/log/mysql/general.log && tail -f /var/log/mysql/general.log & /entrypoint.sh mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql
root         9     7  0 06:00 ?        00:00:00 bash -c  touch /var/log/mysql/general.log && chown mysql:mysql /var/log/mysql/general.log && tail -f /var/log/mysql/general.log & /entrypoint.sh mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql
mysql       10     7  2 06:00 ?        00:00:00 mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --general-log=true --general-log-file=/var/log/mysql/general.log --sql-mode=ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --innodb-use-native-aio=0
root        13     9  0 06:00 ?        00:00:00 tail -f /var/log/mysql/general.log
root        67     0  1 06:00 pts/0    00:00:00 bash
root        73    67  0 06:00 pts/0    00:00:00 ps -ef

結果

こうするとホストから Ctrl + Cを叩き込んでもタイム・アウトすることなく、素直に落ちてくれるようになりました。

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?