php读写分离是什么意思,php mysql读写分离
header("Content-Type:text/html; charset=utf8");
/**
* MySQL读写分离类的实现
* $db_config = array(
* 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>'stat'),
* 'slave' => array(
* array('host'=>'localhost:3307','user'=>'admin','passwd'=>'123456','db'=>'stat'),
* array('host'=>'localhost:3308','user'=>'admin','passwd'=>'123456','db'=>'stat')
* )
* );
*
* 注释:如果slave有多个时随机连接其中的一个
*/
$db_config = array(
'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>'stat'),
'slave' => array(
array('host'=>'localhost:3307','user'=>'admin','passwd'=>'123456','db'=>'stat'),
array('host'=>'localhost:3308','user'=>'admin','passwd'=>'123456','db'=>'stat')
)
);
$db = MySQL::getInstance('','r-w');
$sql ="SELECT call_id,caller,callee,call_time,call_money,`date`,media_money,record_money FROM `success_bill` WHERE user_id='1' AND `date` BETWEEN 1427081035 AND 1427089577 LIMIT 0,15 ";
$start_time = microtime(true);
$rs = $db->query($sql);
while ($row = $db->fetch($rs)){
echo "".$row['call_id']." ".$row['caller']." ".$row['callee']." ".$row['call_time']." ".$row['call_money']." ".$row['date']." ".$row['media_money']." ".$row['record_money']."
";
}
// var_dump($db->get($rs)) ;
$end_time = microtime(true);
echo "总耗时 ", (($end_time - $start_time)*1000), " MS\n";
echo "峰值内存 ", round(memory_get_peak_usage()/1000), " KB\n";
echo "
";
class MySQL
{
private static $_instance = null;//数据库连接实例
private static $_master = null;//主数据库连接实例
private static $_slave = null;//重数据库连接实例
public $_config = array();//数据库连接配置信息
public $_res = null;//查询实例句柄
public $_flag = '';//标识当前语句是在主还是从数据库上执行
public $_link = null;
/**
* 单实例
* Enter description here ...
* @param unknown_type $dbname
* @param unknown_type $mode
*/
public static function & getInstance($dbname='',$mode='rw'){
if (is_null(self::$_instance)){
self::$_instance = new self();
self::$_instance->__getConf();
self::$_instance->connect($dbname,$mode);
}
return self::$_instance;
}
/**
* 获取数据库配置信息
* Enter description here ...
*/
public function __getConf(){
global $db_config;
$this->_config['master'] = $db_config['master'];
$this->_config['slave'] = $db_config['slave'];
}
/**
* 数据库连接
* Enter description here ...
* @param $dbname 指定连接的数据库名,默认情况下连接配置文件的库
* @param $mode rw表示连接主库,r-w表示读写分离
*/
public function connect($dbname='',$mode = 'rw'){
if($mode == 'rw'){
if(is_null(self::$_master)){
$this->_master = $this->_slave = $this->conn_master($dbname);
}
}else{
if(is_null(self::$_master)){
$this->_master = $this->conn_master($dbname);
}
if(is_null(self::$_slave)){
$this->_slave = $this->conn_slave($dbname);
}
}
}
/**
* 连接到主数据库服务器
* Enter description here ...
*/
public function conn_master($dbname=''){
$_link = mysql_connect($this->_config['master']['host'],$this->_config['master']['user'],$this->_config['master']['passwd'],true) or die ("Connect ".$this->_config['master']['host']." fail.");
mysql_select_db(empty($dbname)?$this->_config['master']['db']:$dbname,$_link) or die(" The DB name ".$this->_config['master']['db']." is not exists.");
mysql_query("set names utf8",$_link);
return $_link;
}
/**
* 连接到从数据库服务器
* Enter description here ...
*/
public function conn_slave($dbname=''){
$offset = rand(0,count($this->_config['slave'])-1);
$_link = @mysql_connect($this->_config['slave'][$offset]['host'],$this->_config['slave'][$offset]['user'],$this->_config['slave'][$offset]['passwd'],true) or die(" Connect ".$this->_config['slave'][$offset]['host']." fail.");
mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$_link) or die(" The DB name ".$this->_config['slave'][$offset]['db']." is not exists.");
mysql_query("set names utf8",$_link);
return $_link;
}
/**
* 执行数据库查询
* Enter description here ...
* @param string $sql
*/
public function query($sql,$master=true){
if($master == true || (substr(strtolower($sql),0,6) != 'select') && $master == false){
$this->_res = mysql_query($sql,$this->_master);
if(!$this->_res){
$this->_error[] = mysql_error($this->_master);
}
$this->_flag = 'master';
$this->_link = $this->_master;
} else {
$this->_res = mysql_query($sql,$this->_slave);
if(!$this->_res){
$this->_error[] = mysql_error($this->_slave);
}
$this->_flag = 'slave';
$this->_link = $this->_slave;
}
return $this->_res;
}
/**
* 获取单行记录
* Enter description here ...
* @param mixed $rs
*/
public function get($rs=''){
if(empty($rs)){
$rs = $this->_res;
}
return mysql_fetch_row($rs);
}
/**
* 获取多行记录
* Enter description here ...
* @param mixed $rs
* @param $result_type
*/
public function fetch($rs = ''){
if(empty($rs)){
$rs = $this->_res;
}
return mysql_fetch_array($rs,MYSQL_ASSOC);
}
/**
* 插入数据
* Enter description here ...
* @param unknown_type $sql
*/
public function add($sql){
$rs = $this->query($sql);
if($rs)
return mysql_insert_id($this->_link);
return false;
}
/**
* 更新数据
* Enter description here ...
* @param unknown_type $sql
*/
public function update($sql){
if(empty($sql)) return false;
$rs = $this->query($sql);
if($rs)
return $this->fetchNum();
return false;
}
/**
* 获取上一条语句影响的行数
* Enter description here ...
*/
public function fetchNum(){
return mysql_affected_rows($this->_link);
}
/**
* 析构函数,释放数据库连接资源
* Enter description here ...
*/
public function __destruct(){
mysql_close($this->_link);
}
}
php读写分离是什么意思,php mysql读写分离相关推荐
- django给mysql配主从_django中的mysql主从读写分离:一、配置mysql主从分离
一.配置mysql主从同步的步骤: (1) 在主服务器上,必须开启二进制日志机制和配置一个独立的ID (2) 在每一个从服务器上,配置一个唯一的ID,创建一个用来专门复制主服务器数据的账号 (3) 在 ...
- proxy实现 mysql 读写分离
实现 mysql 读写分离 图解: 环境: iptables 和 selinux 关闭 proxy:test2 172.25.1.2 Master: test3 172.25.1.3 Slave:te ...
- 项目性能优化(MySQL读写分离、MySQL主从同步、Django实现MySQL读写分离)
当项目中数据库表越来越多,数据量也逐渐增多时,需要做数据库的安全和性能的优化.对于数据库的优化,可以选择使用MySQL读写分离实现. 1.MySQL主从同步 1.主从同步机制 1.1.主从同步介绍和优 ...
- MaxScale:实现MySQL读写分离与负载均衡的中间件利器
1.MaxScale 是干什么的? 配置好了MySQL的主从复制结构后,我们希望实现读写分离,把读操作分散到从服务器中,并且对多个从服务器能实现负载均衡. 读写分离和负载均衡是MySQL集群的基础需求 ...
- asp.net mysql 读写分离_MySQL读写分离
MySQL读写分离 1,为啥要读写分离? 系统到了高并发阶段,数据库一定要做的读写分离了,因为大部分的项目都是读多写少.所以针对这个情况,把写操作放一个主库,主库下挂多个从库处理读操作,这样就可以支撑 ...
- mysql读写分离,主从配置
2019独角兽企业重金招聘Python工程师标准>>> 一个完整的mysql读写分离环境包括以下几个部分: 应用程序client database proxy database集群 ...
- asp.net mysql 读写分离_.NET Core实现分表分库、读写分离的通用 Repository功能
首先声明这篇文章不是标题党,我说的这个类库是 FreeSql.Repository,它作为扩展库现实了通用仓储层功能,接口规范参考 abp vnext 定义,实现了基础的仓储层(CURD). 安装 d ...
- php mysql读写分离主从复制_mysql主从复制 读写分离原理及实现
主从复制,读写分离原理 在实际的生产环境中,对数据库的读和写都在同一个数据库服务器中,是不能满足实际需求的.无论是在安全性.高可用性还是高并发等各个方面都是完全不能满足实际需求的.因此,通过主从复制的 ...
- centos mysql卸载重装_提高性能,MySQL 读写分离环境搭建
MySQL 读写分离在互联网项目中应该算是一个非常常见的需求了.受困于 Linux 和 MySQL 版本问题,很多人经常会搭建失败,今天松哥就给大伙举一个成功的例子,后面有时间再和大家分享下使用 Do ...
最新文章
- .net中6个重要的基础概念:Stack, heap, Value types, reference types, boxing and Unboxing.
- margin 和 padding 的使用区别
- USACO 3.2 Stringsobits
- 生物信息学概论_大学专业详解系列83——生物信息学(理学学士)
- 云架构师是做什么的_为什么以及如何成为云架构师
- Golang的简明安装指南
- 天然黑糖行业调研报告 - 市场现状分析与发展前景预测
- android文章 - 收藏集 - 掘金
- php gd绘制图片,PHP-用GD绘制图形
- 零数科技获评《互联网周刊》2021元宇宙潜力企业TOP50
- 6.11编写计算正方体、圆柱体、球体的表面积和体积的类。
- SpringBoot系列教程(六十七):SpringBoot自定义Fastjson为JSON消息转换器
- Firefox about:config设置
- 关于移动宽带连不上某些网站的解决办法
- Kepserver EX6配置opc ua服务端 以及客户端
- 位图文件, JPG格式,PNG格式
- 他教全世界程序员怎么写好代码,答案写在这里!
- JAVA SE之面向对象12:集合3(Set)
- MATLAB散点密度图的画法三
- 前端Bootstrap框架
热门文章
- UE3 虚幻编辑器控制台命令
- 向iOS开发者介绍C++
- 设计模式的征途—1.单例(Singleton)模式
- 【原】文本挖掘——特征选择
- linux下配置Java和Go环境
- Wcf Rest Service模板--方法输入输出流数据
- 理解 JavaScript 闭包{转载}
- “内存不足”的九大原因及解决方法
- 多个python文件打包成exe_Python 3.4 .py文件打包成exe可执行文件方法
- linux脚本硬盘,Linux mount挂载和卸载硬盘脚本分享