本篇文章給大家帶來的內容是關于laravel學習:主從讀寫分離配置的實作,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助,
在DB的連接工廠中找到以下代碼
.../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/**
* 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']);
}
|



鏈接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取碼:x2p5
免費分享,但是X度限制嚴重,如若鏈接失效點擊鏈接或搜索加群 群號518475424,
工廠類通過隨機獲取讀DB配置來進行讀取操作,由此可推出DB的配置應該如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
'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' => '',
]
|
加強版,支持多主多從,支持獨立用戶名和密碼,配置如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
'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變化來確定配置是否生效
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/122671.html
標籤:PHP
上一篇:java集合到底是用來干什么用的
下一篇:Runtime.getRuntime().exec()