首頁  >  文章  >  後端開發  >  php實作的支援讀寫分離的MySQL類

php實作的支援讀寫分離的MySQL類

WBOY
WBOY原創
2016-07-25 08:56:07921瀏覽
  1. /**

  2. * MySQL讀取與寫入分離類別
  3. * $db_config = array(
  4. * 'master' => array('host'=>'localhost:3306','user'=>'admin',' passwd'=>'123456','db'=>'stat'),
  5. * 'slave' => array(
  6. * array('host'=>'localhost:3307','user'= >'admin','passwd'=>'123456','db'=>'stat'),
  7. * array('host'=>'localhost:3308','user'=>'admin', 'passwd'=>'123456','db'=>'stat')
  8. * )
  9. * );
  10. *
  11. * 註:如果slave有多個時隨機連接其中的一個
  12. * 最後編輯:bbs.it-home.org
  13. */
  14. /*
  15. $db_config = array(
  16. 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>' stat'),
  17. '從站' => array(
  18. array('host'=>'localhost:3307','user'=>'admin','passwd'=>'123456 ','db'=>'stat' ),
  19. array('host'=>'localhost:3308','user'=>'admin','passwd'=>'123456', 'db'=>'stat')
  20. )
  21. );
  22. $db = MySQL::getInstance('','r-w');< ;/p>

  23. $sql = "從管理中選擇*";

  24. $rs = $db->query($sql);

  25. while ($row = $db->fetch($rs)){
  26. echo "uid:".$row['uid']." ".$row['userName']."
    ";
  27. }
  28. echo "


    ";
  29. */
  30. class MySQL

  31. {
  32. private static $_instance = null;//資料庫連線實例
  33. private static $_master = null;/ /主資料庫連線實例
  34. private static $_slave = null;//重新資料庫連線實例
  35. public $_config = array();//資料庫連線設定資訊
  36. public $_res = null; //查詢實例句柄
  37. public $_flag = '';//標識目前語句是在主或重資料庫上執行
  38. public $_link = null;
  39. /**
  40. * 單一實例
  41. * 在此輸入描述...
  42. * @paramknown_type $dbname
  43. * @paramunknown_type $mode
  44. * /
  45. public static function & getInstance($dbname='',$mode='rw '){
  46. if (is_null(self::$_instance)){
  47. self::$_instance = new self ();
  48. self::$_instance->__getConf();
  49. self::$_instance->connect($dbname,$mode);
  50. }
  51. return self:: $_instance;
  52. }
  53. /**

  54. * 取得資料庫設定資訊
  55. * Enter description here ...
  56. */
  57. public function __getConf(){
  58. global $db_config;
  59. $this->_config[🎜> global $db_config;
  60. $this->_config[ 'master'] = $db_config['master'];
  61. $this->_config['slave'] = $db_config['slave'];
  62. }
  63. /**
  64. * 資料庫連線
  65. * Enter description here ...
  66. * @param $dbname 指定連接的資料庫名,預設連接設定檔的函式庫
  67. * @param $mode rw表示連接主庫,r-w表示讀寫分離
  68. */
  69. public function connect($dbname='' ,$mode = 'rw'){
  70. if($mode == 'rw'){
  71. if(is_null(self::$_master )){
  72. $this->_master = $this ->_slave = $this->conn_master($dbname);
  73. }
  74. }else{
  75. if(is_null(self::$_master )){
  76. $this->_master = $this->conn_master($dbname);
  77. }
  78. if(is_null(self::$_slave)){
  79. $this->_slave = $this->conn_slave($dbname );
  80. }
  81. }
  82. }
  83. /**
  84. * 連接到主資料庫伺服器
  85. * Enter description here ...
  86. */
  87. public function conn_master($dbname=''){
  88. $_link = mysql_connect( $this->_config['master']['host'],$this->_config['master']['user'],$this->_config['master'] ['passwd'] ,true) 或死亡("連線".$this->_config['master']['host']." 失敗。 ");
  89. mysql_select_db(empty($dbname)?$this->_config ['master']['db']:$dbname,$_link) 或die(" 資料庫名稱".$this->_config[ 'master']['db']." 不存在。") ;
  90. mysql_query("設定名稱utf8",$_link);
  91. return $_link; }
  92. /**
  93. * 連接到從資料庫伺服器
  94. * Enter description here ...
  95. */
  96. public function conn_slave($dbname=''){
  97. $offset = rand(0,count($this->_config['slave') ])-1);
  98. $_link = @mysql_connect($this->_config['slave'][$offset]['host'],$this->_config['slave'][$offset ][ 'user'],$this->_config['slave'][$offset]['passwd'],true) 或die(" Connect ".$this->_config['slave'][$offset][' host']." 失敗。");
  99. mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$ _link) 或die( " 資料庫名稱".$this->_config['slave'][$offset]['db']." 不存在。");
  100. mysql_query("設定名稱utf8" ,$_link);
  101. return $_link;
  102. }
  103. /**

  104. * 執行資料庫查詢
  105. * Enter description here ...
  106. * @param string $sql
  107. */
  108. 公用函數查詢($sql,$master=true){
  109. if($master == true || (substr(strtolower($sql),0,6) != 'select') && $master == false){
  110. $this->_res = mysql_query($ sql ,$this->_master);
  111. if(!$this->_res){
  112. $this->_error[] = mysql_error($this->_master);
  113. }
  114. $ this->_flag = 'master';
  115. $this->_link = $this->_master;
  116. } else {
  117. $this->_res = mysql_query($sql ,$this->_slave) ;
  118. if(!$this->_res){
  119. $this->_error[] = mysql_error($this->_slave);
  120. }
  121. $this->_flag = 'slave' ;
  122. $this->_link = $this->_slave;
  123. }
  124. return $this->_res;

  125. }
  126. /**
  127. * 取得單行記錄
  128. * Enter description here ...
  129. * @param mixed $rs
  130. */
  131. public function get($rs=''){
  132. if(empty($rs) ){
  133. $rs = $this->_res;
  134. }
  135. return mysql_fetch_row($rs);
  136. }
  137. /**
  138. * 取得多行記錄
  139. * Enter description here ...
  140. * @param mixed $rs
  141. * @param $result_type
  142. */
  143. public function fetch($rs = ''){
  144. if(empty($rs)) {
  145. $rs = $this->_res;
  146. }
  147. return mysql_fetch_array($rs,MYSQL_ASSOC) );
  148. }
  149. /**
  150. * 插入資料
  151. * Enter description here ...
  152. * @param unknown_type $sql
  153. /
  154. * 更新資料
  155. * Enter description here ...
  156. * @param unknown_type $sql
  157. */
  158. public function add($sql){
  159. $rs = $this->query($sql);
  160. if($rs)
  161. return mysql_insert_id($this->_link);
  162. return false;
  163. }
  164. /**
  165. * 取得上一語句影響的行數
  166. * Enter description here ...
  167. */
  168. 公用函數更新($sql){
  169. if(empty($sql)) return false;
  170. $rs = $this->query($sql);
  171. if($rs)
  172. return $this ->fetchNum();
  173. return false;
  174. }
  175. /**

  176. * 析構函數,釋放資料庫連線資源
  177. * Enter description here ...
  178. */
  179. public function fetchNum(){
  180. return mysql_affected_rows($this->_link);
  181. }
  182. /***/* 🎜> public function __destruct(){
  183. mysql_close($this->_link);
  184. }
  185. }
複製代碼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn