gpt4 book ai didi

postgresql - Php artisan migrate 不适用于 docker 容器

转载 作者:行者123 更新时间:2023-11-29 12:16:52 27 4
gpt4 key购买 nike

解决方案:docker inspect | grep IP 获取运行容器的 postgres 的 ip,并分别使用容器的 ip 和端口更新您的 env DB_HOST 和 DB_PORT。

更新:将 postgres 写入 DB_HOST 是最佳实践,因为 ip 地址可能会改变。


我一直在寻找一天将数据库从 docker 容器连接到我的 Laravel 应用程序,但无论我尝试过什么,即使我可以通过 Postico 连接它,我也无法迁移我的数据库。我收到以下错误:

SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 3306?

我的 Dockerfile:

FROM php:7.1.3-fpm-alpine

RUN apk update && apk add build-base

RUN apk add postgresql postgresql-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql

RUN apk add zlib-dev git zip \
&& docker-php-ext-install zip

RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
&& ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

COPY . /app
WORKDIR /app

RUN composer install --prefer-source --no-interaction

ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}"

我的 docker-compose 文件:

version: '2'
services:
nginx:
image: nginx:1.11.10-alpine
ports:
- 3000:80
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- api
api:
build: .
ports:
- 9000:9000
volumes:
- .:/app
- /app/vendor
depends_on:
- postgres
environment:
DATABASE_URL: postgres://foodorder@postgres/foodorder
postgres:
image: postgres:latest
environment:
POSTGRES_USER: foodorder
POSTGRES_DB: foodorder
POSTGRES_PASSWORD: 123123
ports:
- 3306:5432

我的环境文件:

DB_CONNECTION = "pgsql"
DATABASE_CONNECTIONS_MASTER_PGSQL_HOST="localhost"
DATABASE_CONNECTIONS_MASTER_PGSQL_SCHEMA="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_DATABASE="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_PORT="3306"
DATABASE_CONNECTIONS_MASTER_PGSQL_USERNAME="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_PASSWORD="123123"

最后,在我的 database.php 中,

'default' => env('DB_CONNECTION', 'pgsql'),

'pgsql' => [
'driver' => 'pgsql',
'host' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_HOST'),
'port' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_PORT'),
'database' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_DATABASE'),
'username' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_USERNAME'),
'password' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_SCHEMA'),
'sslmode' => 'prefer',
],

最佳答案

检查数据库用户是否可以访问 localhost127.0.0.1。听起来您正在尝试连接到 localhost,但只授予了 127.0.0.1 的访问权限,反之亦然。

关于postgresql - Php artisan migrate 不适用于 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48839876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com