Home > Article > PHP Framework > 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!