This article mainly introduces how to set up the long connection of Yii database, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
1. Set properties in the configuration file
webb/config/main.php Add persistent or attributes 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. Check whether the persistent connection is successfully enabled
You can call the getPersistent() function under frameworkdbCDbConnection.php and return true to indicate successful activation. , false means activation failed.
For example:/web/protected/controllers/TestController.php
public function actionList() { $oQues = MyBD::model(); var_dump($oQues->getPersistent()); }
Note:
If a long connection is used and the database is not logged in for a long time Any operation, then after the timeout value, the mysql
server will close the connection, and the client will get an error similar to "MySQL server has gone away" when executing the query.
First log in to MYSQL as a super user. Note that you must be a super user, otherwise you will be prompted later that you do not have modification permissions. Then enter
show global variables like 'wait_timeout';
. The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to scan files in a directory and enter them into the database in the yii framework
The above is the detailed content of How to set up a long connection for yii database. For more information, please follow other related articles on the PHP Chinese website!