0

I'm getting this error:

An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

I have tried changing the IP address in my .env to localhost but I then got a not found error.

I also tried changing my .env db host to match my docker compose file:

DB_HOST=mysql

docker composer file:

version: "3.7"
  services:
    app:
    image: kooldev/php:7.4-nginx
    ports:
      - ${KOOL_APP_PORT:-80}:80
  environment:
    ASUSER: ${KOOL_ASUSER:-0}
    UID: ${UID:-0}
  volumes:
    - .:/app:delegated
  networks:
    - kool_local
    - kool_global
  database:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
  ports:
    - ${KOOL_DATABASE_PORT:-3306}:3306

I used kool.dev to do the Symfony install, that looks ok and the DB seems to be working as expected:

user@DESKTOP-QSCSABV:/mnt/c/dev/symfony-project$ kool status
+----------+---------+------------------------------------------------------+-------------------------+
| SERVICE  | RUNNING | PORTS                                                
| STATE                   |
+----------+---------+------------------------------------------------------+-------------------------+
| app      | Running | 0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp          
| Up 15 minutes           |
| database | Running | 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 
33060/tcp | Up 15 minutes (healthy) |
+----------+---------+------------------------------------------------------+-------------------------+

[done] Fetching services status g

in my .env file:

DB_USERNAME=myusername
DB_PASSWORD=mypassword
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydatabase
DB_VERSION=8.0
DATABASE_URL="mysql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?serverVersion=${DB_VERSION}"

Any suggestions on how to resolve this?

user1532669
  • 2,288
  • 4
  • 36
  • 72
  • Please edit your question and add the code, logs, output, error messages... in the question body as code or citation blocks. Using images is usually barely readable, it impairs search engines ability to index the content, visually impaired people cannot use their voice synthetsizer, people trying to help you cannot copy/paste the content if needed, it uses (in best cases...) 1000 times more data volume (e.g. disk space to store, data transfers....) than the equivalent text. And above all, it is specifically listed as a bad practice in [How to ask](/help/how-to-ask). Thanks – Zeitounator Jul 29 '21 at 10:04
  • https://stackoverflow.com/a/26719422/5391965 – Reynadan Jul 29 '21 at 10:20

1 Answers1

1
DB_HOST=127.0.0.1

in your environment file should be

DB_HOST=database

127.0.0.1 is the address of the container itself, so in your case, the app container tries to make a connection to itself. Docker compose creates a virtual network where each container can be addressed by its service name. So in your case, you want to connect to the database service.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35