CodeIgniter 多个数据库持久连接?
我在 CodeIgniter 应用程序中使用多个数据库,并且阅读了大量内容,认为应该关闭持久连接。
为什么建议采取此措施?在最新版本 2.0.2 中是否仍然有必要?
我正在做类似的事情
$db2 = $this->load->database("dbname", TRUE);
I'm using multiple database in my CodeIgniter application and have been reading a lot that persistent connections should be turned off.
Why is this measure recommended and is this still necessary in the newest version, 2.0.2?
I'm doing things like
$db2 = $this->load->database("dbname", TRUE);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Code Igniter 文档没有解释
不幸的是,2.0.2 的 Code Igniter 文档没有解释为什么应该关闭它们。它只是说明有一个设置可以这样做。这样做的原因很可能是因为它实际上不是 Code Igniter 功能,而是更多的底层 PHP/MySQL 功能。 PHP 在其文档中有一个关于持久连接的非常好且详细的页面。
服务器管理员(我)的解释
基本上,这取决于性能。如果您有少量用户位于加利福尼亚州,并且您的 MySQL 数据库服务器位于瑞士,则连接到 MySQL 服务器将会产生大量开销(与在加利福尼亚州同一服务器上连接到 MySQL 相比) )。拥有持久连接将避免每次您想要使用 MySQL 查找内容时重新连接的开销。还有许多其他问题可能会导致连接开销,例如服务器或数据库的配置方式。 除了性能之外,如果连接关闭不当,它还会导致问题,从而导致表锁定。
为什么建议关闭它
此设置对于大多数服务器来说并不理想的原因是,如果您有超过 60 个用户同时使用您的数据库,或者您的 MySQL 配置设置了最大连接数,您将很快达到 最大连接数,这可能会导致数据库错误或服务器崩溃等。
Code Igniter Documentation Doesn't Explain
Unfortunately, the Code Igniter documentation for 2.0.2 does not explain why they should be turned off. It merely explains that there is a setting to do so. The reasoning for this is most likely because it really isn't a Code Igniter functionality but more of an underlying PHP/MySQL functionality. PHP has a very good and detailed page in their documentation about persistent connections.
The Explanation From a Server Admin (me)
Basically, it comes down to performance. If you have a small number of users that are located in California and your MySQL database server is in Switzerland, there will be a substantial overhead to make a connection to the MySQL server (when compared to a connection to MySQL on the same server in California). Having a persistent connection will prevent the overhead of re-connecting every time you want to look something up with MySQL. There's also many other issues that can cause connection overhead, such as the way the server or database is configured. Besides performance, it can also cause issues if a connection closes improperly, leading to locked tables.
Why It's Recommended to be Turned Off
The reason this setting is not ideal for most servers is that if you have more than 60 users using your database simultaneously, or what ever your MySQL configuration has set as the maximum number of connections, you will quickly reach the maximum number of connections and this could cause database errors or server crashes etc.
这是默认数据库: $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'mysqli', 'dbprefix' => '', 'pconnect' => TRUE, 'db_debug' => = '生产'), 'cache_on' => '', 'char_set' => 'utf8', 'dbcollat' => ' ', '加密' => FALSE, '压缩' => FALSE, '故障转移' => array(), 'save_queries' => TRUE在database.php文件底部添加另一个数据库 $db['second'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', '密码' => '', '数据库' => 'mysqli', 'dbprefix' => '', 'db_debug' = > (ENVIRONMENT !== '生产'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => swap_pre' => '', '加密' => FALSE, 'stricton' => FALSE, 'failover' => TRUE ) ;在 autoload.php 配置文件中 $autoload['libraries'] = array('database', 'email', 'session');默认数据库通过自动加载数据库库可以正常工作,但第二个数据库通过使用模型和控制器中的构造函数加载和连接... db2 = $this->load->database('second', TRUE); } public function getsecondUsers(){ $query = $this->db2->get('members');返回 $query->result(); } } ?>
This is default database : $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'mydatabase', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => TRUE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); Add another database at the bottom of database.php file $db['second'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'mysecond', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => TRUE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); In autoload.php config file $autoload['libraries'] = array('database', 'email', 'session'); The default database is worked fine by autoload the database library but second database load and connect by using constructor in model and controller... db2 = $this->load->database('second', TRUE); } public function getsecondUsers(){ $query = $this->db2->get('members'); return $query->result(); } } ?>