3
votes

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): Drupal\Core\Database\Driver\mysql\Connection::open(Array)\n#2 /var/www/html/core/lib/Drupal/Core/Database/Database.php(166): Drupal\Core\Database\Database::openConnection('default', 'default')\n#3 [internal function]: Drupal\Core\Database\Database::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): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'database')\n#6 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(260): Drupal\Component\DependencyInjection\Container->get('database', 1)\n#7 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(62): Drupal\Component\DependencyInjection\PhpArrayContainer->resolveServicesAndParameters(Array)\n#8 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'cache.container')\n#9 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(515): Drupal\Component\DependencyInjection\Container->get('cache.container')\n#10 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(876): Drupal\Core\DrupalKernel->getCachedContainerDefinition()\n#11 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(468): Drupal\Core\DrupalKernel->initializeContainer()\n#12 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(664): Drupal\Core\DrupalKernel->boot()\n#13 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#14 {main}

2
What is the case if I didn't defined phpmyadmin: section in docker-compose file. I am also facing same issueJackson

2 Answers

4
votes

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',
);
0
votes

Replace loca ip address

'host' => '127.0.0.1',

by service docker name

'host' => 'db',