0

我已经使用 Laravel Sail(基于 Docker)设置了我的 Laravel 应用程序。除 MySQL 外,一切正常。MySQL 服务器对 web ( local.mysite.com:8080) 和 CLI (ex: php artisan migrate) 的行为不同。

配置 (1)

如果我在我的.env文件中使用以下配置,

...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307

网络工作,而不是 CLI。当我运行php artisan migrate命令时,我得到SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

配置 (2)

如果我使用以下配置,

...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=local.mysite.com 
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307

CLI 工作正常,而不是 Web。我Connection Refured在网络上收到 MySQL 的错误。

我一直在用不同的尝试敲打我的头,没有运气......

我究竟做错了什么?


这是我docker-compose.yml的参考(phpmyadmin顺便说一句,效果很好):

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    phpmyadmin:
        depends_on:
            - mysql
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
            - '8081:80'
        environment:
            PMA_HOST: mysql
            MYSQL_ROOT_PASSWORD: password
        networks:
            - sail

    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        extra_hosts:
            - '${APP_DOMAIN}:127.0.0.1'
        hostname: '${APP_DOMAIN}'
        domainname: '${APP_DOMAIN}'
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local

4

1 回答 1

0

我意识到我的错误。

我应该使用sail artisan migrate而不是Laravel Sailphp artisan migrate官方文档中明确解释的那样。

因此正确的配置是:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307
于 2021-07-11T17:08:01.693 回答