解决方案:docker检查\ grep,获取正在运行容器的postgres的IP,并使用容器的ip和端口分别更新env、DB_HOST和DB_PORT。
更新:将postgres写到DB_HOST是最佳实践,因为ip地址可能会改变。
我已经搜索了一天来将数据库从docker容器连接到我的Laravel应用程序,但是无论我尝试了什么,即使我可以通过Postico连接它,我也不能迁移我的数据库。我得到以下错误:
SQLSTATE08006无法连接到服务器:服务器是否在主机"localhost“(127.0.0.1)上运行,并且在端口3306上接受TCP/IP连接?
我的文档文件:
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}"
我的对接者-撰写文件:
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
我的env文件:
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',
],
发布于 2018-02-17 01:44:53
检查数据库用户是否可以访问localhost
和127.0.0.1
。听起来您正在尝试连接到localhost
,但访问权限只授予127.0.0.1
,反之亦然。
https://stackoverflow.com/questions/48839876
复制相似问题