php - 错误:[8] PDO::__construct():发送 5 个字节失败,errno=3

我不断随机收到以下错误:

Error: [8] PDO::__construct(): send of 5 bytes failed with errno=32 Broken pipe

在/var/www/test/includes/classes/class.Database.php 第 33 行

一切都很好,直到我将 Maria DB 升级到 10.2.xx 服务器设置是:

PHP 7.0.27-0+deb9u1 (cli) (built: Jan  5 2018 13:51:52) ( NTS )
mysql  Ver 15.1 Distrib 10.2.13-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

我尝试增加 wait_timeoutmax_allowed_pa​​cket 我还启用了 pdo_mysql 但似乎没有任何区别。 有谁知道哪里会出问题? 我怀疑 Maria DB 从 10.1.xx 升级到 10.2.xx,因为在升级之前我没有看到这样的错误,即使我从 Ubuntu 16.04 切换到 Debian 9。

public function __construct($host, $user,$pass, $dbname, $charset, $prefix){
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
    $this->dbname = $dbname;
    $this->charset = $charset;
    $this->prefix = $prefix;

    // Set DSN
    $dsn = 'mysql:host='.$this->host.';dbname='.$this->dbname.';charset='.$this->charset;
    // Set options
    $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => true
    );
    // Create a new PDO instanace
    try{
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); //this is line 33
    }
    // Catch and log all errors
    catch(PDOException $e){
        $this->error = $e->getMessage();
        error_log($e->getMessage());
    }
}

最佳答案

有了这样的错误消息,很难确定问题的确切原因。但我们确实知道一件事,它无法连接。

最可能的原因是它没有可用的连接并且与 PHP 结合通常是由持久连接引起的。

与普通连接不同,持久连接在您关闭它们时关闭。对于长进程很有用,因此服务器不需要每次都重新连接到数据库,这样您就不需要构建某种检查数据库是否仍处于连接状态。然而,这意味着您需要存放 handle 并在完成后自行关闭,以避免过度耗尽泳池。

当脚本执行完毕后,正常的连接会返回到连接池中。它们仍然打开(如果长时间不使用则关闭)并重新用于下一个请求。由于 PHP 构建在 HTTP 协议(protocol)之上,在大多数情况下请求不会花费超过一秒的时间来完成,因此使用任何持久性连接都没有什么意义。

所以除非你有一个很长的过程,否则可能一个持久连接的套接字服务器被完全使用。

所以只需禁用它或删除以下行:

PDO::ATTR_PERSISTENT => true,

或者.. 如果仍想使用持久连接,请更改数据库 configuration允许更多连接,但这很可能会延长问题再次出现之前的时间。

关于php - 错误:[8] PDO::__construct():发送 5 个字节失败,errno=32 Broken pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210184/

相关文章:

java - 强制执行以避免循环依赖

">javascript - 在 Firefox 中隐藏 <input type ="date">

javascript - 错误类型错误 : Cannot read property 'tagNam

tensorflow - 如何在 tensorflow playground 的第 4 个数据集上实

java - 如何更改 azure-functions-maven-plugin 的日志记录级别?

android - 获取屏幕曲线、角和切口的物理形状

python-sphinx - 如何在 python-sphinx 中突出显示文本?

javascript - 子方法未定义 vue.js

react-native - react 导航 : stack navigator with tab

python - 如何在 git post-receive hook 中指定 python 解释器?