这篇文章主要介绍了关于如何设置yii数据库的长连接,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1.在配置文件设置属性
webb/config/main.php中添加persistent或者attributes属性
'components'=>array( // 数据库配置 to mysql 'dsnmybd'=>array( 'class'=>'cdbconnection', 'connectionstring' => 'mysql:host=127.0.0.1;port=3306;dbname=my_db', 'tableprefix' => 'pre_', 'emulateprepare' => true, 'username' => 'root', 'password' => '123456', 'charset' => 'utf8', 'persistent' => true, //长连接 // 'attributes' => array(pdo::attr_persistent => true), //长连接 ), ......... );
2.检测是否成功启用长连接
可以调用frameworkdbcdbconnection.php下的getpersistent()函数,返回true为启用成功,false为启用失败。
例如:/web/protected/controllers/testcontroller.php
public function actionlist() { $oques = mybd::model(); var_dump($oques->getpersistent()); }
备注:
如果使用了长连接而长期没有对数据库进行任何操作,那么在timeout值后,mysql
server就会关闭此连接,而客户端在执行查询的时候就会得到一个类似于“mysql server has gone away“这样的错误。
首先作为超级用户登录到mysql,注意必须是超级用户,否则后面会提示没有修改权限。然后输入
show global variables like 'wait_timeout';
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
在yii框架中扫描目录下文件入数据库的方法
关于yii改善并发数的性能优化的方法
以上就是如何设置yii数据库的长连接的详细内容。