简体   繁体   中英

Symfony 5 + Docker: 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:root@127.0.0.1:3306/database_name?serverVersion=8.0
DATABASE_ROOT_PASSWORD=root
DATABASE_NAME=database_name

service definition:

some.repository:
    class: Some\Class
    arguments:
        - '@database_connection'

The address in the DATABASE_URL 127.0.01 refers to the local docker container. The issue is that the MySQL is another container.

To resolve the issue: Replace 127.0.0.1 with the database container in your case “mysql”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM