【问题标题】:Codeigniter alternate database connection if server down如果服务器关闭,Codeigniter 备用数据库连接
【发布时间】:2012-11-11 06:55:18
【问题描述】:

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

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

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

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

【问题讨论】:

标签: php codeigniter


【解决方案1】:

更新: 刚刚想出了一个更好的方法。

假设你在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;
    }
}

【讨论】:

    【解决方案2】:

    看看system/database/drivers/mysql/mysql_driver.php

    找到函数function db_connect() [或function db_pconnect(),取决于你使用的是哪一个]

    有连接代码:

    return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
    

    更改逻辑以满足您的需要。

    顺便说一句,最好使用PDO 驱动程序,因为默认情况下,codeigniter 使用其折旧过程开始的mysql_*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      相关资源
      最近更新 更多