Home  >  Article  >  PHP Framework  >  How to connect multiple databases in yii2

How to connect multiple databases in yii2

王林
王林Original
2020-03-11 14:17:203922browse

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',
],

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!

Statement:
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