如果主数据库宕机,Codeigniter 会切换到辅助数据库
我想知道是否有办法处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我不知道这是否有效,但你实际上可以尝试这个:
1)创建 2 组数据库设置(在 application/config/database.php 中):
2)设置 deubg 关闭以避免显示数据库错误并实际杀死你的脚本(对两者都这样做):
3)你可以在加载库时将 TRUE 传递给第二个参数,以便它实际上有一个返回值;它返回数据库对象本身:
现在,您可以检查“连接 ID”资源以查看是否建立了连接:
现在您可以决定加载另一个数据库,以防第一个数据库未加载。缺点是,您实际上并不知道为什么数据库连接不起作用...
至于如何实现这一点,您可能想尝试扩展数据库类,或者更好的是创建您自己的库,实际上只是检查连接是否存在,然后加载它而不是数据库。由于它返回一个数据库对象(除了所有 2 个连接都失败时),因此您可以像处理普通数据库类一样处理该对象:
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):
2) Set deubg off to avoid showing db errors and actually killing your script (do it for both):
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:
Now, you can just check for the "connection ID" resource to see if a connection was established or not:
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: