[初心者の備忘録] Dockerで、MySQLの入ったdbコンテナが立ち上がらない！

備忘録です。

##  1.やりたいこと
`docker-compose up`コマンドでMySQLの入ったdbコンテナを立ち上げたい。

## 2.課題
dbコンテナでエラーが発生し、立ち上がらない。

## 状況
- 数週間ぶりに作業しようとした
- Dockerコンテナは、ApacheとPHPの入ったappコンテナと、MySQLの入ったdbコンテナ、phpMyAdminコンテナ

```zsh:ターミナル
$ docker-compose up -d
Creating network "portfolio_default" with the default driver
Creating portfolio_db_1 ... done
Creating portfolio_app_1        ... done
Creating portfolio_phpmyadmin_1 ... done

$ docker-compose ps
       Name                     Command               State            Ports
------------------------------------------------------------------------------------
portfolio_app_1          docker-php-entrypoint apac ...   Up       0.0.0.0:60080->80/tcp
portfolio_db_1           docker-entrypoint.sh mysqld      Exit 1
portfolio_phpmyadmin_1   /docker-entrypoint.sh apac ...   Up       0.0.0.0:61080->80/tcp
```

dbコンテナ以外は立ち上がってる。

## 環境
MacOS 11.2
Docker Desktop for mac
dbコンテナ（MySQL:5.5.62）
appコンテナ（php:7.4-apache）
phpMyAdminコンテナ

- 参考：docker-compose.ymlの中身

```yaml:docker-compose.yml

version: "3"

services:
  app:
    build:
      context: .
      dockerfile: docker/app/Dockerfile
    ports:
      - "60080:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:5.5.62
    ports:
      - "63306:3306"
    volumes:
      - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
      - mysql_data:/var/lib/mysql
    env_file:
      - ./docker/db/db-variables.env

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=db
      - PMA_USER=root
      - PMA_PASSWORD=pass
    links:
      - db
    ports:
      - 61080:80
    volumes:
      - /sessions

volumes:
  mysql_data:
```

- dbコンテナのログを確認すると、下記が出力された

```:ログ
$ docker-compose logs db
Attaching to portfolio_db_1
db_1          | 210206 22:31:38 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
db_1          | 210206 22:31:38 [Note] mysqld (mysqld 5.5.62) starting as process 1 ...
db_1          | 210206 22:31:38 [Note] Plugin 'FEDERATED' is disabled.
db_1          | mysqld: Table 'mysql.plugin' doesn't exist
db_1          | 210206 22:31:38 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
db_1          | 210206 22:31:38 InnoDB: The InnoDB memory heap is disabled
db_1          | 210206 22:31:38 InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1          | 210206 22:31:38 InnoDB: Compressed tables use zlib 1.2.11
db_1          | 210206 22:31:38 InnoDB: Using Linux native AIO
db_1          | 210206 22:31:38 InnoDB: Initializing buffer pool, size = 128.0M
db_1          | 210206 22:31:38 InnoDB: Completed initialization of buffer pool
db_1          | 210206 22:31:38 InnoDB: highest supported file format is Barracuda.
db_1          | InnoDB: Log scan progressed past the checkpoint lsn 48941
db_1          | 210206 22:31:38  InnoDB: Database was not shut down normally!
db_1          | InnoDB: Starting crash recovery.
db_1          | InnoDB: Reading tablespace information from the .ibd files...
db_1          | InnoDB: Restoring possible half-written data pages from the doublewrite
db_1          | InnoDB: buffer...
db_1          | InnoDB: Warning: database page corruption or a failed
db_1          | InnoDB: file read of space 0 page 202.
db_1          | InnoDB: Trying to recover it from the doublewrite buffer.
db_1          | InnoDB: Recovered the page from the doublewrite buffer.
db_1          | InnoDB: Doing recovery: scanned up to log sequence number 1595675
db_1          | 210206 22:31:38  InnoDB: Starting an apply batch of log records to the database...
db_1          | InnoDB: Progress in percents: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
db_1          | InnoDB: Apply batch completed
db_1          | 210206 22:31:38  InnoDB: Waiting for the background threads to start
db_1          | 210206 22:31:39 InnoDB: 5.5.62 started; log sequence number 1595675
db_1          | 210206 22:31:39 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
db_1          | 210206 22:31:39 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
db_1          | 210206 22:31:39 [Note] Server socket created on IP: '0.0.0.0'.
db_1          | 210206 22:31:39 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
```

