PHP 程序无法连接到 CentOS 上的PostgreSQL,但是在 CentOS 服务器上却能正常运行 psql,

操作如下:多次重启 PG 数据库后发现 CGI 脚本无法连接数据库,但是可以使用 psql 连接上数据库

PHP 程序报错如下:

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

网上查了很多文章,发现是SELinux(Linux上的强制访问控制安全模块)惹得祸,很想关闭它,

但是据说他是系统被攻破后的最后一道防线,那就暂时记录此类问题的解决方案,以后有时间的话,好好研究一下这个SELinux!

此问题的原因是SELinux阻止httpd进程--httpd_can_network_connect_db 连接数据库--不管是哪种类型的数据库

所以需要确保设置正确的布尔值以允许Web应用程序与数据库通信,使用setsebool命令改变该布尔变量的状态,从而使得httpd进程能够访问数据库服务器:

sudo setsebool -P httpd_can_network_connect_db 

改变成功后,可以使用getsebool命令进行查看:

/usr/sbin/getsebool httpd_can_network_connect_db

查看 SELinux 设置的所有 Booleans 值

getsebool -a

上述状态改变只是暂时性的,一旦系统重启,该变量状态将改变回初始状态,因此,可以使用如下命令永久性改变状态:

/usr/sbin/setsebool -P httpd_can_network_connect_db on

如需关闭SELinux则可以执行以下操作
修改/etc/selinux/config SELINUX=enforcing 为 SELINUX=disabled

参考链接:

https://stackoverflow.com/questions/27749691/php-cant-connect-to-postgresql-on-centos-7

https://unix.stackexchange.com/questions/8854/how-do-i-configure-selinux-to-allow-outbound-connections-from-a-cgi-script

关于 SELinux  的参考链接:

https://wiki.centos.org/TipsAndTricks/SelinuxBooleans#line-44

http://www.blogjava.net/ycyk168/archive/2012/09/09/387340.html

postgresql connection failure:SQLSTATE[08006] [7] could not connect to server: Permission denied Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?的更多相关文章

  1. JDBC 连Sql Server 接数据库--The TCP/IP connection to the host localhost, port 1433 has failed

    原文:https://blog.csdn.net/qq_39241986/article/details/80848855 这样的错误,你有遇到过吗? The TCP/IP connection to ...

  2. 已经安装了客户端,但是cmd输入sqlcmd报错:Sqlcmd:Error:Connection failure.SQL Native Client is not installed correctly

    以前安装了sqlserver2008,没有卸载掉,后面又安装了sqlserver2014,所以系统环境变量中既有2008的环境变量的配置,又有2014的环境变量的配置,所以在终端输入sqlcmd时报错 ...

  3. ADO Connection failure

    使用ado连接,频繁报错.为何?是网络的问题吗?太灵敏了.Connection failure. 跟大文本又关系??

  4. Linux 出现telnet: 127.0.0.1: Connection refused错误解决办法

    Linux 出现telnet: connect to address 127.0.0.1: Connection refused错误解决办法 没有xinetd服务: 1./etc/init.d目录中放 ...

  5. ERROR [RMI TCP Connection(3)-127.0.0.1] - init datasource error

    运行报错 ERROR [RMI TCP Connection(3)-127.0.0.1] - init datasource error, url: jdbc:mysql://localhost:33 ...

  6. Linux用sendmail发信失败,提示Connection refused by [127.0.0.1]

    现象: Linux用sendmail发信失败,提示Connection refused by [127.0.0.1] 29 14:10:44 iZ257p7xxilZ sendmail[3395]: ...

  7. android异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused

    android手机做下载文件时,报了如下异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused 模拟器 ...

  8. telnet: connect to address 127.0.0.1: Connection refused

    telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...

  9. com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect

      问题描述:最简单的数据库连接报错,到主机  的 TCP/IP 连接失败.(win 7 操作系统) 错误信息: com.microsoft.sqlserver.jdbc.SQLServerExcep ...

随机推荐

  1. 学习Rust Book之写Cargo配置文件

    不知道为什么这个文件就是编译不过 [package] name = "hello_world" version = "0.0.1" author = " ...

  2. 【wiki】红帽linux

    Red Hat Enterprise Linux From Wikipedia, the free encyclopedia wiki 上面红帽的版本信息. https://en.wikipedia. ...

  3. CVE-2018-4878

    0x00前言 该漏洞影响 Flash Player 版本28.0.0.137以及之前的所有版本 0x01 poc Poc 这里只列出关键代码 public function triggeruaf() ...

  4. SpringBoot学习2:springboot整合servlet

    整合方式1:通过注解扫描完成 Servlet 组件的注册 1.编写servlet package com.bjsxt.servlet; import javax.servlet.ServletExce ...

  5. ElasticSearch High Level REST API【2】搜索查询

    如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...

  6. 浅谈JavaScript字符串拼接

    本文给大家汇总介绍了几种javascript中字符串拼接的方法,十分的简单实用,有需要的小伙伴可以参考下. 在JavaScript中会经常遇到字符串拼接,但是如果要拼接的字符串过长就比较麻烦了. 如果 ...

  7. phpspider案例

    phpspider案例 <?php require './autoload.php'; use phpspider\core\phpspider; /* Do NOT delete this c ...

  8. 【css】如何实现响应式布局

    “自适应网页设计”到底是怎么做到的?其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name="viewport" content=&qu ...

  9. MySQL批量插入大量数据方法

    在MySQL数据库中,如果要插入上百万级的记录,用普通的insert into来操作非常不现实,速度慢人力成本高,推荐使用Load Data或存储过程来导入数据,我总结了一些方法分享如下,主要基于My ...

  10. day 63 Django基础九之中间件

    Django基础九之中间件   本节目录 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证 六 xxx 七 xxx 八 xxx 一 前戏 我们在前面的课程中已经学 ...