1

I Created a docker configuration for LEMP local server. I Tried to connect the Symfony app with MySQL database version 8 but the Connection is refused.

Error: SQLSTATE[HY000] [2002] Connection refused.

The problem is with DATABASE_URL in .env file required by Symfony 5. What is the correct value for DATABASE_URL for a given docker-compose.yml configuration ? I run this on Docker for Windows. I was Able to connect to mysql bash with username root and root password.

docker-compose.yml:

###############################################################################
#                          Generated on phpdocker.io                          #
###############################################################################
version: "3.1"
services:

    memcached:
      image: memcached:alpine
      container_name: sampleapp-memcached

    mailhog:
      image: mailhog/mailhog:latest
      container_name: sampleapp-mailhog
      ports:
        - "8001:8025"

    redis:
      image: redis:alpine
      container_name: sampleapp-redis

    mysql:
      image: mysql:8.0
      container_name: sampleapp-mysql
      working_dir: /app
      volumes:
        - .:/app
      environment:
        - MYSQL_ROOT_PASSWORD=Ur7HJWzZ2QK9
        - MYSQL_DATABASE=maindatabase
        - MYSQL_USER=sampleuser
        - MYSQL_PASSWORD=FxGBWfJ86ykq
      ports:
        - "8002:3306"

    elasticsearch:
      image: elasticsearch:6.5.4
      container_name: sampleapp-elasticsearch

    webserver:
      image: nginx:alpine
      container_name: sampleapp-webserver
      working_dir: /app
      volumes:
          - .:/app
          - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8000:80"

    php-fpm:
      build: phpdocker/php-fpm
      container_name: sampleapp-php-fpm
      working_dir: /app
      volumes:
        - .:/app
        - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini


2
  • Please share the current configuration - when that error message is thrown by MySQL itself, the connection is technically possible, but fails due to permission issues Commented Jun 2, 2020 at 14:02
  • fixed the grammar, and formatted some words Commented Jun 2, 2020 at 21:23

1 Answer 1

2

Usually the DATABASE_URL string is a standard one:

mysql://user:pwd@host:port/db

In this case:

DATABASE_URL=mysql://sampleuser:[email protected]:8002/maindatabase

Put it in your .env.local file and it should work. Note however that I work on unix and I do not have a Windows machine nearby.

Update

Following Alexander's suggestion, if you are running your Symfony app in a container (I didn't notice your webserver container, sorry!), then you should change the string to

DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq@mysql:3306/maindatabase

Note, however, that for container to container communications you should use the default port (or expose a new one). As regards setting the serverVersion, you can use the query parameter or (IMHO a better approach) set it in your doctrine.yaml config file. In addition, note that your server version should match the one you've specified in your docker-compose file.

Sign up to request clarification or add additional context in comments.

3 Comments

you are almost right. The correct DSN is: DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq@mysql:8002/maindatabase?serverVersion=5.7. Not the mysql it replaces 127.0.0.1, because it should connect to mysql container and 127.0.0.1 refers to it's own container.
Thank you for your comment, you are right! However, for a container-to-container communication, you should use the default port, right? And also, the server version should match the one indicated in the docker-compose file. Am I right? By the way, I've updated the answer, let me know.
Regarding the port, no. In his case he mapped the port 8002, so he can use port 8002. That is the purpose of mapping the ports. Regarding the server version, you can use the query parameter in the DSN /keep in mind that this is not the same as DNS. DSN stands for Data Source Name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.