Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
Can't connect to MySQL inside docker image

I already found some threads to this topic but none of them were able to help me, because I already did the provided solution or the problem was not exactly the same as mine


I have a docker environment with an nginx/php-fpm etc server and am using the official mysql image which I'm linking into the other server. This is my docker-compose

db:
    image: mysql
    ports:
    - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=symfonyrootpass
      - MYSQL_DATABASE=symfony
      - MYSQL_USER=symfony
      - MYSQL_PASSWORD=symfonypass

project:
    image: docker.io/project/project-dev
    ports:
    - "80:80"
    environment:
      - XDEBUG_HOST=${LOCAL_IP}
      - XDEBUG_PORT=9000
      - XDEBUG_REMOTE_MODE=req
    links:
    - db
    volumes:
    - ../project:/var/www/app

When I start it and connect from my host machine like this: mysql -h127.0.0.1 -usymfony -psymfonypass and then use use symfony; it works without problems. I see my database, I can edit, delete, update etc. I can do everything.

However, I can't say the same for docker. I'm using Symfony and when I have the parameters set correctly, I'm always getting the error

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

These are my parameters

database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: symfony
database_password: symfonypass

I already tried to change database_host to localhost but then I got the error No such file or directory with the same SQLSTATE. I also tried to set the port to 3306 but this also didn't help.

What do I need to do to access my database from inside docker?

This is the part in the config.yml where I describe my mysql connection

Doctrine Configuration

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        server_version: "%database_version%"
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your database is running in a different container than your project. Docker-compose will give it an IP address and a host name, which is db because this is how you named it. So, database_host should not be 127.0.0.1, or localhost, but db.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda