Laravel - 如何对从数据库进行数据库连接

Laravel - How to do DB Connection to Slave Database

正如上面的标题,我需要连接到从数据库(由于某些原因)...

但是我找不到怎么办?

.

下面是我的数据库配置:

'mysql' => [
    'read' => [
        'host' => env('DB_SLAVE', '127.0.0.1'),
        'port' => env('DB_SLAVE_PORT', '3306'),
        'username'  => env('DB_SLAVE_USERNAME', 'root'),
        'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
    ],
    'write' => [
        'host' => env('DB_MASTER', '127.0.0.1'),
        'port' => env('DB_MASTER_PORT', '3308'),
        'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
        'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
    ],
    'driver'    => 'mysql',
    'database'  => 'amazingapp',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'aa_',
    'strict' => false,
    'engine' => env('DB_ENGINE', 'InnoDB'),
    'unix_socket' => env('DB_SOCKET', ''),
],  

.

要做slave DB的连接,目前我想的是DB::connection('mysql.slave');

但是没用..

你的意思是在database.php里面新建数据库连接入口,然后通过DB::connection('mysql2')统计方法连接?

也许你可以检查这个link

.

可以试试这个配置

'mysql' => [
    'read' => [
        'host' => env('DB_SLAVE', '127.0.0.1'),
        'port' => env('DB_SLAVE_PORT', '3306'),
        'username'  => env('DB_SLAVE_USERNAME', 'root'),
        'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
    ],
    'write' => [
        'host' => env('DB_MASTER', '127.0.0.1'),
        'port' => env('DB_MASTER_PORT', '3308'),
        'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
        'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
    ],
    'driver'    => 'mysql',
    'database'  => 'amazingapp',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'aa_',
    'strict' => false,
    'engine' => env('DB_ENGINE', 'InnoDB'),
    'unix_socket' => env('DB_SOCKET', ''),
],  

'mysql_master' => [
    'host' => env('DB_MASTER', '127.0.0.1'),
    'port' => env('DB_MASTER_PORT', '3308'),
    'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
    'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
    'driver'    => 'mysql',
    'database'  => 'amazingapp',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'aa_',
    'strict' => false,
    'engine' => env('DB_ENGINE', 'InnoDB'),
    'unix_socket' => env('DB_SOCKET', ''),
],  

'mysql_slave' => [
    'host' => env('DB_SLAVE', '127.0.0.1'),
    'port' => env('DB_SLAVE_PORT', '3306'),
    'username'  => env('DB_SLAVE_USERNAME', 'slaveuser'),
    'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
    'driver'    => 'mysql',
    'database'  => 'amazingapp',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'aa_',
    'strict' => false,
    'engine' => env('DB_ENGINE', 'InnoDB'),
    'unix_socket' => env('DB_SOCKET', ''),
],  

.

所以,如果想连接到slave,只需使用DB::connection('mysql_slave');

您可以在您的 .env 上进行多个 DB 连接,而无需触摸您的 database.php

这就是我连接多个数据库的方式

 DB_CONNECTION=mysql
 DB_HOST=localhost
 DB_PORT=3306
 DB_DATABASE=sample
 DB_USERNAME=root
 DB_PASSWORD=123456


 DB_CONNECTION=mysql1
 DB_HOST=localhost
 DB_PORT=3306
 DB_DATABASE=sample
 DB_USERNAME=root
 DB_PASSWORD=123456

以及当您想按照 protrafree

所述在您的代码中调用它时

像这样

  DB::connection('mysql')

   DB::connection('mysql1')