Symfony 5 SQLSTATE[HY000] [2002] Connection refused

I’m having the following error when trying to connect to DB using Symfony 5 and Docker.

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

Maybe you could know what’s wrong on my config files. If i missed any config file that could be related to this problem, please tell me.

docker-compose.yaml:

mysql:
    image: mysql:8
    ports:
        - "3306:3306"
    volumes:
        - ./docker/database:/docker-entrypoint-initdb.d
    environment:
        - MYSQL_DATABASE=${DATABASE_NAME}
        - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
    command: ["--default-authentication-plugin=mysql_native_password"]

doctrine.yaml:

doctrine:
dbal:
    url: '%env(resolve:DATABASE_URL)%'
    default_table_options:
        charset: utf8mb4
        collate: utf8mb4_unicode_ci

.env:

DATABASE_URL=mysql://root:[email protected]:3306/database_name?serverVersion=8.0
DATABASE_ROOT_PASSWORD=root
DATABASE_NAME=database_name

service definition:

some.repository:
    class: SomeClass
    arguments:
        - '@database_connection'

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


vtiertant

vtiertant

In your Docker, your host is not “localhost” or “127.0.0.1”, but “mysql” (defined in docker-compose.yaml)

So, replace in .env :
DATABASE_URL=mysql://root:[email protected]:3306/database_name?serverVersion=8.0