简体   繁体   English

本地服务器无法使用 PHP 连接到远程 postgresql 数据库,但本地终端可以连接

[英]local server can't connect to remote postgresql database using PHP, but local terminal can connect

The title might make this seem like a duplicate of some existing questions here on SO, but its kind of not in a way, in my opinion.标题可能会使这看起来像是 SO 上一些现有问题的重复,但在我看来,这在某种程度上不是。

8140863 , 30617357 , 27749691 8140863 , 30617357 , 27749691

But I can't for the life of me make the answers to these threads work, instead of asking questions on those threads, I would like to create my own question, I'd like to discuss my setup and then my problem, and then what I've tried但是我一生都无法使这些线程的答案起作用,而不是在这些线程上提问,我想创建自己的问题,我想讨论我的设置,然后讨论我的问题,然后我试过的

My server setup is this, I have a local(an actual physical machine) and a VPS I'm renting, both are running at Centos7.x, PostgreSQL10, Php 7.x, and Python-2.x, BOTH can ping and ssh each other through a VPN just fine.我的服务器设置是这样的,我有一个本地(一台实际的物理机器)和一个我租用的 VPS,两者都在 Centos7.x、PostgreSQL10、 Php 7.x 和 Python-2.x 上运行,两者都可以 ping 和ssh 对方通过VPN就好了。

Both can connect to their own respective databases just fine through PHP, Python, PSQL Terminal两者都可以通过 PHP、Python、PSQL 终端很好地连接到各自的数据库

On my VPS Server, I can connect to my Local Server's PostgreSQL database using Python(psycopg2), PHP(php-pgsql) and Terminal(psql) totally fine.在我的 VPS 服务器上,我可以完全使用 Python(psycopg2)、PHP(php-pgsql) 和 Terminal(psql) 连接到本地服务器的 PostgreSQL 数据库。

On my local server, I can connect to my VPS PostgreSQL using Python(psycopg2) and Terminal(psql) EXCEPT through PHP(php-pgsql)在我的本地服务器上,我可以使用 Python(psycopg2)和终端(psql)连接到我的 VPS PostgreSQL,除了 PHP(php-pgsql)

Whenever I connect to VPS's PostgreSQL through PHP, I'm getting this error like everyone else每当我通过 PHP 连接到 VPS 的 PostgreSQL 时,我都会像其他人一样收到此错误

pg_connect(): Unable to connect to PostgreSQL server: 
could not connect to server: Permission denied 
Is the server running on host "123.456.0.789" and accepting TCP/IP connections on port 5432?

Here are what I've done.这是我所做的。

  1. On postgresql.conf set listen_address = "*" -- this is OK在 postgresql.conf 设置 listen_address = "*" -- 这没关系
  2. Added pg_hba entries for both -- this is OK (python and terminal can connect on both)为两者添加了 pg_hba 条目——这没关系(python 和终端都可以连接)
  3. Port 5432/tcp is both enabled and allowed -- this is OK端口 5432/tcp 已启用和允许 - 这没关系
  4. Selinux is disabled -- this is the answers for the thread referenced above, but it doesn't work for me even after reboot of course. Selinux 已被禁用——这是上面引用的线程的答案,但即使在重新启动后它对我也不起作用。

these are the result of my netstat这些是我的 netstat 的结果

#netstat -na | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN
tcp6       0     94 ::1:55110               ::1:5432                ESTABLISHED
tcp6       0      0 ::1:5432                ::1:55060               ESTABLISHED
tcp6       0      0 ::1:5432                ::1:55110               ESTABLISHED
tcp6      12      0 ::1:5432                ::1:55108               ESTABLISHED
tcp6       0     12 ::1:55108               ::1:5432                ESTABLISHED
tcp6       0      0 ::1:55060               ::1:5432                ESTABLISHED
unix  2      [ ACC ]     STREAM     LISTENING     31102    /var/run/postgresql/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     31104    /tmp/.s.PGSQL.5432

iptables iptables

