php中怎么实现Mysql读写分离操作-创新互联
php中怎么实现Mysql读写分离操作,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联是专业的广水网站建设公司,广水接单;提供网站制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行广水网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!连接案例:
'www.aslibra.com', 'dbuser'=>'aslibra', 'dbpw'=>'www.aslibra.com', 'dbname'=>'test' ); $db_ro = array( array( 'dbhost'=>'www.aslibra.com:4306', 'dbuser'=>'aslibra', 'dbpw'=>'www.aslibra.com' ) ); $DB = new mysql_rw_php; //写入连接,单服务器操作 $DB->connect($db_rw[dbhost], $db_rw[dbuser], $db_rw[dbpw], $db_rw[dbname]); //读取连接,可以多服务器操作 $DB->connect_ro($db_ro[0][dbhost], $db_ro[0][dbuser], $db_ro[0][dbpw]); $DB->set_ro_list($db_ro); //send to rw $sql = "insert into a set a='test'"; $DB->query($sql); //send to ro $sql = "select * from a"; $qr = $DB->query($sql); while($row = $DB->fetch_array($qr)){ echo $row[a]; } ?>
连接类:
link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) { $halt && $this->halt('Can not connect to MySQL server'); } } else { if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) { $halt && $this->halt('Can not connect to MySQL server'); } } //只读连接失败 if(!$this->link && !$halt) return false; //未初始化rw时,第一个连接作为rw if($this->link_rw == null) $this->link_rw = $this->link; if($this->version() > '4.1') { if($this->charset) { @mysql_query("SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary", $this->link); } if($this->version() > '5.0.1') { @mysql_query("SET sql_mode=''", $this->link); } } if($dbname) { $this->select_db($dbname); } } //连接一个只读的mysql数据库 function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){ if($this->link_rw == null) $this->link_rw = $this->link; $this->link = null; //不产生halt错误 $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, false); if($this->link){ //连接成功 //echo "link ro sussess!
"; $this->ro_exist = true; $this->link_ro = $this->link; if($this->cur_db){ //如果已经选择过数据库则需要操作一次 @mysql_select_db($this->cur_db, $this->link_ro); } }else{ //连接失败 //echo "link ro failed!
"; $this->link = &$this->link_rw; } } //设置一系列只读数据库并且连接其中一个 function set_ro_list($ro_list){ if(is_array($ro_list)){ //随机选择其中一个 $link_ro = $ro_list[array_rand($ro_list)]; $this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro['dbpw']); } } function select_db($dbname) { //同时操作两个数据库连接 $this->cur_db = $dbname; if($this->ro_exist){ @mysql_select_db($dbname, $this->link_ro); } return @mysql_select_db($dbname, $this->link_rw); } function fetch_array($query, $result_type = MYSQL_ASSOC) { return mysql_fetch_array($query, $result_type); } function fetch_one_array($sql, $type = '') { $qr = $this->query($sql, $type); return $this->fetch_array($qr); } function query($sql, $type = '') { $this->link = &$this->link_rw; //判断是否select语句 if($this->ro_exist && preg_match ("/^(\s*)select/i", $sql)){ $this->link = &$this->link_ro; } $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query'; if(!($query = $func($sql, $this->link)) && $type != 'SILENT') { $this->halt('MySQL Query Error', $sql); } $this->querynum++; return $query; } function affected_rows() { return mysql_affected_rows($this->link); } function error() { return (($this->link) ? mysql_error($this->link) : mysql_error()); } function errno() { return intval(($this->link) ? mysql_errno($this->link) : mysql_errno()); } function result($query, $row) { $query = @mysql_result($query, $row); return $query; } function num_rows($query) { $query = mysql_num_rows($query); return $query; } function num_fields($query) { return mysql_num_fields($query); } function free_result($query) { return mysql_free_result($query); } function insert_id() { return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); } function fetch_row($query) { $query = mysql_fetch_row($query); return $query; } function fetch_fields($query) { return mysql_fetch_field($query); } function version() { return mysql_get_server_info($this->link); } function close() { return mysql_close($this->link); } function halt($message = '', $sql = '') { $dberror = $this->error(); $dberrno = $this->errno(); echo "MySQL Error"; exit(); } } ?>
Message: $message
SQL: $sql
Error: $dberror
Errno.: $dberrno
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。
本文题目:php中怎么实现Mysql读写分离操作-创新互联
标题网址:http://ybzwz.com/article/cdhogj.html