php - 如果服务器关闭,Codeigniter 备用数据库连接

标签 php codeigniter

我想在 Codeigniter 中使用原始 php 代码作为备用数据库连接,我的原始 php 代码是:

$db = mysql_connect('remote_server', 'username', 'password'); if(!$db){ 
 $db = mysql_connect('localhost', 'username', 'password') ; }

如果远程 mysql 服务器由于某种原因关闭,则连接到备份服务器。

有人用 CodeIgniter 做过这种事吗?如果是这样,您介意分享代码或想法吗?

最佳答案

更新: 刚刚找到了更好的方法。

假设你在database.php中有2个数据库配置,一个是默认的,另一个是备份的 即

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'temp_test1'; 
//.......
$db['backup']=$db['default'];
$db['backup']['hostname'] = 'localhost1';
$db['backup']['username'] = 'root';
$db['backup']['password'] = '';
$db['backup']['database'] = 'temp_test1'; 

现在,将其添加到database.php文件的末尾

//check to see if you can connect
$conn=@mysql_connect($db['default']['hostname'],$db['default']['username'],$db['default']['password']);
if($conn) //check to see if it's connecting, if it is close this connection
{
    mysql_close($conn);
}
else{ //if it isnt
    $db['default']=$db['backup']; //replace the default credentials with the backup credentials
}



旧帖子: 您可以采取很多方法。

您可以通过此 mysql_ping() 检查特定连接是否打开,即

$conn=mysql_connect(...);
if(mysql_ping($conn)){...};

因此您可以使用此方法来决定选择哪个数据库。

对于 codeigniter,一种方法(我想说这是一种相当糟糕的方法,但仍然是一种方法)是弄乱系统文件。在DB_Driver中,这部分代码中:

 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
    if ( ! $this->conn_id)
            {
                log_message('error', 'Unable to connect to the database');

                if ($this->db_debug)
                {
                    $this->display_error('db_unable_to_connect');
                }
                return FALSE;
            }

是它尝试连接并检查连接是否成功的地方,如果不成功则给出错误。

我不确定你如何在 CI 中进行异常处理,但基本上你应该处理异常并连接到不同的数据库。

由于我不知道异常处理,假设我创建了一个database_backup.php 文件,其中包含配置文件夹主机名、用户名、密码和数据库变量。然后我将代码更改为这样

$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
if ( ! $this->conn_id) //oops, first connection failed
{
    //no problem, change the credentials of the database to our backup credentials
    $ci=&get_instance();
    $ci->load->config('database_backup');
    $this->username=$ci->config->item('username');
    $this->password=$ci->config->item('password');
    $this->database=$ci->config->item('database');
    $this->hostname=$ci->config->item('hostname');
     //try to connect to database once more
    $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();

    // No connection resource STILL?nothing we can do now,  throw an error

    if ( ! $this->conn_id)
    {
        log_message('error', 'Unable to connect to the database');

            if ($this->db_debug)
            {
                $this->display_error('db_unable_to_connect');
            }
            return FALSE;
    }
}

关于php - 如果服务器关闭,Codeigniter 备用数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329212/

相关文章:

php - 卡在 Paypal 集成中

php - 如果列表中存在特定变量或单词,如何使用 PHP 隐藏 smth

php - 如何在 codeigniter 中为 paypal 支付网关获取购物车值

php - 使用 codeigniter 进行子查询

php - codeigniter 检查每个 Controller 中的用户 session

javascript - 单击“确定”后显示警报消息并重定向

php - 将特殊字符(即元音变音)转换为最可能的 ascii 表示形式

php - Symfony + Doctrine - 定义完整性约束错误时的错误信息

php - 如何使用从 mysql_fetch_array 获取的数据在列中插入行?

php - codeigniter hello world 在 Ubuntu 上失败