PHP MySQL连接错误 - 实现重试逻辑 [英] PHP MySQL Connection error - Implementing Retry logic

查看:1133
本文介绍了PHP MySQL连接错误 - 实现重试逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们看到大量的PHP和MYSQL连接失败的问题。有人建议,我们将构建的重试逻辑,如果数据库连接不成功。我已经粘贴了以下code和错误消息。

有人能指教一下应落实在这个code中的重试逻辑的最佳方式?它处理此Any code样?

PHP的警告:mysqli_connect():[2002]连接尝试失败,因为连接的方没有正确的一段时间,或者建立后connecti响应(试图通过TCP连接:​​//美国cdbr - 蔚蓝西-a.cloudapp.net:3306)在D:\\家\\网站\\ wwwroot的\\ xdfdy.php上线232

PHP的警告:mysqli_connect()(HY000 / 2002):A连接尝试失败,因为连接的方没有正确一段时间后响应或已建立的连接失败,因为连接主机未能响应。

 公共职能connect_to_mysqldb(){
尝试
{
    $这个 - >康恩= mysqli_connect('us-cdbr-azure-west-a.cloudapp.net','XXXXX','密码','DBNAME');
}
赶上(DbException $ E){
    返回false;
}
        //检查连接
如果(mysqli_connect_errno($这个 - >康涅狄格州))
  {
  回声无法连接到MySQL:。 mysqli_connect_error();
  返回false;
  }    返回true;
}


解决方案

如果下面已经发明了车轮,这说DB连接每一个不必要的尝试可能会遇到性能问题,而且也是他们建议的原因之一人你只需要数据库的一个实例,我想告诉你不这样做,但然而,问题是问。

虽然,我从来没有这样做过,这似乎是我喜欢你可以递归调用你的函数,直到建立连接。

 公共职能connect_to_mysqldb(){
        尝试
        {
            $这个 - >康恩= mysqli_connect('us-cdbr-azure-west-a.cloudapp.net','XXXXX','密码','DBNAME');
        }
        赶上(DbException $ E){
                    //写入日志可能?
            $这个 - > connect_to_mysqldb();
        }
        //检查连接(因为catch块可能无法访问?)
        如果(mysqli_connect_errno($这个 - >康涅狄格州))
        {
            $这个 - > connect_to_mysqldb();
        }
        返回true;
    }

所以,当你的连接有错误,该方法将调用本身并返回(真),只有当如果块被跳过(连接建立)

We are seeing lot of PHP and MYSQL Connection failure issues. It has been suggested that we shall built the retry logic, if the database connection is not successful. I have pasted below the code and the error message.

Can anybody advise what shall be the best way to implement the retry logic in this code? Any code sample which handles this?

PHP Warning: mysqli_connect(): [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connecti (trying to connect via tcp://us-cdbr-azure-west-a.cloudapp.net:3306) in D:\home\site\wwwroot\xdfdy.php on line 232

PHP Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

public function connect_to_mysqldb () { 
try 
{
    $this->conn =  mysqli_connect('us-cdbr-azure-west-a.cloudapp.net','xxxxx','password', 'dbname');
} 
catch (DbException  $e) {
    return false;
}
        // Check connection
if (mysqli_connect_errno($this->conn ))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  return false;
  }

    return true;
}   

解决方案

If following the people that already invented the wheel, which say every unnecessary attempt to DB connection might run into performance issues, and also one of the reasons that they suggest you only need one instance of the database, I would tell you DO NOT DO IT, but however, the question is asked.

Although, I have never done this before, it seems for me like you can call your function recursively until establishing a connection

public function connect_to_mysqldb () { 
        try 
        {
            $this->conn =  mysqli_connect('us-cdbr-azure-west-a.cloudapp.net','xxxxx','password', 'dbname');
        } 
        catch (DbException  $e) {
                    // write into logs maybe?
            $this->connect_to_mysqldb();
        }
        // Check connection (maybe unreachable because of the catch block?)
        if (mysqli_connect_errno($this->conn ))
        {
            $this->connect_to_mysqldb();
        }
        return true;
    }

So, while your connection has errors, the method will call itself and will return (true) only when the catch and the if blocks are skipped (the connection is established)

这篇关于PHP MySQL连接错误 - 实现重试逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