一、在laravel目錄下開啟.env檔案 修改為如下
1、MSSQL資料庫
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=password
DB_HOST_SYSTEM=172.146.278.253
DB_PORT_SYSTEM=1433
DB_DATABASE_SYSTEM=databaseSystem
DB_USERNAME_SYSTEM=rootSystem
DB_PASSWORD_SYSTEM=passwordSystem
2、MYSQL資料庫
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=password
DB_HOST_SYSTEM=172.146.278.253
DB_PORT_SYSTEM=3306
DB_DATABASE_SYSTEM=databaseSystem
DB_USERNAME_SYSTEM=rootSystem
DB_PASSWORD_SYSTEM=passwordSystem
二、在laravel/config目錄下 配置 \config\database.php 檔案
1、MSSQL資料庫
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
'sqlsrv_system' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_system', '172.146.278.253'),
'port' => env('DB_PORT_system', '1433'),
'database' => env('DB_DATABASE_system', 'forge'),
'username' => env('DB_USERNAME_system', 'forge'),
'password' => env('DB_PASSWORD_system', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
2、MYSQL資料庫
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
'mysql_system' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_system', '172.146.278.253'),
'port' => env('DB_PORT_system', '3306'),
'database' => env('DB_DATABASE_system', 'forge'),
'username' => env('DB_USERNAME_system', 'forge'),
'password' => env('DB_PASSWORD_system', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
三、呼叫
1、MSSQL資料庫
$users = DB::connection('sqlsrv_system')->select(...);
2、MYSQL資料庫
$users = DB::connection('mysql_system')->select(...);
本作品採用《CC 協議》,轉載必須註明作者和本文連結