• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

mybb主从数据配置

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-09-07点击:

 今天微博上有朋友问到我Mybb如何配置主从

其实我对mybb不是很熟悉,就大致看了一下DB
发现是可以配置主从的
在inc/config.php
中配置如下
$config['database']['read']['hostname'] = 'localhost';
$config['database']['read']['username'] = 'root';
$config['database']['read']['password'] = '';
$config['database']['write']['hostname'] = 'localhost';
$config['database']['write']['username'] = 'root';
$config['database']['write']['password'] = '';
//read为从 write为主 多个的时候['read'][0]['hostname']这样以数据配置即可

但是在db_mysql.php中我却发现在处理主从的时候。有一个不和谐的代码。目前我也没有想明白目的
175行

if(array_key_exists('hostname', $connections[$type]))
   {
    $details = $connections[$type];
    unset($connections);//这里unset了 就肯定只能够连接到read的第一个数据库上
    $connections[$type][] = $details;
}

我没有搞明白目的所以没有进行修改。如果要实现
要实现只需要重新处理一下$connections数组就可以解决
我这里就不累述。
  1. foreach(array('read', 'write') as $type) 
  2.  
  3.         { 
  4.             print_r($connections);if(!is_array($connections[$type])) 
  5.             { 
  6.                 break; 
  7.             } 
  8.              
  9.             if(array_key_exists('hostname', $connections[$type])) 
  10.             { 
  11.                 $details = $connections[$type]; 
  12.                 unset($connections); 
  13.                 $connections[$type][] = $details; 
  14.             } 
  15.  
  16.             // Shuffle the connections 
  17.             shuffle($connections[$type]); 
  18.  
  19.             // Loop-de-loop 
  20.             foreach($connections[$type] as $single_connection) 
  21.             { 
  22.                 $connect_function = "mysql_connect"
  23.                 if($single_connection['pconnect']) 
  24.                 { 
  25.                     $connect_function = "mysql_pconnect"
  26.                 } 
  27.                  
  28.                 $link = $type."_link"; 
  29.  
  30.                 $this->get_execution_time(); 
  31.  
  32.                 $this->$link = @$connect_function($single_connection['hostname'], $single_connection['username'], $single_connection['password'], 1); 
  33.  
  34.                 $time_spent = $this->get_execution_time(); 
  35.                 $this->query_time += $time_spent; 
  36.  
  37.                 // Successful connection? break down brother! 
  38.                 if($this->$link) 
  39.                 { 
  40.                     $this->connections[] = "[".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']} (Connected in ".number_format($time_spent, 0)."s)"; 
  41.                     break; 
  42.                 } 
  43.                 else 
  44.                 { 
  45.                     $this->connections[] = "<span style=\"color: red\">[FAILED] [".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']}</span>"; 
  46.                 } 
  47.             } 
  48.         } 

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1978130638@qq.com ,  QQ