6
8

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 3 years have passed since last update.

MySQLのコンテナを作成したのに再起動し続けるときの対応方法

Posted at
  • 環境
    • CentOS Linux release 7.8.2003 (Core)
    • 使っているMySQLのイメージ : MySQL8.0.20

事象 : MySQLのコンテナを作成したのに再起動し続ける

docker-compose.yml
version: '3.8'
services:
  other-service:
...省略...
  mysql80:
    image: mysql:8.0.20
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
    environment:
      MYSQL_DATABASE: sampledb
      MYSQL_USER: ponsuke
      MYSQL_PASSWORD: ponsuke
      MYSQL_TCP_PORT: 3306
    ports:
...省略...
# MySQLのコンテナを作成したのに(作成されるのが異様に早かった)
$ docker-compose up -d --no-recreate --build
...省略...
Status: Downloaded newer image for mysql:8.0.20
Starting other-service ... done
Creating mysql80   ... done

# なぜか、再起動し続ける
$ docker ps -s
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                          PORTS                  NAMES       SIZE
123456789012        mysql:8.0.20         "docker-entrypoint.s…"  33 seconds ago      Restarting (1) 10 seconds ago                          mysql80     0B (virtual 541MB)
abcdefghijkl        host_other-service   "/usr/sbin/init"         32 hours ago        Up 7 minutes                    0.0.0.0:3389->3389/tcp other-service 324MB (virtual 5.5GB)

# だから、コンテナを使うこともできない
$ docker exec -it 123456789012 bash
Error response from daemon: Container 12... is restarting, wait until the container is running

原因 : rootユーザーのパスワードを指定しないから

参考 : docker, docker-composeでmysqlが起動しない - Qiita

docker-compose.ymlにMYSQL_ROOT_PASSWORDでrootのパスワードは指定できる。
「mandatory」という英単語を知らず・・・「必須の」という意味だった。

MYSQL_ROOT_PASSWORD
This variable is mandatory and specifies the password that will be set for the MySQL root superuser account. In the above example, it was set to my-secret-pw.
mysql - Docker Hub

$ docker-compose logs | grep mysql
Attaching to mysql80, other-service
mysql80      | 2020-05-11 12:48:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
mysql80      | 2020-05-11 12:48:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql80      | 2020-05-11 12:48:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
mysql80      | 2020-05-11 12:48:28+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
mysql80      |    You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

対応 : docker-compose.ymlにMYSQL_ROOT_PASSWORDを指定する

docker-compose.yml
...省略...
    environment:
      MYSQL_DATABASE: sampledb
      MYSQL_USER: ponsuke
      MYSQL_PASSWORD: password
      MYSQL_TCP_PORT: 3306
      MYSQL_ROOT_PASSWORD: password
...省略...
# 作成に失敗したコンテナを
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                 CREATED             STATUS                          PORTS                   NAMES
123456789012        mysql:8.0.20         "docker-entrypoint.s…"  23 minutes ago      Restarting (1) 15 seconds ago                          mysql80
abcdefghijkl        host_other-service   "/usr/sbin/init"         33 hours ago        Up 30 minutes                   0.0.0.0:3389->3389/tcp other-service

# 停止して、
$ docker-compose stop mysql80
Stopping mysql80 ... done

# 削除する。
$ docker-compose rm -f mysql80
Going to remove mysql80
Removing mysql80 ... done

# 使わないボリュームなんかを掃除する
$ docker system prune -a -f --volumes
...省略...
Total reclaimed space: 540.6MB

# 作り直す
$ docker-compose up -d --no-recreate --build
...省略...
Status: Downloaded newer image for mysql:8.0.20
Creating mysql80   ... done
Starting other-service ... done

# 今度はちゃんと起動している
$ docker ps -s
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                  NAMES         SIZE
4870537bd210        mysql:8.0.20        "docker-entrypoint.s…" 2 minutes ago       Up 2 minutes        33060/tcp              mysql80       7B (virtual 541MB)
abcdefghijkl        4c61a7143a87        "/usr/sbin/init"        33 hours ago        Up 46 minutes       0.0.0.0:3389->3389/tcp other-service 324MB (virtual 5.5GB)
6
8
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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?