我想在我的系统中创建多个数据库。大多数时候数据库是MySQL;但是它可能在将来有所不同,即Admin可以生成这样的报告,这是使用异构数据库系统的源。
display: none
答案 0 :(得分:1)
您需要像这样在In .env和config / database.php中添加两个数据库
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=secret
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=secret
In config/database.php
config\database.php
'mysql' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'mysql2' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
],
答案 1 :(得分:0)
我发现这篇文章解释了如何在Laravel中使用两个不同的数据库(完全可以与Eloquent模型一起使用):http://fideloper.com/laravel-multiple-database-connections