0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docker-composeで立ち上げたAppからDBにアクセスするとConnection refusedになる

Last updated at Posted at 2025-01-26

背景

  • laravel + Vue3 + mysqlの構成でアプリを開発中
  • ローカル開発環境をXAMPPからDockerに移行
  • docker-composeでappコンテナとdbコンテナを立ち上げることはできたが、appにアクセスすると以下のエラーになる
    SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from `sessions` where `id` = xxx limit 1)`
    

解決法

接続先のDB_HOSTをDBのサービス名またはコンテナ名にする

docker-compose.yml
services:
  ...
  mysql: #サービス名
    image: mysql:8.0
    container_name: mysql-db #コンテナ名
    ports:
      - "3306:3306"
.env
DB_HOST=127.0.0.1 ⇒ NG
DB_HOST=mysql ⇒ OK
DB_HOST=mysql-db ⇒ OK

参考

0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?