[PHP] Larval 主从读写分离配置
progpark 2018-04-20 11:03:10 浏览287 评论0摘要: 在DB的连接工厂中找到以下代码 .../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php /** * Get the read configuration for a read / write connection.
在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变化来确定配置是否生效
用云栖社区APP,舒服~
【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口 详情请点击 评论 (0) 点赞 (0) 收藏 (0)相关文章
- [PHP] Larval 主从读写分离配置
- thinkphp 3.2分布式数据库读写分离扩展阅读
- Amoeba-mysql主从+读写分离实战+测试+排错
- phalapi-进阶篇5(数据库读写分离以及多库使用)
- Mysql的主从复制的读写分离之Amoeba实现
- 网站系统架构梳理-解决高负载高并发
- MySQL数据库主从同步实现
- 想玩集群?读写分离?你要先懂这个!
- 网站Web业务架构从小到大演变
- MySQL主从复制+mysql-proxy读写分离