skip to Main Content

Directories structure of durpal 8:

.
├── docker-compose.yml
├── dump
│   └── drupal.sql
├── sessions
└── www
    └── core
    └── site
    └── modules
    └── vendor
    └── index.php

My settings.php database configuration

$databases['default']['default'] = array (
'database' => 'drupal',
'username' => 'root',
'password' => 'test',
'prefix' => '',
'host' => '127.0.0.1',
'port' => '3306',
'namespace' => 'Drupal\Core\Database\Driver\mysql',
'driver' => 'mysql',
);

My docker-compose.yml code

version: "2"
services:
www:
    image: drupal:8
    ports: 
        - "8001:80"
    volumes:
        - ./www:/var/www/html/
    depends_on:
        - db
    networks:
        - default
db:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    ports: 
        - "3306:3306"
    environment:
        MYSQL_DATABASE: drupal
        MYSQL_USER: user
        MYSQL_PASSWORD: test
        MYSQL_ROOT_PASSWORD: test
    volumes:
        - ./dump:/docker-entrypoint-initdb.d
        - /var/lib/mysql
    networks:
        - default
phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links: 
        - db:db
    ports:
        - 8000:80
    environment:
        PMA_HOST: db
        MYSQL_USER: user
        MYSQL_PASSWORD: test
        MYSQL_ROOT_PASSWORD: test

Using above code I am able to connect phpmyadmin to mysql and its working fine but I am not able to connect Drupal to mysql and getting below error:

PDOException: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php on line 79 #0 /var/www/html/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php(420): PDO->__construct(‘mysql:host=127….’, ‘root’, ‘test’, Array)n#1 /var/www/html/core/lib/Drupal/Core/Database/Database.php(371): DrupalCoreDatabaseDrivermysqlConnection::open(Array)n#2 /var/www/html/core/lib/Drupal/Core/Database/Database.php(166): DrupalCoreDatabaseDatabase::openConnection(‘default’, ‘default’)n#3 [internal function]: DrupalCoreDatabaseDatabase::getConnection(‘default’)n#4 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(79): call_user_func_array(‘Drupal\Core\Dat…’, Array)n#5 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): DrupalComponentDependencyInjectionPhpArrayContainer->createService(Array, ‘database’)n#6 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(260): DrupalComponentDependencyInjectionContainer->get(‘database’, 1)n#7 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(62): DrupalComponentDependencyInjectionPhpArrayContainer->resolveServicesAndParameters(Array)n#8 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): DrupalComponentDependencyInjectionPhpArrayContainer->createService(Array, ‘cache.container’)n#9 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(515): DrupalComponentDependencyInjectionContainer->get(‘cache.container’)n#10 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(876): DrupalCoreDrupalKernel->getCachedContainerDefinition()n#11 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(468): DrupalCoreDrupalKernel->initializeContainer()n#12 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(664): DrupalCoreDrupalKernel->boot()n#13 /var/www/html/index.php(19): DrupalCoreDrupalKernel->handle(Object(SymfonyComponentHttpFoundationRequest))n#14 {main}

2

Answers


  1. I guess you should point your database connections to the hostname you created in docker-compose file. Can’t be a localhost address as you registered:

    $databases['default']['default'] = array (
    'database' => 'drupal',
    'username' => 'root',
    'password' => 'test',
    'prefix' => '',
    'host' => 'db', #here you point to the container's service name
    'port' => '3306', 
    'namespace' => 'Drupal\Core\Database\Driver\mysql',
    'driver' => 'mysql',
    );
    
    Login or Signup to reply.
  2. Replace loca ip address

    'host' => '127.0.0.1',
    

    by service docker name

    'host' => 'db',
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search