CakePHP无法连接到Postgres。但是Pgadmin4可以

时间:2016-12-10 11:02:06

标签: php postgresql cakephp

CakePHP3无法连接到我的PostgreSQL数据库。

我的设置如下:

  • Windows 10主机
  • CentOS7 Virtualbox VM来宾
    • PostgreSQL 9.6
    • Apache 2.4
    • PHP 7

错误:

CakePHP无法连接到数据库。

无法建立与数据库的连接:SQLSTATE [7]无法连接到服务器:权限被拒绝服务器是否在主机上运行" localhost" (:1)并接受端口5432上的TCP / IP连接?无法连接到服务器:权限被拒绝服务器是否在主机上运行" localhost" (127.0.0.1)并接受端口5432上的TCP / IP连接?

我尝试在app.php中指定localhost127.0.0.1192.168.2.31作为我的主机值。他们都给出了同样的错误。

一切都没有丢失,我可以通过以下方式访问PostgreSQL:

  • 使用Pgadmin4
  • 通过我的工作站连接服务器
  • 通过SSH连接,我可以通过使用psql连接

[me@localhost ~]$ psql -U my_user -h 127.0.0.1 -d my_db

更新#1

默认数据库配置

'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Postgres',
            'persistent' => true,
            'host' => 'localhost',
            /**
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 5432,
            'username' => 'user',
            'password' => 'pass',
            'database' => 'my_db',
            'schema' => 'public',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

            /**
             * Set identifier quoting to true if you are using reserved words or
             * special characters in your table or column names. Enabling this
             * setting will result in queries built using the Query Builder having
             * identifiers quoted when creating SQL. It should be noted that this
             * decreases performance because each query needs to be traversed and
             * manipulated before being executed.
             */
            'quoteIdentifiers' => false,

            /**
             * During development, if using MySQL < 5.6, uncommenting the
             * following line could boost the speed at which schema metadata is
             * fetched from the database. It can also be set directly with the
             * mysql configuration directive 'innodb_stats_on_metadata = 0'
             * which is the recommended value in production environments
             */
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

            //'url' => env('DATABASE_URL', null),
        ]

更新#2

有关PostgreSQL的

phpinfo数据

PDO
PDO support enabled
PDO drivers     mysql, pgsql, sqlite 

pdo_pgsql
PDO Driver for PostgreSQL   enabled
PostgreSQL(libpq) Version   9.2.15
Module version  7.0.13
Revision    $Id: f9b0c62eba234361d62f16fcbaaa120353ab5175 $ 

pgsql
PostgreSQL Support  enabled
PostgreSQL(libpq) Version   9.2.15
PostgreSQL(libpq)   PostgreSQL 9.2.15 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4), 64-bit
Multibyte character support     enabled
SSL support     enabled
Active Persistent Links     0
Active Links    0
Directive   Local Value Master Value
pgsql.allow_persistent  On  On
pgsql.auto_reset_persistent Off Off
pgsql.ignore_notice Off Off
pgsql.log_notice    Off Off
pgsql.max_links Unlimited   Unlimited
pgsql.max_persistent    Unlimited   Unlimited

更新#3

运行tcpdump时: tcpdump -i lo port 5432 -w pg.cap

我使用psql时收到了一些数据包。但是,当我通过CakePHP访问我的网站时,我没有收到任何数据包。

pg_hba.conf

host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5
host    all             all             127.0.0.1/32            md5

1 个答案:

答案 0 :(得分:1)

在基于RedHat的Linux发行版中,有一个配置值httpd_can_network_connect_db,可以启用/禁用Apache连接到数据库服务器。在默认状态下,此值设置为off,这意味着Appache 不允许连接。

检查你的盒子是否允许:

getsebool httpd_can_network_connect_db

如果它返回off,你应该允许它:

setsebool -P httpd_can_network_connect_db on

-P使此设置在重新启动后保持不变)

有关详细信息,请参阅SELinux Booleans⁠Configuring Booleans