Search code examples
phpdockersymfonydoctrine

SQLSTATE[HY000] [2002] Connection refused when I execute make:migrations


I have this containers

services:
  php:
    container_name: user-php
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html
      - ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
    ports:
      - "80:80"
    depends_on:
      - mysql
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
      XDEBUG_MODE: "develop,debug"
      XDEBUG_CONFIG: "client_host=host.docker.internal start_with_request=yes"

  mysql:
    container_name: user-mysql
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: user_database
      MYSQL_USER: my_user
      MYSQL_PASSWORD: my_password
    ports:
      - "3306:3306"
    volumes:
      - database_data:/var/lib/mysql

volumes:
  database_data:

I can connect to de database using docker exec and using dbbeaver-ce but when I try to execute php bin/console make:migrations on the php container I get this error message

[critical] Error thrown while running command "make:migration". Message: "An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused"

In ExceptionConverter.php line 101:
                                                                                  
  An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused  
                                                                                  

In Exception.php line 28:
                                             
  SQLSTATE[HY000] [2002] Connection refused  
                                             

In Driver.php line 34:
                                             
  SQLSTATE[HY000] [2002] Connection refused  
                                             

this is the connection url on .env file

DATABASE_URL="mysql://root:[email protected]:3306/user_database?serverVersion=8.0.39&charset=utf8mb4"

I changed the doctrine entity mapping to xml but it sholdn't be the problem.


Solution

  • Inside docker, the container name is used instead of 127.0.0.1. In your case, it is root:root@mysql:3306/...