doulu4233 2017-12-09 15:35
浏览 53
已采纳

如果第一个失败,我可以连接到第二个数据库主机吗?

I have a question about a connection to a second host if the first fail down or connection limit reached.

My Host is OVH in France on a shared host (I can't modify apache/php/sql config) and the MAX USER_CONNECTIONS (sql) is 30.

I wanted to switch Host is the first one reach the limit and refuse connection.

Can I use ?:

$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'dbuser';
$dbpswd = 'dbpass';
try{
    $db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname,$dbuser,$dbpswd,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}catch(PDOexception $e){
    $dbhost2 = 'secondhost';
    $dbname2 = 'dbname';
    $dbuser2 = 'dbuser';
    $dbpswd2 = 'dbpass';
    try{
        $db = new PDO('mysql:host='.$dbhost2.';dbname='.$dbname2,$dbuser2,$dbpswd2,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    }catch(PDOexception $e){
        die("DB Error please contact the web admin.");
    }
}

Or if you have a better option..

NB: I do not have much experience in programming, I do not use OOP yet.

  • 写回答

1条回答 默认 最新

  • douzi8127 2017-12-09 16:17
    关注

    The idea is right, but this particular implementation is inflexible and unhelpful towards the site admin who is supposed to fix errors.

    Imagine a case when a password has been changed for the main server. Your code will start connecting to the backup server 100% of time, thanks to the unconditional algorithm, and nobody would have an idea on that.

    Given you want to overcome a particular error ("too many connections"), in your catch block you should test whether it is the error you are expecting to, and at least log the error message otherwise. So the site ad min will notice a problem and will have an idea what's going on.

    die("DB Error please contact the web admin."); won't be of a much use for the site admin either, as he won't have an idea what happened with the backup server in turn.

    So for the second one do not catch its exception right in place, but follow the common error handling routine for your site. The easiest one would be a global try catch around the whole code, which looks rather blunt but at least better than several dozens local try catches with its own error message each

    So it should be like

    $dboptions = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
    
    try{
        $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8mb4",$dbuser,$dbpswd,$dboptions);
    }catch(PDOexception $e){
        if ($e->getMessage() != "whatever particular error message you get") {
            error_log($e);
        }
        $dbhost2 = 'secondhost';
        $dbname2 = 'dbname';
        $dbuser2 = 'dbuser';
        $dbpswd2 = 'dbpass';
        $db = new PDO("mysql:host=$dbhost2;dbname=$dbname;charset=utf8mb4",$dbuser,$dbpswd,$dboptions);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?