Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create MySQL database #650

Open
AlbertoNessi opened this issue Aug 6, 2024 · 2 comments
Open

Cannot create MySQL database #650

AlbertoNessi opened this issue Aug 6, 2024 · 2 comments
Assignees

Comments

@AlbertoNessi
Copy link

AlbertoNessi commented Aug 6, 2024

Hi everyone!

I'm on a Macbook Air M2 2022 and i'm trying to create the database with MySQL instead of PostgreSQL, but when i run:

docker compose exec php bin/console dbal:run-sql -q "SELECT 1" && echo "OK" || echo "Connection is not working"

I get:

In ExceptionConverter.php line 101:
  An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for database failed: Name or service not known
...
Connection is not working.

And sometimes this:

Error response from daemon: Container <container_id> is restarting, wait until the container is running
Connection is not working

If i run:

docker-compose logs database

i get:

Creating a "symfony/skeleton " project at "./tmp"
container_name-database-1  |
container_name-database-1  | In CreateProjectCommand.php line 388:
container_name-database-1  |
container_name-database-1  |   Invalid stability provided (), must be one of: stable, RC, beta, alpha, dev

These are my edits from PostgreSQL, I followed the guide of this project obviously: Using MySQL

  • In my Dockerfile i added this line:
RUN install-php-extensions pdo_mysql

In compose.yaml

services:
  php:
    image: ${IMAGES_PREFIX:-}app-php
    restart: unless-stopped
    environment:
      SERVER_NAME: ${SERVER_NAME:-localhost}, php:80
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      TRUSTED_PROXIES: ${TRUSTED_PROXIES:-127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16}
      TRUSTED_HOSTS: $${TRUSTED_HOSTS:-^$${SERVER_NAME:-example\.com|localhost}|php$$}
      # Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
      DATABASE_URL: mysql://${MYSQL_USER:-app}:${MYSQL_PASSWORD:-root_psw}@database:3306/${MYSQL_DATABASE:-app}?serverVersion=${MYSQL_VERSION:-8}&charset=${MYSQL_CHARSET:-utf8mb4}
      # Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration
      MERCURE_URL: ${CADDY_MERCURE_URL:-http://php/.well-known/mercure}
      MERCURE_PUBLIC_URL: $${CADDY_MERCURE_PUBLIC_URL:-https://$${SERVER_NAME:-localhost}/.well-known/mercure}
      MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      # The two next lines can be removed after initial installation
      SYMFONY_VERSION: ${SYMFONY_VERSION:-}
      STABILITY: ${STABILITY:-stable}
    volumes:
      - caddy_data:/data
      - caddy_config:/config
    ports:
      # HTTP
      - target: 80
        published: ${HTTP_PORT:-80}
        protocol: tcp
      # HTTPS
      - target: 443
        published: ${HTTPS_PORT:-443}
        protocol: tcp
      # HTTP/3
      - target: 443
        published: ${HTTP3_PORT:-443}
        protocol: udp

# Mercure is installed as a Caddy module, prevent the Flex recipe from installing another service
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

###> doctrine/doctrine-bundle ###
  database:
    image: mysql:${MYSQL_VERSION:-8}
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE:-app}
      # You should definitely change the password in production
      MYSQL_RANDOM_ROOT_PASSWORD: "true"
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-root_psw}
      MYSQL_USER: ${MYSQL_USER:-app}
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 5s
      retries: 5
      start_period: 60s
    volumes:
      - database_data:/var/lib/mysql:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/mysql:rw
###< doctrine/doctrine-bundle ###

volumes:
  caddy_data:
  caddy_config:
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

###> doctrine/doctrine-bundle ###
  database_data:
###< doctrine/doctrine-bundle ###

In compose.override.yaml:

# Development environment override
services:
  php:
    build:
      context: .
      target: frankenphp_dev
    volumes:
      - ./:/app
      - ./frankenphp/Caddyfile:/etc/caddy/Caddyfile:ro
      - ./frankenphp/conf.d/app.dev.ini:/usr/local/etc/php/conf.d/app.dev.ini:ro
      # If you develop on Mac or Windows you can remove the vendor/ directory
      #  from the bind-mount for better performance by enabling the next line:
      #- /app/vendor
    environment:
      MERCURE_EXTRA_DIRECTIVES: demo
      # See https://xdebug.org/docs/all_settings#mode
      XDEBUG_MODE: "${XDEBUG_MODE:-off}"
    extra_hosts:
      # Ensure that host.docker.internal is correctly defined on Linux
      - host.docker.internal:host-gateway
    tty: true

###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

###> doctrine/doctrine-bundle ###
  database:
    ports:
      - "3306"
###< doctrine/doctrine-bundle ###

and the .env file:

# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
# https://symfony.com/doc/current/configuration/secrets.html
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=65efaf30cc8461feb7b8a398bc16fda9
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8.0.32&charset=utf8mb4"
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
DATABASE_URL=mysql://${MYSQL_USER:-app}:${MYSQL_PASSWORD:-root_psw}@database:3306/${MYSQL_DATABASE:-app}?serverVersion=${MYSQL_VERSION:-8}&charset=${MYSQL_CHARSET:-utf8mb4}
###< doctrine/doctrine-bundle ###

What can i do to fix this? I seem to be configuring everything correctly.

Thanks in advance!

@dunglas
Copy link
Owner

dunglas commented Aug 6, 2024

Commenting this block fixes the issue on my side: https://github.com/dunglas/symfony-docker/blob/main/frankenphp/docker-entrypoint.sh#L33-L37

It looks like Doctrine returns a 255 status code when failing to connect to MySQL. It's not the case for Postgres. Maybe should we tweak or comment this check?

Context: #110

@maxhelias maxhelias added support Support requested bug and removed support Support requested labels Aug 7, 2024
@vdwijngaert
Copy link

Commenting this block fixes the issue on my side: https://github.com/dunglas/symfony-docker/blob/main/frankenphp/docker-entrypoint.sh#L33-L37

It looks like Doctrine returns a 255 status code when failing to connect to MySQL. It's not the case for Postgres. Maybe should we tweak or comment this check?

Context: #110

Might also be a good idea to mentioned it somewhere in docs/mysql.md.

I've been looking into it, and it seems like the 2002 Connection refused exception code is converted to 255 by Symfony\Component\Console\Application here. Seems like it has to do with the inner workings of the Database Driver being used: what error codes are returned, and how it's being converted into an exception (and whether the error code is set as the Exception code).

Anyway, safe to say it's probably not ideal to use the 255 exit code as a way of identifying failure states unrelated to the database health check. I don't have any immediate alternatives though, only thing that comes to mind is to wrap the dbal:run-sql in a custom command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
@dunglas @vdwijngaert @maxhelias @AlbertoNessi and others