Home> PHP Framework> YII> body text

How to connect multiple databases in yii2

王林
Release: 2020-03-11 14:17:20
Original
3844 people have browsed it

How to connect multiple databases in yii2

A project will require connecting to multiple databases as needed. So how to connect multiple databases in yii2? It's actually very simple and can be completed with a little configuration in the configuration file.

The configuration method is as follows:

Open the database configuration file common\config\main-local.php, add db2 under the original db configuration item, and configure the properties of the second database.

Example:

'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=hyii2', //数据库hyii2 'username' => 'root', 'password' => 'pwhyii2', 'charset' => 'utf8', ], 'db2' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=hyii', //数据库hyii 'username' => 'root', 'password' => 'pwhyii', 'charset' => 'utf8', ],
Copy after login

The above configuration can complete the function of yii2 connecting to multiple databases.

Related tutorial recommendations:yii framework

The above is the detailed content of How to connect multiple databases in yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!