如何正确连接我的php(symfony)容器与mysql容器?

问题描述 投票:0回答:1

我试图将php容器与symfony 4连接到mysql容器,我收到此错误:

An exception occurred in driver: SQLSTATE [HY000] [2002] php_network_getaddresses: getaddrinfo failed: Unknown host.

我可以通过phpmyadmin连接mysql。

这是我的docker-compose.yml:

version: '3'
services:
    mysql:
        image: mysql
        command:
            - "--default-authentication-plugin=mysql_native_password"
        container_name: sf4_mysql
        volumes:
            - .docker/data/db:/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

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

和.env文件

DATABASE_URL=mysql://sf4:sf4@mysql/sf4

这是我的doctrine.yaml文件。我删除了不必要的评论。

parameters:
    env(DATABASE_URL): ''

doctrine:
    dbal:
        driver: 'pdo_mysql'
        server_version: '8.0'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

这是我目前的repository先谢谢你的帮助。

mysql docker docker-compose doctrine database-connection
1个回答
0
投票

端口丢失了。

尝试

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

提醒 :

确保在使用以下命令登录后从容器内执行symfony命令:

docker-compose exec php bash
© www.soinside.com 2019 - 2024. All rights reserved.