【问题标题】:Laravel Sail work migrate or contact with databaseLaravel Sail 工作迁移或与数据库联系
【发布时间】:2021-10-03 15:19:45
【问题描述】:

我运行 larvae 8 帆,他工作正常

我的文件内容是 docker-compose.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    rami.dev:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mariadb
    mariadb:
        image: 'mariadb:10'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmariadb:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" , "-h", "localhost"]
          retries: 3
          timeout: 5s
    phpmyadmin:
        image: phpmyadmin
        restart: always
        ports:
            - 8080:80
        environment:
            PMA_ARBITRARY: 1
            PMA_HOST: 'mariadb'
            PMA_USER: '${DB_USERNAME}'
            PMA_PASSWORD: '${DB_PASSWORD}'
        networks:
            - sail
    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
                - '/dev/shm:/dev/shm'
        networks:
            - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmariadb:
        driver: local

如果我把 .env DB_HOST=mariadb

我使用 mariadb 是因为我使用 mariadb 而不是 mysql

如果.env DB_HOST=mariadb 必须更改为DB_HOST=127.0.0.1,我无法运行php artisan migrate

并使用DB_HOST=mariadb 得到此错误

php artisan migrate

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = ramiyusu_live and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

      +36 vendor frames
  37  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

如果我更改为 DB_HOST=127.0.0.1 迁移工作并停止站点

Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `id` = 1 limit 1)
http://localhost/

如果返回 DB_HOST 我的站点工作正常,我可以同时运行并运行 migrate 使用 sail shell 然后 php artisan migrate

【问题讨论】:

  • 试试看这个stackoverflow.com/questions/42567475/…也许对你有帮助
  • 只有在容器中运行时,您的代码才能使用 DB_HOST=mariadb 访问数据库。您必须仅从容器 shell 使用 artisan 迁移,而不是从本地 shell。
  • @Maksim 是的,我知道这行得通

标签: laravel docker laravel-sail


【解决方案1】:

不要从本地机器调用php artisan migrate,而是从容器调用它。作为per documentation,你可以使用sail artisan migrate

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 2014-01-22
    • 2019-07-19
    • 2018-06-21
    • 2018-04-04
    • 2015-10-18
    • 2021-04-10
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多