【问题标题】:CakePHP cannot connect to Postgres. Pgadmin4 can howeverCakePHP 无法连接到 Postgres。然而 Pgadmin4 可以
【发布时间】:2017-04-25 18:42:40
【问题描述】:

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

我的设置如下:

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

错误:

CakePHP 无法连接到数据库。

无法建立与数据库的连接:SQLSTATE[08006] [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

phpinfo PostgreSQL 相关数据

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

【问题讨论】:

  • 显示你的数据库配置DATABASE_CONFIG类。
  • 第 1 步 - 在数据源设置中检查您的凭据并使用与命令行相同的凭据。
  • 我正在使用与我的工作站上使用的 PgAdmin 相同的工作凭据。
  • 你好像用的是Redhat,试试这个。默认情况下,Apache 被阻止访问数据库服务器 - access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/…
  • 其实我用的是基于 RHEL 的 CentOs7... 还适用吗?

标签: php postgresql cakephp


【解决方案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

【讨论】:

  • 奇数。我不得不在虚拟机上执行此操作两次,并且它们都在重新启动时收到以下消息:无法加载 SELinux 策略。为了解决这个问题,我需要在启动 VM 之前设置 selinux=0...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 2020-05-23
  • 2020-05-05
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多