skip to Main Content

Symfony can’t seem to connect to the database even if I can do it with the same credentials using PMA.

docker-compose.yml:

version: '3'
services:
apache:
    build: .docker/apache
    container_name: sf4_apache
    ports:
      - 80:80
    volumes:
      - .docker/config/vhosts:/etc/apache2/sites-enabled
      - .:/home/wwwroot/sf4
    depends_on:
      - php

mysql:
  image: mysql
  command: "--default-authentication-plugin=mysql_native_password"
  container_name: sf4_mysql
  restart: always
  volumes:
    - ./data/db/mysql:/var/lib/mysql
  environment:
    MYSQL_ROOT_PASSWORD: root
    MYSQL_DATABASE: sf4
    MYSQL_USER: sf4
    MYSQL_PASSWORD: sf4

php:
    build: .docker/php
    container_name: sf4_php
    volumes:
      - .:/home/wwwroot/sf4
    environment:
      - maildev_host=sf4_maildev
    depends_on:
      - maildev
      - mysql
    links:
      - mysql

phpmyadmin:
  image: phpmyadmin/phpmyadmin
  container_name: sf4_phpmyadmin
  environment:
    PMA_HOST: mysql
    PMA_PORT: 3306
  ports:
    - 8080:80
  links:
    - mysql

maildev:
    image: djfarrelly/maildev
    container_name: sf4_maildev
    ports:
      - 8001:80

Symfony .env:

DATABASE_URL=mysql://root:root@mysql:3306/sf4

Do I have to reconfigure my Doctrine file? Currently it’s default one:

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)%'

The error is:

An exception occurred in driver: could not find driver

And as I said I can connect to the DB via phpMyAdmin using root@root credentials.

This is variable $this->params from Connection.php file just before making a connection to DB:

 ["driver"]=>
  string(9) "pdo_mysql"
  ["charset"]=>
  string(7) "utf8mb4"
  ["url"]=>
  string(32) "mysql://root:root@mysql:3306/sf4"
  ["host"]=>
  string(5) "mysql"
  ["port"]=>
  string(4) "3306"
  ["user"]=>
  string(4) "root"
  ["password"]=>
  string(4) "root"
  ["driverOptions"]=>
  array(0) {
  }
  ["serverVersion"]=>
  string(6) "8.0.15"
  ["defaultTableOptions"]=>
  array(2) {
    ["charset"]=>
    string(7) "utf8mb4"
    ["collate"]=>
    string(18) "utf8mb4_unicode_ci"
  }
  ["dbname"]=>
  string(3) "sf4"

2

Answers


  1. Try this :

    doctrine:
        dbal:
            # configure these for your database server
            driver: 'pdo_mysql'
            server_version: '8.0.15'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci
    
            url: '%env(resolve:DATABASE_URL)%'
    

    The mysql image is at the latest version of mysql.

    The php image is missing the pdo_mysql driver

    it should be added in the Dockerfile like follows :

    RUN docker-php-ext-install pdo pdo_mysql zip
    

    After this, the image has to be rebuilt with the command :

    docker-compose build
    
    Login or Signup to reply.
  2. juste replace mysql by sf4_mysql because you must apply the container name (container_name: sf4_mysql) in your host
    ==>

    DATABASE_URL=mysql://root:root@mysql:3306/sf4
    by
    DATABASE_URL=mysql://root:root@sf4_mysql:3306/sf4

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