Laradockのmysqlが起動できない時の対応(State = Exit0, Exit 1, Exit 3)

調べてたらよくある話でしたが、Laradockで環境を作っていたところ、mysqlのコンテナが起動できませんでした。
StateがExit1→Exit3→Exit1→Exit0→起動と色々なタイプのエラーが出たのでそれぞれ対処法を調べてまとめました。

## 環境

```bash

$ docker version
Client: Docker Engine - Community
 Version:           18.09.0
 API version:       1.37 (downgraded from 1.39)
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:47:43 2018
 OS/Arch:           darwin/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.05.0-ce
  API version:      1.37 (minimum version 1.12)
  Go version:       go1.10.1
  Git commit:       f150324
  Built:            Wed May  9 22:20:42 2018
  OS/Arch:          linux/amd64
  Experimental:     false
```

## やったこととエラーの詳細
[公式のLaradockのドキュメント](http://laradock.io/)に沿って下記のコマンドを実行します。

```bash

$ docker-compose up -d nginx mysql phpmyadmin redis workspace 
```

エラーは出ていませんでしたが、mysqlが立ち上がっていないようなので下記のコマンドで確認しました。
mysqlのみ `Exit 1` というステータスになっており起動ができていないようです。再度mysqlのみ起動させようとしましたがエラーでした。

```bash

$ docker-compose ps
           Name                          Command               State                     Ports
----------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh            Up       2375/tcp
laradock_mysql_1              docker-entrypoint.sh mysqld      Exit 1
laradock_nginx_1              nginx                            Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm    Up       9000/tcp
laradock_phpmyadmin_1         /run.sh supervisord -n           Up       0.0.0.0:8080->80/tcp, 9000/tcp
laradock_redis_1              docker-entrypoint.sh redis ...   Up       0.0.0.0:6379->6379/tcp
laradock_workspace_1          /sbin/my_init                    Up       0.0.0.0:2222->22/tcp

$ docker-compose up mysql
Starting otonari_mysql ... done
Attaching to otonari_mysql
otonari_mysql             | Initializing database
otonari_mysql             | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
otonari_mysql             | 2019-01-15T09:55:59.978413Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
otonari_mysql             | 2019-01-15T09:55:59.978462Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
otonari_mysql             | 2019-01-15T09:55:59.981839Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
otonari_mysql             | 2019-01-15T09:55:59.982534Z 0 [ERROR] Aborting
otonari_mysql             | 
otonari_mysql exited with code 1
```

### 【Exit 1】mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
`/var/lib/mysql/is_writable`のパーミッションがないと言われてます。
github上ですでにやりとりされていたのでこちらに沿ってuserを追加しました。
https://github.com/docker-library/mysql/issues/219

```docker-compose.yml
mysql:
  build:
    context: ./mysql
    args:
      - MYSQL_VERSION=${MYSQL_VERSION}
  environment:
    - MYSQL_DATABASE=${MYSQL_DATABASE}
    - MYSQL_USER=${MYSQL_USER}
    - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    - TZ=${WORKSPACE_TIMEZONE}
  volumes:
    - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
    - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
  ports:
    - "${MYSQL_PORT}:3306"
  networks:
    - backend
  user: "1000:50" # 追加
 ```

変化があったか再度実行して確認しましたが、どうやらステータスがexit1からexit3になった模様

```bash

 $ docker-compose up mysql
Recreating laradock_mysql_1 ... done
Attaching to laradock_mysql_1
mysql_1                | Initializing database
mysql_1                | 2019-01-16T00:43:07.776657Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1                | 2019-01-16T00:43:07.776731Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
mysql_1                | 2019-01-16T00:43:07.777012Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.11) initializing of server in progress as process 15
mysql_1                | 2019-01-16T00:43:07.781438Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
mysql_1                | mbind: Operation not permitted
mysql_1                | 2019-01-16T00:43:08.945906Z 1 [ERROR] [MY-012592] [InnoDB] InnoDB: Operating system error number 22 in a file operation.
mysql_1                | 2019-01-16T00:43:08.948143Z 1 [ERROR] [MY-012596] [InnoDB] InnoDB: Error number 22 means 'Invalid argument'
mysql_1                | 2019-01-16T00:43:08.948671Z 1 [ERROR] [MY-012646] [InnoDB] InnoDB: File ./undo_001: 'aio write' returned OS error 122. Cannot continue operation
mysql_1                | 2019-01-16T00:43:08.949410Z 1 [ERROR] [MY-012981] [InnoDB] InnoDB: Cannot continue operation.
laradock_mysql_1 exited with code 3
```

### 【Exit 3】 InnoDB: Operating system error number 22 in a file operation.
こちらの記事ですでに対応されていたのでこちらに沿ってcommandを追加します。
https://blog.tes.co.jp/entry/2018/07/09/100555

```docker-compose.yml
mysql:
  build:
    context: ./mysql
    args:
      - MYSQL_VERSION=${MYSQL_VERSION}
  environment:
    - MYSQL_DATABASE=${MYSQL_DATABASE}
    - MYSQL_USER=${MYSQL_USER}
    - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    - TZ=${WORKSPACE_TIMEZONE}
  volumes:
    - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
    - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
  ports:
    - "${MYSQL_PORT}:3306"
  networks:
    - backend
  user: "1000:50"
  command: --innodb-use-native-aio=0 # 追加
```

これでできるようになったか再度コマンドを実行して確認しましたが、再度ステータスがexit1に戻りました。

```bash

 $ docker-compose up mysql
Recreating laradock_mysql_1 ... done
Attaching to laradock_mysql_1
mysql_1                | Initializing database
mysql_1                | 2019-01-16T00:45:31.293622Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1                | 2019-01-16T00:45:31.293692Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
mysql_1                | 2019-01-16T00:45:31.294066Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.11) initializing of server in progress as process 15
mysql_1                | 2019-01-16T00:45:31.296450Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
mysql_1                | 2019-01-16T00:45:31.297074Z 0 [ERROR] [MY-010119] [Server] Aborting
mysql_1                | 2019-01-16T00:45:31.298141Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.11)  MySQL Community Server - GPL.
laradock_mysql_1 exited with code 1
```

### 【Exit 1】 initialize specified but the data directory has files in it. Aborting.
dataディレクトリにファイルがあるのがいけないようなので、こちらを削除してみます。
先ほどの`docker-compose.yml`に記載されている、mysqlのvolume内にある下記のパスに出力されます。

> - ${DATA_PATH_HOST}/mysql:/var/lib/mysql

`DATA_PATH_HOST`は`.env`に記載されているので下記のコマンドでdataディレクトリの場所を確認します。

```bash
$ cat .env | grep DATA_PATH_HOST
DATA_PATH_HOST=~/.laradock/data
```

ということで一度削除を行い再度起動できるか確認します。

```bash
$ rm -rf ~/.laradock/data
$ docker-compose up mysql
Starting laradock_mysql_1 ... done
Attaching to laradock_mysql_1
mysql_1                | 2019-01-16T03:29:20.092125Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1                | 2019-01-16T03:29:20.092199Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
mysql_1                | 2019-01-16T03:29:20.092496Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.11) starting as process 1
mysql_1                | 2019-01-16T03:29:20.096822Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
mysql_1                | mbind: Operation not permitted
mysql_1                | 2019-01-16T03:29:20.913803Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1                | 2019-01-16T03:29:20.926521Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1                | 2019-01-16T03:29:20.945320Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.946705Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.947626Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.948236Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.949406Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.950447Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.951790Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.962215Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1                | 2019-01-16T03:29:20.962941Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
CÇ^CGracefully stopping... (press Ctrl+C again to force)
Stopping laradock_mysql_1 ... done
```

warningがで続けて終わらないので、一度ストップしてステータスを確認すると `Exit 0` に変わりました。

```
 $ docker-compose ps
           Name                          Command               State                     Ports
----------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh            Up       2375/tcp
laradock_mysql_1              docker-entrypoint.sh --inn ...   Exit 0
laradock_nginx_1              nginx                            Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm    Up       9000/tcp
laradock_phpmyadmin_1         /run.sh supervisord -n           Up       0.0.0.0:8080->80/tcp, 9000/tcp
laradock_redis_1              docker-entrypoint.sh redis ...   Up       0.0.0.0:6379->6379/tcp
laradock_workspace_1          /sbin/my_init                    Up       0.0.0.0:2222->22/tcp
```

### 【Exit 0】 connection pool is full
exit 0の情報があまり出てこなくてログを確認していたところ `connection pool is full` がいくつか出ていて気になりました。

```
$ docker-compose logs
(省略)
WARNING: Connection pool is full, discarding connection: 192.168.99.100
```

こちらのコマンドで一度削除を行い再度実行したところでやっとmysqlも立ち上がりました！
[docker network prune](https://docs.docker.com/engine/reference/commandline/network_prune/)

```
$ docker network prune
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] N

$ docker-compose up -d php-fpm nginx mysql workspace
Creating network "laradock_frontend" with driver "bridge"
Creating network "laradock_backend" with driver "bridge"
Creating network "laradock_default" with the default driver
Creating laradock_mysql_1            ... done
Creating laradock_docker-in-docker_1 ... done
Creating laradock_workspace_1        ... done
Creating laradock_php-fpm_1          ... done
Creating laradock_nginx_1            ... done

$ docker-compose ps

           Name                          Command               State                    Ports
---------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh            Up      2375/tcp
laradock_mysql_1              docker-entrypoint.sh --inn ...   Up      0.0.0.0:3306->3306/tcp
laradock_nginx_1              nginx                            Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm    Up      9000/tcp
laradock_workspace_1          /sbin/my_init                    Up      0.0.0.0:2222->22/tcp

```
## その後
一度全てコンテナを削除をしてから再度コンテナを立ち上げても何もエラーは発生しなくなりました。

## 追記
mysqlの問題ではないのですが、新しいメンバーが環境を作成する際にエラーになったのでここに追記しちゃいます。
下記のエラーが出て環境構築が駅なかったのですが、参考の記事を参考に対応を行いました。

```
adduser: group 'www-data' in use
ERROR: Service 'nginx' failed to build: The command '/bin/sh -c apk update     && apk upgrade     && apk add --no-cache openssl     && apk add --no-cache bash     && adduser -D -H -u 1000 -s /bin/bash www-data' returned a non-zero code: 1
```

参考
[Laradockコンテナのビルドで ”adduser: group ‘www-data’ in use” が発生したときの対応【メモ】 ](https://imanengineer.net/laradock-build-faild-adduser-group-www-data-in-use/)