2016-07-27 45 views
1

我使用Laravel的照明數據庫庫與laravel之外的人員mongodb。使用mysql路由器照亮數據庫連接

我的要求是通過照明數據庫的多個數據庫連接。

目前,我已經添加了兩個連接一個mysql和一個mongodb。

要拆分數據庫負載,我需要直接連接到mysql路由器而不是mysql db服務器。另外,我希望只有一個用於讀取操作,一個用於讀取/寫入操作。

請幫助我解決這個問題。

在此先感謝。

當前連接

$db = new Capsule; 

$db->addConnection([ 
    'driver' => 'mysql', 
    'host'  => '127.0.0.1', 
    'database' => 'test', 
    'username' => 'test', 
    'password' => '[email protected]#', 
    'charset' => 'utf8', 
    'collation' => 'utf8_unicode_ci', 
    'prefix' => '', 
], "default"); 

$db->addConnection([ 
    'driver' => 'mongodb', 
    'host'  => '127.0.0.1', 
    'port'  => 27017, 
    'database' => 'test', 
    'username' => null, 
    'password' => null, 
    'options' => [] 
], "mongodb"); 

$db->getDatabaseManager()->extend('mongodb', function ($config) { 
    return new Connection($config); 
}); 

$db->setEventDispatcher(new Dispatcher(new Container)); 
$db->setAsGlobal(); 
$db->bootEloquent(); 

我需要更換通過mysql的路由器進行讀取和讀/寫操作兩個MySQL連接一個MySQL連接。

回答

1

您可以定義讀/要麼MySQL的主機MySQL的路由器的主機和端口

$db->addConnection([ 
    'driver' => 'mysql', 
    'read'  => [ 
     'host'  => '<mysql_router_host_ip>', 
     'port'  => '<mysql_router_host_port>' 
    ], 
    'write'  => [ 
     'host'  => '<mysql_router_host_ip>', 
     'port'  => '<mysql_router_host_port>' 
    ], 
    'database' => '<mysql_database>', 
    'username' => '<mysql_database_user>', 
    'password' => '<mysql_database_password>', 
    'charset' => 'utf8', 
    'collation' => 'utf8_unicode_ci', 
    'prefix' => '', 
], "mysql"); 
選項分開來寫