Rumah > php教程 > PHP源码 > 关于mybb主从数据配置

关于mybb主从数据配置

PHP中文网
Lepaskan: 2016-05-25 17:10:09
asal
1247 orang telah melayarinya

今天微博上有朋友问到我Mybb如何配置主从其实我对mybb不是很熟悉,就大致看了一下DB发现是可以配置主从的在inc/config.php中配置如下$con fig['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为主 多个的时候['r ead'][0]['hostname']这样以数据配置即可但是在db_mysql.php中我却发现在处理主从的时候。有一个不和谐的代码。目前我也没有想明白目 的175行if(array_key_exists('hostname', $connections[$type])) { $details = $connections[$type]; unset($connections);//这里unset了 就肯定只能够连接到read的第一个数据库上 $connections[$type][] = $details;}我没有搞明白目的所以没有进行修改。如果要实现要实现只需要重新处理一下$connections数组就可以解决我这里就不累述。

处理主从的代码

foreach(array('read', 'write') as $type)
        {
            print_r($connections);if(!is_array($connections[$type]))
            {
                break;
            }

            if(array_key_exists('hostname', $connections[$type]))
            {
                $details = $connections[$type];
                unset($connections);
                $connections[$type][] = $details;
            }

            // Shuffle the connections
            shuffle($connections[$type]);

            // Loop-de-loop
            foreach($connections[$type] as $single_connection)
            {
                $connect_function = "mysql_connect";
                if($single_connection['pconnect'])
                {
                    $connect_function = "mysql_pconnect";
                }

                $link = $type."_link";

                $this->get_execution_time();

                $this->$link = @$connect_function($single_connection['hostname'], $single_connection['username'], $single_connection['password'], 1);

                $time_spent = $this->get_execution_time();
                $this->query_time += $time_spent;

                // Successful connection? break down brother!
                if($this->$link)
                {
                    $this->connections[] = "[".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']} (Connected in ".number_format($time_spent, 0)."s)";
                    break;
                }
                else
                {
                    $this->connections[] = "<span style="color: red">[FAILED] [".strtoupper($type)."] {$single_connection[&#39;username&#39;]}@{$single_connection[&#39;hostname&#39;]}</span>";
                }
            }
        }
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan