데이터베이스 구성(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, // 연결 풀 내 연결의 최대 유휴 시간, 시간 초과 시 닫혀 수집됩니다. 최소 연결 수로 돌아갈 때까지. 'heartbeat_interval' => 50, // 연결 풀의 하트비트 검사 시간, 초 단위, 60초 이하가 권장됩니다. ], ], 'sqlite' => [ 'driver' => 'sqlite', 'database' => '', 'prefix' => '', 'pool' => [ // 연결 풀 구성, swoole/swow 드라이버만 지원 'max_connections' => 5, // 최대 연결 수 'min_connections' => 1, // 최소 연결 수 'wait_timeout' => 3, // 연결 풀에서 연결을 얻기 위해 기다리는 최대 시간, 시간 초과 시 예외가 발생합니다. 'idle_timeout' => 60, // 연결 풀 내 연결의 최대 유휴 시간, 시간 초과 시 닫혀 수집됩니다. 최소 연결 수로 돌아갈 때까지. '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, // 연결 풀 내 연결의 최대 유휴 시간, 시간 초과 시 닫혀 수집됩니다. 최소 연결 수로 돌아갈 때까지. '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, // 연결 풀 내 연결의 최대 유휴 시간, 시간 초과 시 닫혀 수집됩니다. 최소 연결 수로 돌아갈 때까지. '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();