#iptables-save | grep 5432
-A IN_public_allow -p tcp -m tcp --dport 5432 -m conntrack --ctstate NEW,UNTRACKED -j ACCEPT

getenforce强制执行

#getenforce    
Disabled

Selinux Selinux

#/usr/sbin/sestatus | grep SELinux
SELinux status:                 disabled
#sudo setsebool -P httpd_can_network_connect_db 1
setsebool:  SELinux is disabled.

postgresql.conf postgresql.conf

listen_addresses = '*'                           
port = 5432   

pg_hba.conf (i just changed the ip for posting here) I tried using trust , but its still the same pg_hba.conf(我刚刚更改了 ip 以便在此处发布)我尝试使用trust ,但它仍然相同

# "local" is for Unix domain socket connections only
local   all             all                                     password
# IPv4 local connections:
host    all             all             127.0.0.1/32            password
host    all             all             123.456.0.1/32            password
host    all             all             123.456.0.11/32           password
host    all             all             123.456.0.20/32           password
# IPv6 local connections:
host    all             all             ::1/128                 password

edit start: My PHP pg_connect code (I just changed the IP just for this post, but it points to the VPS IP)编辑开始:我的 PHP pg_connect 代码(我只是为这篇文章更改了 IP,但它指向 VPS IP)

$pgcon = pg_connect("dbname=database1 user=some_user password=some_password host=123.456.0.789");

edit end: ==编辑结束:==

I would like to emphasize that BOTH servers can connect on each others and their own PostgreSQL database through other means, with the exception of my Local Server, it couldn't connect to VPS PostgreSQL using PHP.我想强调的是,两台服务器可以通过其他方式相互连接以及他们自己的 PostgreSQL 数据库,除了我的本地服务器,它无法使用 PHP 连接到 VPS PostgreSQL。

VPS To Self(localhost connection) VPS To Self(本地主机连接)

VPS to Self via PHP is OK
VPS to Self via Python is OK
VPS to Self via PSQL(Terminal) is OK

VPS To Local Server(Connecting using Local Server's VPN IP) VPS 到本地服务器(使用本地服务器的 VPN IP 连接)

VPS to Local Server via PHP is OK
VPS to Local Server via Python is OK
VPS to Local Server via PSQL(Terminal) is OK

Local Server to Self(localhost connection)本地服务器到自身(本地主机连接)

Local Server to Self via PHP is OK
Local Server to Self via Python is OK
Local Server to Self via PSQL(Terminal) is OK

Local Server to VPS(Connecting using VPS's IP by VPN and Public IP)本地服务器到 VPS(使用 VPS 的 IP 通过 VPN 和公共 IP 连接)

Local Server to VPS via Python is OK
Local Server to VPS via PSQL(Terminal) is OK
Local Server to VPS via PHP Is NOT OK

I'm really sorry for the long thread, I wanted to give as much information as I can.我真的很抱歉这个长线程,我想提供尽可能多的信息。

Let's split error by types.让我们按类型划分错误。 I'd made several tests with your case and what I found:我已经对您的案例进行了几次测试以及我的发现:

  1. If we mess with connection parameters (ie wrong port, or IP address) we get the error "could not connect to server: Connection refused" or "Operation timed out Is the server running on host "128.0.0.1" and accepting")如果我们弄乱了连接参数(即错误的端口,或 IP 地址),我们会收到错误“无法连接到服务器:连接被拒绝”或“操作超时服务器在主机“128.0.0.1”上运行并接受“)

  2. If we mess with credentials, we get the error "FATAL: password authentication failed for user..."如果我们弄乱凭据,我们会收到错误“致命:用户密码验证失败...”

  3. Permission denied can be thrown only if you have not permissions to open a TCP connection from your PHP library.仅当您无权从 PHP 库中打开 TCP 连接时,才能抛出 Permission denied。

Pls, double-check if you disable SElinux on your Local Server as for me, this is still the closest workaround for your problem.请仔细检查您是否像我一样在本地服务器上禁用 SElinux,这仍然是您的问题最接近的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM