PDO 连接从命令行工作,而不是通过 Apache?

PDO connection works from command line, but not through Apache?(PDO 连接从命令行工作,而不是通过 Apache?)
本文介绍了PDO 连接从命令行工作,而不是通过 Apache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个非常简单的测试脚本:

I have a very simple test script:

<?php

$DSN = "mysql:host=db.example.edu;port=3306;dbname=search_data";

try {

    $DB = new PDO($DSN, "username", "super-secret-password!");

} catch (PDOException $e) {

    header('Content-Type: text/plain');
    print "Could not connect to database, rawr. :-(";
    exit;

}


$SQL = "SELECT phrase FROM search ORDER BY RAND() LIMIT 10";

foreach($DB->query($SQL) as $row){

    print $row['phrase']."
";

}

?>

当我从命令行执行此脚本时,它运行良好:

When I execute this script from the command line, it works perfectly:

$ php test.php
corporal punishment
Stretches
voluntary agencies and the resettlement of refugees
music and learning
Nike Tiger Woods Scandal
Hermeneia
PSYCHINFO
anthony bourdain
Black-White Couples and their Social Worlds
colonization, hodge

但是当我通过网络浏览器访问完全相同的脚本时,它说:

But when I access the exact same script through my web browser, it says:

Could not connect to database, rawr. :-(

我已经尝试了 var_dump 解决错误,消息是:SQLSTATE[HY000] [2003] 无法连接到 MySQL 服务器上的 'db.example.edu' (13)".

I've tried var_dump on the error, and the message is: "SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'db.example.edu' (13)".

这令人费解.这是完全相同的服务器上的完全相同的脚本——为什么当我从命令行执行它时它可以工作,但当 Apache 执行它时却失败?

This is puzzling. It's the exact same script on the exact same server -- why does it work when I execute it from the command line, but fail when Apache executes it?

推荐答案

如果这是运行 SELinux(或任何使用 SELinux 的任何非 Red Hat 衍生产品)的 Red Hat 衍生发行版(RHEL、CentOS、Fedora、ScientificLinux),则默认撰写本文时的策略设置是禁止 Apache 与其他服务器或数据库建立外部连接.作为 root,您必须启用以下两个 SELinux 布尔值.使用 -P 选项在重新启动后保持更改.

If this is a Red Hat-derived distribution (RHEL, CentOS, Fedora, ScientificLinux) running SELinux (or any non Red Hat derivative using SELinux), the default policy setting at time of this writing is to prohibit Apache from making external connections to other servers or databases. As root, you must enable the following two SELinux booleans. Use the -P option to persist the change across a reboot.

setsebool -P httpd_can_network_connect=1
setsebool -P httpd_can_network_connect_db=1

请注意,httpd_can_network_connect 可能不是必需的.首先尝试仅打开 httpd_can_network_connect_db.

Note that httpd_can_network_connect may not be necessary. Try it first turning on only httpd_can_network_connect_db.

这篇关于PDO 连接从命令行工作,而不是通过 Apache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)