[Docker]mysqlに接続できない。SQLSTATE[HY000] [2002] php_network_getaddresses
解決したいこと
php artisan migrate
コマンドでmysqlに接続できないエラーが発生しました。
いくつかのサイトでは、docker-compose.ymlファイルとenvファイルの設定が異なっていることが原因と書かれていましたが私の環境では違いが見られず詰んでいるためご助言いただきたいです。
発生している問題・エラー
コンテナを立ち上げ、下記のコマンドを実行した際にエラーが発生しました。
$ php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = database and table_name = migrations and table_type = 'BASE TABLE')
該当するソースコード
db:
build:
context: .
dockerfile: ./docker/db/Dockerfile
ports:
- 3306:3306
environment:
MYSQL_DATABASE: training
MYSQL_USER: training-user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
TZ: 'Asia/Tokyo'
volumes:
- mysql-volume:/var/lib/mysql
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=training
DB_USERNAME=training-user
DB_PASSWORD=password
コンテナが立ち上がっていることは確認済みです。
$ docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
docker_handson-app-1 docker_handson-app "docker-php-entrypoi…" app 31 minutes ago Up 31 minutes 9000/tcp
docker_handson-db-1 docker_handson-db "docker-entrypoint.s…" db 21 minutes ago Up 21 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp
docker_handson-web-1 docker_handson-web "/docker-entrypoint.…" web 31 minutes ago Up 31 minutes 0.0.0.0:8081->80/tcp
自分で試したこと
dockerDesktopで該当のContainers,Images,Volumesを削除
変更が反映されていないと思い、全て削除してから再度コンテナを立ち上げました。
docker-compose up -d --build
「docker-compose logs」でログを確認
エラーは見当たりませんでした、、
$ docker-compose logs
docker_handson-app-1 | [10-Mar-2024 15:26:49] NOTICE: fpm is running, pid 1
docker_handson-app-1 | [10-Mar-2024 15:26:49] NOTICE: ready to handle connections
docker_handson-web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
docker_handson-web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
docker_handson-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
docker_handson-web-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
docker_handson-web-1 | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
docker_handson-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
docker_handson-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
docker_handson-web-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: using the "epoll" event method
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: nginx/1.20.2
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: built by gcc 10.3.1 20210424 (Alpine 10.3.1_git20210424)
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: OS: Linux 5.15.49-linuxkit
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: start worker processes
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: start worker process 31
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: start worker process 32
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: start worker process 33
docker_handson-web-1 | 2024/03/10 15:26:49 [notice] 1#1: start worker process 34
docker_handson-db-1 | 2024-03-10 15:37:00+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.36-1.el8 started.
docker_handson-db-1 | 2024-03-10 15:37:01+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
docker_handson-db-1 | 2024-03-10 15:37:01+09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.36-1.el8 started.
docker_handson-db-1 | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
docker_handson-db-1 | 2024-03-10T06:37:01.536056Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
docker_handson-db-1 | 2024-03-10T06:37:01.537315Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.36) starting as process 1
docker_handson-db-1 | 2024-03-10T06:37:01.542483Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
docker_handson-db-1 | 2024-03-10T06:37:01.641950Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
docker_handson-db-1 | 2024-03-10T06:37:01.801087Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
docker_handson-db-1 | 2024-03-10T06:37:01.801112Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
docker_handson-db-1 | 2024-03-10T06:37:01.802092Z 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.
docker_handson-db-1 | 2024-03-10T06:37:01.811629Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
docker_handson-db-1 | 2024-03-10T06:37:01.811645Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.36' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
他に確認すべき点等ありましたら教えていただきたいです。
よろしくお願いいたします。