CodeIgniter configure different IP for READ and WRITE MySQL data

倖福魔咒の 提交于 2019-12-11 00:00:04

问题


My company has been setting up a different server for read and write access of mysql DB. Previously, we use custom php script. However, I was recently given task to create tools by leveraging CI. I've done it on test server. Now, I'm confused, how to implement it on live server.


回答1:


Setup multiple 'connection groups' for each server in your database.php config file, then in the Model:

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

//read from DB1
$DB1->query();

//write to DB2
$DB2->insert();

See examples in: Connecting to Multiple Databases




回答2:


Might it be easier, rather than doing this by IP, do it using MySQL users.

You could have two different users, one with all the write privileges like INSERT and UPDATE, then another user with all the READ privileges like SELECT, or what ever you wish.

Then what ever code is running on your write web server uses the write user and vice-a-versa?




回答3:


I've made a simple libary that extend database functionality. You can modify it depend with what you need. http://rangga.net/splitting-read-write-codeigniter-database/



来源:https://stackoverflow.com/questions/3633692/codeigniter-configure-different-ip-for-read-and-write-mysql-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!