如何在Laravel中划分读/写数据库设置?

时间:2022-08-08 00:03:15

This is recommended regulation for database divide setting in Laravel.

这是Laravel数据库划分设置的推荐规则。

'mysql' => [
    'read' => [
                  'host' => '192.168.1.1',
              ],
   'write' => [
                  'host' => '196.168.1.2'
              ],
      'sticky'    => true,
      'driver'    => 'mysql',
      'database'  => 'database',
      'username'  => 'root',
      'password'  => '',
      'charset'   => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix'    => '',
  ],  

But if I want to use different access information as DB_NAME, USER_ID, PASS etc to each read/write databases, then how could I make it? Thank you.

但是,如果我想使用不同的访问信息,如DB_NAME、USER_ID、传递给每个读/写数据库等等,那么我怎么做呢?谢谢你!

2 个解决方案

#1


1  

You can create two connections then specify the connection with Eloquent using the on method:

您可以创建两个连接,然后使用on方法指定连接:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST2', '127.0.0.1'),
            'port' => env('DB_PORT2', '3306'),
            'database' => env('DB_DATABASE2', 'forge'),
            'username' => env('DB_USERNAME2', 'forge'),
            'password' => env('DB_PASSWORD2', ''),
            'unix_socket' => env('DB_SOCKET2', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],


User::on('mysql2')->where('id', $id)->update($data);


Or using Query Builder:

或者使用查询构建器:

DB::connection('mysql2')->table('users')->where('id', $id)->update($data);

#2


1  

You can declare another db conecction like this:

你可以这样声明另一个db函数:

'mysql' => [
    'read' => [
                  'host' => '192.168.1.1',
              ],
   'write' => [
                  'host' => '192.168.1.1'
              ],
      'sticky'    => true,
      'driver'    => 'mysql',
      'database'  => 'database',
      'username'  => 'root',
      'password'  => '',
      'charset'   => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix'    => '',
], 

'writecon' => [
    'read' => [
        'host' => '196.168.1.2',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver' => 'mysql',
    'port' => env('DB2_PORT', '3306'),
    'database' => env('DB2_DATABASE', 'db2'),
    'username' => env('DB2_USERNAME', 'somename'),
    'password' => env('DB2_PASSWORD', 'somepass'),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
],

Then in your .env add:

然后在你的。env中补充:

DB2_PORT=3306
DB2_DATABASE=db2
DB2_USERNAME=somename
DB2_PASSWORD=somepass

And you can use it like this:

你可以这样使用它:

$someModel->setConnection('writecon');
$someModel->save();

not tested but you can try it.

没有经过测试,但你可以试试。

#1


1  

You can create two connections then specify the connection with Eloquent using the on method:

您可以创建两个连接,然后使用on方法指定连接:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST2', '127.0.0.1'),
            'port' => env('DB_PORT2', '3306'),
            'database' => env('DB_DATABASE2', 'forge'),
            'username' => env('DB_USERNAME2', 'forge'),
            'password' => env('DB_PASSWORD2', ''),
            'unix_socket' => env('DB_SOCKET2', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],


User::on('mysql2')->where('id', $id)->update($data);


Or using Query Builder:

或者使用查询构建器:

DB::connection('mysql2')->table('users')->where('id', $id)->update($data);

#2


1  

You can declare another db conecction like this:

你可以这样声明另一个db函数:

'mysql' => [
    'read' => [
                  'host' => '192.168.1.1',
              ],
   'write' => [
                  'host' => '192.168.1.1'
              ],
      'sticky'    => true,
      'driver'    => 'mysql',
      'database'  => 'database',
      'username'  => 'root',
      'password'  => '',
      'charset'   => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix'    => '',
], 

'writecon' => [
    'read' => [
        'host' => '196.168.1.2',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver' => 'mysql',
    'port' => env('DB2_PORT', '3306'),
    'database' => env('DB2_DATABASE', 'db2'),
    'username' => env('DB2_USERNAME', 'somename'),
    'password' => env('DB2_PASSWORD', 'somepass'),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
],

Then in your .env add:

然后在你的。env中补充:

DB2_PORT=3306
DB2_DATABASE=db2
DB2_USERNAME=somename
DB2_PASSWORD=somepass

And you can use it like this:

你可以这样使用它:

$someModel->setConnection('writecon');
$someModel->save();

not tested but you can try it.

没有经过测试,但你可以试试。