配置資料庫(Laravel風格)
webman/database 資料庫及版本支持情況如下:
- MySQL 5.6+
- PostgreSQL 9.4+
- SQLite 3.8.8+
-
SQL Server 2017+
資料庫配置文件位置為
config/database.php
。return [ // 預設資料庫 'default' => 'mysql', // 各種資料庫配置 'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => '127.0.0.1', 'port' => 3306, 'database' => 'webman', 'username' => 'webman', 'password' => '', 'unix_socket' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'pool' => [ // 連接池配置,僅支持 swoole/swow 驅動 'max_connections' => 5, // 最大連接數 'min_connections' => 1, // 最小連接數 'wait_timeout' => 3, // 從連接池獲取連接等待的最大時間,超時後會拋出異常 'idle_timeout' => 60, // 連接池中連接最大空閒時間,超時後會關閉回收,直到連接數為 min_connections 'heartbeat_interval' => 50, // 連接池心跳檢測時間,單位秒,建議小於 60 秒 ], ], 'sqlite' => [ 'driver' => 'sqlite', 'database' => '', 'prefix' => '', 'pool' => [ // 連接池配置,僅支持 swoole/swow 驅動 'max_connections' => 5, // 最大連接數 'min_connections' => 1, // 最小連接數 'wait_timeout' => 3, // 從連接池獲取連接等待的最大時間,超時後會拋出異常 'idle_timeout' => 60, // 連接池中連接最大空閒時間,超時後會關閉回收,直到連接數為 min_connections 'heartbeat_interval' => 50, // 連接池心跳檢測時間,單位秒,建議小於 60 秒 ], ], 'pgsql' => [ 'driver' => 'pgsql', 'host' => '127.0.0.1', 'port' => 5432, 'database' => 'webman', 'username' => 'webman', 'password' => '', 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', 'sslmode' => 'prefer', 'pool' => [ // 連接池配置,僅支持 swoole/swow 驅動 'max_connections' => 5, // 最大連接數 'min_connections' => 1, // 最小連接數 'wait_timeout' => 3, // 從連接池獲取連接等待的最大時間,超時後會拋出異常 'idle_timeout' => 60, // 連接池中連接最大空閒時間,超時後會關閉回收,直到連接數為 min_connections 'heartbeat_interval' => 50, // 連接池心跳檢測時間,單位秒,建議小於 60 秒 ], ], 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => 'localhost', 'port' => 1433, 'database' => 'webman', 'username' => 'webman', 'password' => '', 'charset' => 'utf8', 'prefix' => '', 'pool' => [ // 連接池配置,僅支持 swoole/swow 驅動 'max_connections' => 5, // 最大連接數 'min_connections' => 1, // 最小連接數 'wait_timeout' => 3, // 從連接池獲取連接等待的最大時間,超時後會拋出異常 'idle_timeout' => 60, // 連接池中連接最大空閒時間,超時後會關閉回收,直到連接數為 min_connections 'heartbeat_interval' => 50, // 連接池心跳檢測時間,單位秒,建議小於 60 秒 ], ], ], ];
使用多個資料庫
通過
Db::connection('配置名')
來選擇使用哪個資料庫,其中配置名
為配置文件config/database.php
中的對應配置的key
。例如如下資料庫配置:
return [
// 預設資料庫
'default' => 'mysql',
// 各種資料庫配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'webman',
'username' => 'webman',
'password' => '',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'webman2',
'username' => 'webman2',
'password' => '',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => '127.0.0.1',
'port' => 5432,
'database' => 'webman',
'username' => 'webman',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
];
像這樣切換資料庫。
// 使用預設資料庫,等價於 Db::connection('mysql')->table('users')->where('name', 'John')->first();
$users = Db::table('users')->where('name', 'John')->first();;
// 使用 mysql2
$users = Db::connection('mysql2')->table('users')->where('name', 'John')->first();
// 使用 pgsql
$users = Db::connection('pgsql')->table('users')->where('name', 'John')->first();