Database Configuration (Laravel Style)

The database and version support for webman/database is as follows:

  • MySQL 5.6+
  • PostgreSQL 9.4+
  • SQLite 3.8.8+
  • SQL Server 2017+

The database configuration file is located at config/database.php.

 return [
     // Default database
     'default' => 'mysql',
     // Various database configurations
     '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' => [ // Connection pool configuration, supports only swoole/swow drivers
                'max_connections' => 5, // Maximum number of connections
                'min_connections' => 1, // Minimum number of connections
                'wait_timeout' => 3,    // Maximum wait time for obtaining a connection from the pool, an exception will be thrown after timeout
                'idle_timeout' => 60,   // Maximum idle time for connections in the pool, will close and recycle after timeout, until the number of connections is min_connections
                'heartbeat_interval' => 50, // Connection pool heartbeat detection time, in seconds, recommended to be less than 60 seconds
            ],
         ],

         'sqlite' => [
             'driver'   => 'sqlite',
             'database' => '',
             'prefix'   => '',
             'pool' => [ // Connection pool configuration, supports only swoole/swow drivers
                'max_connections' => 5, // Maximum number of connections
                'min_connections' => 1, // Minimum number of connections
                'wait_timeout' => 3,    // Maximum wait time for obtaining a connection from the pool, an exception will be thrown after timeout
                'idle_timeout' => 60,   // Maximum idle time for connections in the pool, will close and recycle after timeout, until the number of connections is min_connections
                'heartbeat_interval' => 50, // Connection pool heartbeat detection time, in seconds, recommended to be less than 60 seconds
            ],
         ],

         'pgsql' => [
             'driver'   => 'pgsql',
             'host'     => '127.0.0.1',
             'port'     => 5432,
             'database' => 'webman',
             'username' => 'webman',
             'password' => '',
             'charset'  => 'utf8',
             'prefix'   => '',
             'schema'   => 'public',
             'sslmode'  => 'prefer',
             'pool' => [ // Connection pool configuration, supports only swoole/swow drivers
                'max_connections' => 5, // Maximum number of connections
                'min_connections' => 1, // Minimum number of connections
                'wait_timeout' => 3,    // Maximum wait time for obtaining a connection from the pool, an exception will be thrown after timeout
                'idle_timeout' => 60,   // Maximum idle time for connections in the pool, will close and recycle after timeout, until the number of connections is min_connections
                'heartbeat_interval' => 50, // Connection pool heartbeat detection time, in seconds, recommended to be less than 60 seconds
            ],
         ],

         'sqlsrv' => [
             'driver'   => 'sqlsrv',
             'host'     => 'localhost',
             'port'     => 1433,
             'database' => 'webman',
             'username' => 'webman',
             'password' => '',
             'charset'  => 'utf8',
             'prefix'   => '',
             'pool' => [ // Connection pool configuration, supports only swoole/swow drivers
                'max_connections' => 5, // Maximum number of connections
                'min_connections' => 1, // Minimum number of connections
                'wait_timeout' => 3,    // Maximum wait time for obtaining a connection from the pool, an exception will be thrown after timeout
                'idle_timeout' => 60,   // Maximum idle time for connections in the pool, will close and recycle after timeout, until the number of connections is min_connections
                'heartbeat_interval' => 50, // Connection pool heartbeat detection time, in seconds, recommended to be less than 60 seconds
            ],
         ],
     ],
 ];

Using Multiple Databases

Use Db::connection('config_name') to select which database to use, where config_name is the key of the corresponding configuration in the config/database.php configuration file.

For example, the database configuration as follows:

 return [
     // Default database
     'default' => 'mysql',
     // Various database configurations
     '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',
          ],
 ];

Switch databases like this.

// Use the default database, equivalent to Db::connection('mysql')->table('users')->where('name', 'John')->first();
$users = Db::table('users')->where('name', 'John')->first(); 
// Using mysql2
$users = Db::connection('mysql2')->table('users')->where('name', 'John')->first();
// Using pgsql
$users = Db::connection('pgsql')->table('users')->where('name', 'John')->first();