CakePHP3无法连接到我的PostgreSQL数据库。
我的设置如下:
错误:
CakePHP无法连接到数据库。
无法建立与数据库的连接:SQLSTATE [7]无法连接到服务器:权限被拒绝服务器是否在主机上运行" localhost" (:1)并接受端口5432上的TCP / IP连接?无法连接到服务器:权限被拒绝服务器是否在主机上运行" localhost" (127.0.0.1)并接受端口5432上的TCP / IP连接?
我尝试在app.php中指定localhost
,127.0.0.1
和192.168.2.31
作为我的主机值。他们都给出了同样的错误。
一切都没有丢失,我可以通过以下方式访问PostgreSQL:
[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
答案 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。