配置数据库(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();