只需三步,便可实现。
第一步,在.env文件中配置
DB_HOST=localhost DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=root DB_HOST_TWO=localhost DB_DATABASE_TWO=test1 DB_USERNAME_TWO=root DB_PASSWORD_TWO=root
第二步,在config/database.php进行数据库配置
//系统默认优先使用第一个 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '192.168.0.y'), 'database' => env('DB_DATABASE', 'yyy'), 'username' => env('DB_USERNAME', 'yyyy'), 'password' => env('DB_PASSWORD', 'yyyyy'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], 'mysql_two' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_TWO', '192.168.0.x'), 'database' => env('DB_DATABASE_TWO', 'xxx'), 'username' => env('DB_USERNAME_TWO', 'xxxx'), 'password' => env('DB_PASSWORD_TWO', 'xxxxx'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],
第三步,在你需要用到的model中,进行切换
class meeting_news extends Model { //没有指定的话,默认使用 mysql protected $connection = 'mysql_two'; protected $table = 'meeting'; }
至于上面的xxx什么yyy的,线上服务自己配一下就行了