7

I use docker for MySql and Adminer interface, the connection between Adminer and MySql works but the Symfony connection is refused. Symfony isn't in a container.

What can i do for the connection between Symfony 4 and MySql container works ?

this is my docker-compose.yml :

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'

  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

my .env on symfony 4:

# This file defines all environment variables that the application needs.
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.
# Use ".env.local" for local overrides during development.
# Use real environment variables when deploying to production.
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=825aa371242c34bdcc0c693ac4241bcf
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS='^localhost|example\.com$'
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://testuser:[email protected]:3306/dbname

###< doctrine/doctrine-bundle ###

and my doctrine.yml :

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    #env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
        unix_socket: /tmp/mysql.sock
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

UPDATE


Well, thanks for your help it works, Symfony or more especially doctrine not found the container, like you said I exposed the Mysql port, but I have to make an other change : This line works for me :

DATABASE_URL=mysql://root:[email protected]:3306/dbname

and not this line :

DATABASE_URL=mysql://testuser:[email protected]:3306/dbname

In fact it seems like that my docker-compose up doesn't work very well and compose with old version, docker doesn't "see" my change on docker-compose.yml, so the password and user stay "root". I'm going to try this : https://forums.docker.com/t/how-to-delete-cache/5753/2 and this: https://github.com/docker/compose/issues/1487

Sometimes, database need to be delete => Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. https://hub.docker.com/_/postgres/

8
  • Possible duplicate of Symfony can't connect to MySQL Docker container Commented Nov 27, 2018 at 14:07
  • @Cid, not a duplicate. Hence the part where the OP mentions that symfony is NOT in a docker. This changes the whole nature of the issue Commented Nov 27, 2018 at 14:08
  • And @cisco_bro, is there a specific reason why your symfony is not in a docker? Commented Nov 27, 2018 at 14:09
  • @SvenHakvoort i try to have the most simple config, i already try to use Symfony in a container but I encountered the same problem. Commented Nov 27, 2018 at 14:24
  • @cisco_bro Your username and password might need to be URL Encoded if they contains specials characters. Commented Nov 27, 2018 at 15:13

1 Answer 1

3

You need to expose database port like eg.

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'
    ports:
        - 3306:3306
  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

and then in .env file

DATABASE_URL=mysql://testuser:[email protected]:3306/dbname

Make sure that the ip address matches the IP of your docker host.

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

2 Comments

I exposed the port and now I have a docker log : mysql_1_5d6024a6800d | 2018-11-27T15:10:55.985191Z 2 [Note] Access denied for user 'testuser'@'172.45.0.1' (using password: YES) It seems like my environnement configuration on docker-compose.yml doesn't work
Now it is a problem with your mysql account not with docker. You need to allow for remote access for your mysql user

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.