Drupal8没有连接到docker中的mysql

xmq68pz9  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(361)

durpal 8的目录结构:

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

my settings.php数据库配置

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

我的docker-compose.yml代码

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

使用上面的代码,我可以将phpmyadmin连接到mysql,并且工作正常,但我无法将drupal连接到mysql,出现以下错误:
pdoexception:sqlstate[hy000][2002]第79行的/var/www/html/core/lib/drupal/component/dependencyinjection/phparraycontainer.php中的连接被拒绝#0/var/www/html/core/lib/drupal/core/database/driver/mysql/connection.php(420):pdo->mysql:host=127....'根','测试',数组)\n#1/var/www/html/core/lib/drupal/core/database/database.php(371):drupal\core\database\driver\mysql\connection::open(数组)\n#2/var/www/html/core/lib/drupal/core/database/database.php(166):drupal\core\database\database::openconnection('default','default')\n#3[内部函数]:drupal\core\database\database::getconnection('default')\n#4/var/www/html/core/lib/drupal/component/dependencyinjection/phparraycontainer.php(79):调用\u user\u func\u 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}

dy2hfwbg

dy2hfwbg1#

替换loca ip地址

'host' => '127.0.0.1',

按服务docker名称

'host' => 'db',
nkkqxpd9

nkkqxpd92#

我想您应该将数据库连接指向docker compose文件中创建的主机名。注册时不能是本地主机地址:

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

相关问题