How to set up a long connection for yii database

不言
Release: 2023-04-02 22:36:01
Original
2797 people have browsed it

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), //长连接 ), ......... );
Copy after login

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()); }
Copy after login

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';
    Copy after login

. 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!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!