Symfony:驱动程序发生异常:SQLSTATE[08006] [7]无法连接到服务器

pgky5nke  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(155)

我在windows上有Symfon项目。在docker中,它在端口57058:5432上运行postgres数据库。当我运行symfony console make:migration时,我得到了这个:

In ExceptionConverter.php line 77:

  An exception occurred in the driver: SQLSTATE[08006] [7] could not connect to server: Connection refused (0x0000274D/10061)  
        Is the server running on host "127.0.0.1" and accepting                                                                      
        TCP/IP connections on port 30000?                                                                                            

In Exception.php line 28:
                                                                                          
  SQLSTATE[08006] [7] could not connect to server: Connection refused (0x0000274D/10061)  
        Is the server running on host "127.0.0.1" and accepting                                 
        TCP/IP connections on port 30000?                                                       

In Driver.php line 35:

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

make:migrationa

运行symfony var:export时,

DATABASE_PORT=30000 DATABASE_USERNAME=main DATABASE_PASSWORD=main PGPORT=30000 PGPASSWORD=main SYMFONY_TUNNEL_ENV=1 SYMFONY_DOCKER_ENV=1 DATABASE_VERSION=15 DATABASE_NAME=main DATABASE_URL=postgres://main:[email protected]:30000/main?s
slmode=disable&charset=utf8&serverVersion=15 DATABASE_DATABASE=main DATABASE_DRIVER=postgres PGUSER=main DATABASE_SERVER=postgres://127.0.0.1:30000 SYMFONY_TUNNEL=main DATABASE_HOST=127.0.0.1 PGDATABASE=main DATABASE_USER=main PGHOS
T=127.0.0.1

为什么有30000端口?在pgadmin中我可以在端口5432上连接db
我的文件:.env

APP_ENV=dev
APP_SECRET=50232f36c947f58b7a7ca24b159c795e

DATABASE_URL="postgresql://127.0.0.1:5432/postgres?serverVersion=15&charset=utf8"

docker-compose.yml

version: '3'

services:
  database:
    image: postgres:${POSTGRES_VERSION:-15}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      # You should definitely change the password in production
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    volumes:
      - database_data:/var/lib/postgresql/data:rw

volumes:
  database_data:

docker-compose.override.yml

version: '3'

services:
###> doctrine/doctrine-bundle ###
  database:
    ports:
      - "5432"
###< doctrine/doctrine-bundle ###
q5iwbnjs

q5iwbnjs1#

我猜你用的是Symfony吧?
下面是我的docker-compose.yml文件:

version: '3'

services:
###> doctrine/doctrine-bundle ###
  database:
    image: postgres:${POSTGRES_VERSION:-15}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      # You should definitely change the password in production
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    volumes:
      - database_data:/var/lib/postgresql/data:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###

volumes:
###> doctrine/doctrine-bundle ###
  database_data:
###< doctrine/doctrine-bundle ###

我的docker-compose.override.yml文件:

version: '3'

services:
###> doctrine/doctrine-bundle ###
  database:
    ports:
      - "5432"
###< doctrine/doctrine-bundle ###

###> symfony/mailer ###
  mailer:
    image: schickling/mailcatcher
    ports: ["1025", "1080"]
###< symfony/mailer ###

我的.env.local文件:

DATABASE_URL="postgresql://app:!Chang[email protected]:5432/app?serverVersion=15&charset=utf8"

解决方案

在我的docker-compose.override.yml文件中设置一个HOST_PORT解决了这个问题:

###> doctrine/doctrine-bundle ###
   database:
     ports:
-      - "5432"
+      - "5432:5432"
 ###< doctrine/doctrine-bundle ###
 
 ###> symfony/mailer ###
   mailer:
     image: schickling/mailcatcher
-    ports: ["1025", "1080"]
+    ports: ["1025:1025", "1080:1080"]
 ###< symfony/mailer ###

解释

有两种语法可以指定端口号:HOST_PORT:CONTAINER_PORTCONTAINER_PORT
HOST_PORT是可以从容器外部访问的端口,即主机。CONTAINER_PORT是将在容器内部打开的端口。
Docker将在这两个端口之间创建一个隧道,并转发所有传入和传出的请求和响应。
请注意,如果只指定了一个端口(CONTAINER_PORT),这是Symfony的docker-compose.override.yml的情况,Docker将随机选择HOST_PORT。要找出使用的端口,您需要列出正在运行的容器:

$ docker compose ps
NAME                     IMAGE                    COMMAND                  SERVICE             CREATED             STATUS              PORTS
symfony-p11-database-1   postgres:15-alpine       "docker-entrypoint.s…"   database            2 minutes ago       Up 2 minutes        0.0.0.0:32768->5432/tcp, :::32768->5432/tcp
symfony-p11-mailer-1     schickling/mailcatcher   "sh -c 'mailcatcher …"   mailer              2 minutes ago       Up 2 minutes        0.0.0.0:32770->1025/tcp, :::32770->1025/tcp, 0.0.0.0:32769->1080/tcp, :::32769->1080/tcp
$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES
e7852b394913   postgres:15-alpine       "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:32768->5432/tcp, :::32768->5432/tcp                                                symfony-p11-database-1
c339ca9944b4   schickling/mailcatcher   "sh -c 'mailcatcher …"   2 minutes ago   Up 2 minutes   0.0.0.0:32770->1025/tcp, :::32770->1025/tcp, 0.0.0.0:32769->1080/tcp, :::32769->1080/tcp   symfony-p11-mailer-1

这里可以看到postgres容器的HOST_PORT32768CONTAINER_PORT5432
因此,您可以调整您的.env.local如下:

- DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=15&charset=utf8"
+ DATABASE_URL="postgresql://app:!Change[email protected]:32768/app?serverVersion=15&charset=utf8"

有关端口号的详细信息,请参阅https://docs.docker.com/compose/networking/

相关问题