Symfony 4 + Postgresql:即使我更改了doctrine.yaml和.env中的值,服务器仍然尝试连接到“ localhost”数据库

问题描述

我最近在一个运行该应用程序的虚拟机上部署了我的symfony应用程序,该虚拟机已连接到运行Postgresql服务器的主机。我正在尝试更改symfony应用程序中的数据库URL(我从将env中的DATABASE_URL更改为

pdo_pgsql://postgres:*password here*@192.168.122.1:5432/postgres?serverVersion=11&charset=utf8

但是,在服务器启动并访问页面时,出现错误

      The Web server is using PHP CGI 7.3.21                                                                            
      http://127.0.0.1:8000                                                                                             
                                                                                                                        

[Web Server/PHP ] Aug 18 14:03:53 |ERROR| PHP    Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in driver: sqlSTATE[08006] [7] Could not connect to server: Connection refused 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP       Is the server running on host "localhost" (::1) and accepting 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP       TCP/IP connections on port 5432? 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP    Could not connect to server: Connection refused 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP       Is the server running on host "localhost" (127.0.0.1) and accepting 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP       TCP/IP connections on port 5432?" at /symfony/dynamic_form_generator/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgresqlDriver.PHP line 73 
[Web Server/PHP ] Aug 18 14:03:53 |INFO | PHP     
[Web Server/PHP ] Aug 18 14:03:53 |ERROR| SERVER POST (500) /login host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"

这清楚地表明,无论出于何种原因,Doctrine / PHP dba东西都试图连接到localhost:5432。我在网上查询过,提到的唯一文件是.env,doctrine.yaml和parameters.yml(我的项目结构中甚至没有)

我已经清除了两次缓存,但是大多数时候我尝试清除缓存时都会收到错误消息

[root@localhost dynamic_form_generator]# PHP bin/console cache:clear
*
 // Clearing the cache for the prod environment with debug false                                                        

 ! [NOTE] For better performances,you should move the cache and log directories to a non-shared folder of the VM.      


In Filesystem.PHP line 176:
                                                                                                                                                                             
  Failed to remove directory "/symfony/dynamic_form_generator/var/cache/prod/doctrine": rmdir(/symfony/dynamic_form_generator/var/cache/prod/doctrine): Directory not empty  
                                                                                                                                                                             

cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

所以我必须手动删除提到的目录,然后重试,此操作成功,但是在服务器运行时,它仍然表明服务器正在尝试运行到localhost:5432。我该怎么办?

这些是我上面提到的参数文件

Doctrine.yaml :(我将URL更改为“手写”值,因为我感到绝望,如果我拥有'%env(resolve:DATABASE_URL)%',它也会执行相同的操作)

doctrine:

    dbal:
        driver: 'pdo_pgsql'
        charset: utf8
        url: pdo_pgsql://postgres:* password here *@127.0.0.1:5432/postgres?serverVersion=11&charset=utf8
        user:     "%env(resolve:DB_USERNAME)%"
        password: "%env(resolve:DB_PASSWORD)%"

        # IMPORTANT: You MUST configure your server version,# either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
~                                                                                         

和.env :(重要的部分,其余就是评论

###> symfony/framework-bundle ###
APP_ENV=prod
APP_SECRET=8cce45f8d9c607e2a1a371349617970c
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16/192.168.122.1
#TRUSTED_HOSTS='^(localhost|example\.com)$'
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an sqlite database,use: "sqlite:///%kernel.project_dir%/var/data.db"
# For a Postgresql database,use: "postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
# IMPORTANT: You MUST configure your server version,either here or in config/packages/doctrine.yaml
DATABASE_URL=pdo_pgsql://postgres:* password here *@192.168.122.1:5432/postgres?serverVersion=11&charset=utf8
DB_USERNAME=postgres
DB_CONNECTION=pgsql
DB_HOST=192.168.122.1
DB_DATABASE=postgres
DB_PASSWORD= * password here *
###< doctrine/doctrine-bundle ###

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1|192\.168\.122\.1)(:[0-9]+)?$
###< nelmio/cors-bundle ###
~                                 
    这里的
  • password *应该是密码所在的位置。我在做什么错了?

编辑:修复了粘贴文件内容中的小错误

解决方法

整天搞乱配置后,我尝试运行 php bin/console debug:config doctrine | grep localhost来查看如果该学说覆盖了我的设置,那是什么问题,实际上似乎存在一个隐藏的参数“ host:localhost”。 因此,我要做的就是返回到yaml文件,并添加“ host:192.168.122.1”行

所以看起来像这样:


    dbal:
        driver: 'pdo_pgsql'
        charset: utf8
        url: pdo_pgsql://postgres:* password here *@127.0.0.1:5432/postgres?serverVersion=11&charset=utf8
        user:     "%env(resolve:DB_USERNAME)%"
        password: "%env(resolve:DB_PASSWORD)%"
        host: 192.168.122.1

我希望这对任何有此问题的人都能解决!