如果主要关闭,Codeigniter切换到辅助数据库(Codeigniter switch to secondary database if primary is down)

编程入门 行业动态 更新时间:2024-10-27 01:33:21
如果主要关闭,Codeigniter切换到辅助数据库(Codeigniter switch to secondary database if primary is down)

我想知道是否有办法处理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) ?

最满意答案

那么,我不知道这是否会起作用,但你可以试试这个:

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; } } }

更多推荐

本文发布于:2023-07-26 05:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:切换到   数据库   Codeigniter   switch   primary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!