[PHP] Larval 主从读写分离配置
2023-09-14 08:57:58 时间
在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变化来确定配置是否生效
PHP 使用数据库的并发问题 在秒杀,抢购等并发场景下,可能会出现超卖的现象; 如:我们一共只有100个商品,在最后一刻,我们已经消耗了99个商品,仅剩最后一个。这个时候,系统发来多个并发请求,这批请求读取到的商品余量都是1个,然后都通过了这一个余量判断,最终导致超发。
相关文章
- 使用PHP + Apache访问有错误的php脚本时不报错
- PHP安装模式cgi、fastcgi、php_mod比较
- lnmp架构下php安全配置分享
- 手机号码归属地查询php函数
- PHP 性能优化 - php.ini 配置
- PHP如何安装和配置Zend Studio
- yii2:如果获取config/web.php配置的值?
- Redhat Server 5.7 安装配置PHP
- 以源码编译的方式安装PHP与php-fpm
- PHP中获取当前页面的完整URL
- php:在linux上用sudo提升权限(centos 8 / PHP 7.4.2)
- centos8上配置openresty/nginx可访问php
- 使用thrift进行跨语言调用(php c# java)
- PHP7中php.ini、php-fpm和www.conf的配置
- PHP面试题集
- PHP和JAVASCRIPT判断访客终端是电脑还是手机
- jquery与php的HTML转义与反转义
- Eclipse的PHP插件PHPEclipse安装和使用
- [PHP] 如何让 php-fpm 的循环 echo 实时输出到浏览器
- [PHP]PHP的session机制,配置与高级应用
- PHP数据结构之——链表
- Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
- paip.php-gtk 桌面程序 helloworld总结
- PHP 文件上传
- PHP面试题:你所知道的php数组相关的函数?
- CentOS下yum安装PHP,配置php-fpm服务
- php知识点-1
- linux 配置 Apache mysql php最新版
- rabbitmq php 学习
- 【整理】Linux下中文检索引擎coreseek4安装,以及PHP使用sphinx的三种方式(sphinxapi,sphinx的php扩展,SphinxSe作为mysql存储引擎)
- 【Deepin 20系统】Debian系统VScode配置运行PHP环境