Symfony 4学说无法连接到Docker MySQL

时间:2019-04-07 14:26:17

标签: php mysql symfony docker symfony4

即使我可以使用PMA以相同的凭据进行操作,Symfony似乎也无法连接到数据库。

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

我必须重新配置我的Doctrine文件吗?当前是默认值:

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

错误是:

  

驱动程序发生异常:找不到驱动程序

正如我所说,我可以使用root @ root凭据通过phpMyAdmin连接到数据库。

在连接到数据库之前,这是Connection.php文件中的变量$ this-> params:

 ["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 个答案:

答案 0 :(得分:1)

尝试一下:

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

mysql映像是mysql的最新版本。

php图像缺少pdo_mysql驱动程序

应将其添加到Dockerfile中,如下所示:

RUN docker-php-ext-install pdo pdo_mysql zip

此后,必须使用以下命令重建图像:

docker-compose build

答案 1 :(得分:0)

只需用 sf4_mysql 替换 mysql ,因为您必须在主机中应用容器名称(container_name:sf4_mysql) ==>

DATABASE_URL = mysql:// root:root @ mysql:3306 / sf4 通过 DATABASE_URL = mysql:// root:root @ sf4_mysql:3306 / sf4