如果主数据库宕机,Codeigniter 会切换到辅助数据库

发布于 2024-12-10 19:10:36 字数 60 浏览 0 评论 0原文

我想知道是否有办法处理 mysql 错误并配置 ci 在主服务器关闭(无法连接)时切换到辅助数据库服务器?

i'm wondering if there's a way to handle mysql error and configure ci to switch to a secondary DB server in the event the primary server is down (unable to connect) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-12-17 19:10:36

好吧,我不知道这是否有效,但你实际上可以尝试这个:

1)创建 2 组数据库设置(在 application/config/database.php 中):

// regular one..
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
//...

// second connection
$db['second']['hostname'] = 'localhost';
$db['second']['username'] = 'root';
//...

2)设置 deubg 关闭以避免显示数据库错误并实际杀死你的脚本(对两者都这样做):

$db['default']['db_debug'] = FALSE;

3)你可以在加载库时将 TRUE 传递给第二个参数,以便它实际上有一个返回值;它返回数据库对象本身:

$dbobject1 = $this->load->database('default',TRUE);
$dbobject2 = $this->load->database('second',TRUE);

现在,您可以检查“连接 ID”资源以查看是否建立了连接:

if(FALSE === $dbobject1->conn_id)
{
  echo 'No connection established!';
}

现在您可以决定加载另一个数据库,以防第一个数据库未加载。缺点是,您实际上并不知道为什么数据库连接不起作用...

至于如何实现这一点,您可能想尝试扩展数据库类,或者更好的是创建您自己的库,实际上只是检查连接是否存在,然后加载它而不是数据库。由于它返回一个数据库对象(除了所有 2 个连接都失败时),因此您可以像处理普通数据库类一样处理该对象:

class Check_db {

     private $CI = '';
     public $DB1 = '';
     public $DB2 = '';

     function __construct()
     {
        $this->CI =&get_instance();
        $this->DB1 = $this->CI->load->database('default',TRUE);
        if(FALSE !== $this->DB1->conn_id)
        {
          return $this->DB1;
        }
        else
        {
          $this->DB2 = $this->CI->load->database('second',TRUE);
          if(FALSE !== $this->DB2->conn_id)
          {
            return $this->DB2;
          }
          else
          {
            return FALSE;
          }
        }
      }

Well, I don't know if this is going to work, but you can actually try this:

1) create 2 groups of database settings (in application/config/database.php):

// regular one..
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
//...

// second connection
$db['second']['hostname'] = 'localhost';
$db['second']['username'] = 'root';
//...

2) Set deubg off to avoid showing db errors and actually killing your script (do it for both):

$db['default']['db_debug'] = FALSE;

3) You can pass a TRUE to the second paramenter while loading the library, so that it actually has a return value; it returns the database object itself:

$dbobject1 = $this->load->database('default',TRUE);
$dbobject2 = $this->load->database('second',TRUE);

Now, you can just check for the "connection ID" resource to see if a connection was established or not:

if(FALSE === $dbobject1->conn_id)
{
  echo 'No connection established!';
}

Now you can decide to load another DB in case the first doesn't load. The downside is, you don't know actually why the db connection didn't work, though...

As for how to implement this, you might want to try extending the database class or, better, create you own library which in fact just checks for whether a connection exists or not, and load this instead of the database library. Since it returns a database object (apart when all 2 connections fail), you can then work on that as you would do on the normal database class:

class Check_db {

     private $CI = '';
     public $DB1 = '';
     public $DB2 = '';

     function __construct()
     {
        $this->CI =&get_instance();
        $this->DB1 = $this->CI->load->database('default',TRUE);
        if(FALSE !== $this->DB1->conn_id)
        {
          return $this->DB1;
        }
        else
        {
          $this->DB2 = $this->CI->load->database('second',TRUE);
          if(FALSE !== $this->DB2->conn_id)
          {
            return $this->DB2;
          }
          else
          {
            return FALSE;
          }
        }
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文