# 3.最終的に効果のあった対策

### 3-1.Dockerの不要なイメージ、コンテナ、ネットワーク、ボリュームを削除して、まっさらな状態からやり直し
ググっても、ピンポイントの解決方法は分からず、まっさらする。

```zsh:ターミナル
# Docker コンテナの停止・削除
$ docker-compose down
Stopping portfolio_phpmyadmin_1 ... done
Stopping portfolio_app_1        ... done
Removing portfolio_phpmyadmin_1 ... done
Removing portfolio_app_1        ... done
Removing portfolio_db_1         ... done
Removing network portfolio_default

# Docker の不要なコンテナ、ネットワーク、ボリュームを削除する（消しまくるので注意）
$ docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

# ボリュームが残ったので、指定して削除する
$ docker system prune --volumes
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all volumes not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

# Docker イメージのビルド
$ docker-compose build
（省略）
Successfully built 04808bd6e7b7
Successfully tagged portfolio_app:latest

# Docker コンテナの起動
$ docker-compose up -d
Creating network "portfolio_default" with the default driver
Creating volume "portfolio_mysql_data" with default driver
Creating portfolio_db_1 ... done
Creating portfolio_app_1        ... done
Creating portfolio_phpmyadmin_1 ... done

# コンテナの起動状態を確認する
portfolio $ docker-compose ps
       Name                     Command               State            Ports
------------------------------------------------------------------------------------
portfolio_app_1          docker-php-entrypoint apac ...   Up       0.0.0.0:60080->80/tcp
portfolio_db_1           docker-entrypoint.sh mysqld      Exit 1
portfolio_phpmyadmin_1   /docker-entrypoint.sh apac ...   Up       0.0.0.0:61080->80/tcp
```

まだ起動してない。dbコンテナのログを再度、確認してみる （エラー内容がなんか変わってる！）

```:ログ

$ docker-compose logs db
Initializing database
210207  3:47:06 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
210207  3:47:06 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.5.62) starting as process 69 ...
210207  3:47:06 [ERROR] Fatal error: Illegal or unknown default time zone 'Asia/Tokyo'

Installation of system tables failed!  Examine the logs in
/var/lib/mysql/ for more information.

You can try to start the mysqld daemon with:

    shell> /usr/local/mysql/bin/mysqld --skip-grant &

and use the command line tool /usr/local/mysql/bin/mysql
to connect to the mysql database and look at the grant tables:

    shell> /usr/local/mysql/bin/mysql -u root mysql
    mysql> show tables

Try 'mysqld --help' if you have problems with paths.  Using --log
gives you a log in /var/lib/mysql/ that may be helpful.

Please consult the MySQL manual section
'Problems running mysql_install_db', and the manual section that
describes problems on your OS.  Another information source are the
MySQL email archives available at http://lists.mysql.com/.

Please check all of the above before submitting a bug report
at http://bugs.mysql.com/
```
`[ERROR] Fatal error: Illegal or unknown default time zone 'Asia/Tokyo'`って言われている。

### 3-2.docker/db/my.cnf の内容を１行削除

```
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-time-zone = 'Asia/Tokyo' # この行を削除

[client]
default-character-set=utf8mb4
```

### 3-3.もう一度、Dockerの不要なイメージ、コンテナ、ネットワーク、ボリュームを削除して、まっさらな状態からやり直し
（3-1と全く同じコマンド）

```:ターミナル

$ docker-compose down
$ docker system prune
$ docker system prune --volumes
$ docker-compose build
$ docker-compose up -d
$ docker-compose ps
       Name                     Command               State            Ports
-------------------------------------------------------------------------------------
portfolio_app_1          docker-php-entrypoint apac ...   Up      0.0.0.0:60080->80/tcp
portfolio_db_1           docker-entrypoint.sh mysqld      Up      0.0.0.0:63306->3306/tcp
portfolio_phpmyadmin_1   /docker-entrypoint.sh apac ...   Up      0.0.0.0:61080->80/tcp

```
立ち上がったーーー！:sob: