skip to Main Content

I’ve got my dev environment running in docker (nginx, php and mariadb) and try to create a database in symfony with doctrine. When I run php bin/console doctrine:database:create , I will get the following error:

[critical] Error thrown while running command "doctrine:database:create". Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution"

My configuration in .env looks like this:

DATABASE_URL="mysql://admin:symfony-admin@db/symfony_test?serverVersion=mariadb-10.1"

And this is my docker-compose.yml:

version: "3.6"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./code:/code
      - ./site.conf:/etc/nginx/conf.d/site.conf
    depends_on:
      - php

  php:
    build: .
    volumes:
      - ./code:/code 
    links:
      - db

  db:
    image: mariadb:latest
    restart: always
    ports:
      - "33006:3306"
    volumes:
      - ./db:/docker-entrypoint-initdb.d/
    environment:
      MYSQL_ROOT_PASSWORD: 'symfony-root-pwd'
      MYSQL_DATABASE: 'symfony_db'
      MYSQL_USER: 'admin'
      MYSQL_PASSWORD: 'symfony-admin'

I also use adminer to have access to the database and there the login works.

Does someone know why I can’t create a database with doctrine?

Cheers,

Michael

Solution:

I found the solution by myself. The command php bin/console doctrine:database:create need to be run within the php docker container and not in the local terminal.
So at first docker-compose exec php /bin/bash and then php bin/console doctrine:database:create

2

Answers


  1. You can add

    links:
          - db
    

    to php service config and change your DATABASE_URL to
    DATABASE_URL="mysql://admin:symfony-admin@db/symfony_db?serverVersion=mariadb-X.X.X"

    Service db will be exposed to php service, and in php container db will be host (link) to db service

    Login or Signup to reply.
  2. Usually, this problem is in the password. As the Connection String is a URL, that encodes the special characters thinking it is a parameter.

    To make it clear, fixing this requires one of these 2 steps:

    1. URL encoding of the password so that you get quoted (% encoded) characters, e.g. in .env.local or .env:

    For example, by replacing character # by %23 (its encoded version).

    -DATABASE_URL=mysql://user:special#password@localhost:3306/db?serverVersion=8.0
    +DATABASE_URL=mysql://user:special%23password@localhost:3306/db?serverVersion=8.0

    1. Or Removing resolve: from doctrine.dbal.url in config/packages/doctrine.yaml:
    doctrine:
        dbal:
             url: '%env(DATABASE_URL)%'
    

    The first one works for me!

    Answer reference: Malformed parameter "url". Symfony 5 does not accept special characters. #35568

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search