MySQL master/slave data configuration-PHP source code

Source: Internet
Author: User
A friend on Weibo asked me how to configure the master and slave data of mybb. actually, I am not very familiar with Mybb, I have roughly looked at the DB and found that the master and slave nodes can be configured in inc/config. in php, configure $ 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 ['R ad'] [0] ['hostname'] for multiple master nodes can be configured with data. However, in db_mysql.php, I found that the master node is being processed. There is an incompatible code. Currently, I do not want to understand the 175 rows of if (array_key_exists ('hostname', $ connections [$ type]) {$ details = $ connections [$ type]; unset ($ connections); // once unset is set, you can only connect to the first read Database. $ connections [$ type] [] = $ details ;} I did not understand the purpose, so I did not modify it. To implement this function, you only need to reprocess the $ connections array.

Process master/slave code

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[] = "[FAILED] [".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']}";                }            }        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.