MYSQL实现主从insert和query分开操作
发表于:2025-11-06 作者:千家信息网编辑
千家信息网最后更新 2025年11月06日,
千家信息网最后更新 2025年11月06日MYSQL实现主从insert和query分开操作
dbConfig = $dbConfig; $this->multi_server = empty ( $this->dbConfig['slave'] ) ? false : true; } /** * 连接数据库方法 * @param type $dbConfig */ public function connect ($db) { $this->dbCurrent = $db; $linkfunction = ( TRUE == $db['persistent'] ) ? 'mysql_pconnect' : 'mysql_connect'; $this->conn = $linkfunction ( $db ['host'], $db ['username'], $db ['password']); if (! $this->conn) { return false; } $re = mysql_select_db($db['dbname'], $this->conn); if(!$re) { return false; } mysql_query ( "SET NAMES '" . $db ['charset'] . "'", $this->conn ); } /** * 初始化数据库连接 * @param type $master */ public function initConnect ($master = true) { if ($master || !$this->multi_server) { if($this->m_link_id){ $this->conn = $this->m_link_id; $this->ping($master); } else { $this->connect ( $this->dbConfig ['master'] ); $this->m_link_id = $this->conn; } } else { if($this->s_link_id){ $this->conn = $this->s_link_id; $this->ping($master); } else { $rand = rand(0, count($this->dbConfig ['slave']) - 1); $this->connect ( $this->dbConfig ['slave'][$rand] ); $this->s_link_id = $this->conn; } } } /** * 按SQL语句获取记录结果,返回数组 * * @param sql 执行的SQL语句 */ public function getArray($sql) { if( ! $result = $this->query($sql) )return FALSE; if( ! mysql_num_rows($result) ) return FALSE; $rows = array(); while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){} mysql_free_result($result); array_pop($rows); return $rows; } /** * 返回当前插入记录的主键ID */ public function newinsertid() { return mysql_insert_id($this->conn); } /** * 格式化带limit的SQL语句 */ public function setlimit($sql, $limit) { return $sql. " LIMIT {$limit}"; } /** * 执行一个SQL语句 * * @param sql 需要执行的SQL语句 */ public function exec($sql) { $this->arrSql[] = $sql; $this->initConnect ( true ); return mysql_query($sql, $this->conn); } /** * 执行一个SQL语句,主要用于查询 * @param type $sql * @param type $master default:false 为true:强制读主库;为false:在有从库的的情况下优先读从库,否则读主库 */ public function query ($sql, $master = false) { $this->arrSql[] = $sql; $this->initConnect ( $master ); return mysql_query($sql, $this->conn); } /** * 返回影响行数 */ public function affected_rows() { return mysql_affected_rows($this->conn); } /** * 获取数据表结构 * * @param tbl_name 表名称 */ public function getTable($tbl_name) { return $this->getArray("DESCRIBE {$tbl_name}"); } //防止mysql gone away public function ping($master) { if(!@mysql_ping($this->conn)){ @mysql_close($this->conn); //注意:一定要先执行数据库关闭,这是关键 if($master || !$this->multi_server) { unset($this->m_link_id); } else { unset($this->s_link_id); } $this->initConnect($master); } } /** * 对特殊字符进行过滤 * * @param value 值 */ public function __val_escape($value) { if(is_null($value))return 'NULL'; if(is_bool($value))return $value ? 1 : 0; if(is_int($value))return (int)$value; if(is_float($value))return (float)$value; if(@get_magic_quotes_gpc())$value = stripslashes($value); $this->conn || $this->initConnect(); return '\''.mysql_real_escape_string($value, $this->conn).'\''; } public function escape($value) { if(is_null($value))return 'NULL'; if(is_bool($value))return $value ? 1 : 0; if(is_int($value))return (int)$value; if(is_float($value))return (float)$value; if(@get_magic_quotes_gpc())$value = stripslashes($value); $this->conn || $this->initConnect(); return mysql_real_escape_string($value, $this->conn); } /** * 析构函数 */ public function __destruct() { if( TRUE != @$this->dbCurrent['persistent'] )@mysql_close($this->conn); }}重点在 initConnect($master)方法里,这里决定加载的配置文件中是连接到主库还是从库
语句
数据
数据库
方法
特殊
关键
函数
名称
字符
情况
数据表
数组
文件
格式
结构
结果
这是
重点
强制
影响
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
ssms新建数据库
天津智能服务器云空间
软件开发常用简写
杭州软件开发合法吗
服务器摸排
保险公司网络安全保障工作方案
服务器 tdp
服务器出现故障黑屏
单位网络安全宣传总结
公安网络安全保卫工作情况报告
sftp 连接数据库
数据库 体会
网络安全通报预警工作
咸宁智慧旅游软件开发
魔兽专业数据库
可以安装苹果的虚拟服务器
周鸿祎网络安全人才缺口大
即域名服务器
资阳软件开发专业定制
梦幻西游山东区有哪些服务器
如果提升软件开发效率
idm服务器响应
数据库谱系
数据库的课本
网络安全和信息化书籍资料
国家桥梁基础数据库录入
assecc数据库创建表时在
h5 保存数据库
数据库dbs概念
数据库升级sql2017