旗下导航:搜·么
当前位置:网站首页 > PHP框架 > Laravel > 正文

laravel进修:主从读写星散设置的完成【laravel框架】

作者:搜搜PHP网发布时间:2019-11-27分类:Laravel浏览:116


导读:本篇文章给人人带来的内容是关于laravel进修:主从读写星散设置的完成,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。在DB的衔接工场中找到以下代码......
本篇文章给人人带来的内容是关于laravel进修:主从读写星散设置的完成,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

在DB的衔接工场中找到以下代码
.../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php

/** 
 * Get the read configuration for a read / write connection. 
 * 
 * @param  array  $config 
 * @return array 
 */  
protected function getReadConfig(array $config)  
{  
    $readConfig = $this->getReadWriteConfig($config, 'read');  
      
    return $this->mergeReadWriteConfig($config, $readConfig);  
}  
  
/** 
 * Get a read / write level configuration. 
 * 
 * @param  array   $config 
 * @param  string  $type 
 * @return array 
 */  
protected function getReadWriteConfig(array $config, $type)  
{  
    if (isset($config[$type][0])) {  
        return $config[$type][array_rand($config[$type])];  
    }  
  
    return $config[$type];  
} 

/**
 * Merge a configuration for a read / write connection.
 *
 * @param  array  $config
 * @param  array  $merge
 * @return array
 */
protected function mergeReadWriteConfig(array $config, array $merge)
{
    return array_except(array_merge($config, $merge), ['read', 'write']);
}

工场类经由过程随机猎取读DB设置来举行读取操纵,由此可推出DB的设置应当以下

'mysql' => [  
    'write'    => [  
        'host' => '192.168.1.180',  
    ],  
    'read'     => [  
        ['host' => '192.168.1.182'],  
        ['host' => '192.168.1.179'],  
    ],  
    'driver'    => 'mysql',  
    'database'  => 'database',  
    'username'  => 'root',  
    'password'  => '',  
    'charset'   => 'utf8',  
    'collation' => 'utf8_unicode_ci',  
    'prefix'    => '', 
]

加强版,支撑多主多从,支撑自力用户名和暗码,设置以下

'mysql' => [  
    'write'    => [  
        [
            'host' => '192.168.1.180',
            'username'  => '',
            'password'  => '',
        ],  
    ],  
    'read'     => [  
        [
            'host' => '192.168.1.182',
            'username'  => '',
            'password'  => '',
        ],  
        [
            'host' => '192.168.1.179',
            'username'  => '',
            'password'  => '',
        ],  
    ],  
    'driver'    => 'mysql',  
    'database'  => 'database',     
    'charset'   => 'utf8',  
    'collation' => 'utf8_unicode_ci',  
    'prefix'    => '', 
]

考证
开启MySQL的general-log,经由过程tail -f的体式格局监控log变化来肯定设置是不是见效

相干文章引荐:

Laravel的功用测试:测试驱动开辟(附代码)

以上就是laravel进修:主从读写星散设置的完成的细致内容,更多请关注ki4网别的相干文章!

标签:laravel学习