thinkphp Distributed database Connection code Analysis

Source: Internet
Author: User
Tags dsn explode getmessage

Thinkphp as a domestic popular framework, I believe that the use of people must be a minority. Let's analyze this article. thinkphpThe more important part of the--the connection of the distributed database.

Of course, we are not here to use the model to make the database to be used for pruning and checking. We do an analysis of the underlying connection code to help you better understand thinkphp's operations on the database. It is convenient for us to use later.

I. Connection to a single database

When used, the connection configuration of a single database is very simple. We only need to configure the information in the configuration file.

' Db_type ' = ' mysql ',
' Db_host ' = ' 192.168.5.102 ',
' Db_name ' = ' databasename ',
' Db_user ' = ' USER ',
' Db_pwd ' = ' password ',
' Db_port ' = ' 3306 ',
' Db_prefix ' = ' onmpw_ ',

After the setup is complete, you can use it. By default, a single database connection is in progress.

Second, the connection of the distributed database

The connection to a single database is simple, and we focus on the connection of the distributed database .

' Db_type ' = ' mysql ',
' Db_host ' = ' 192.168.5.191,192.168.5.88,192.168.5.103 ',
' Db_name ' = ' test,test,test ',
' Db_user ' = ' masteruser,slaveuser,slaveuser ',
' Db_pwd ' = ' masterpass,slavepass,slavepass ',
' Db_port ' = ' 3306 ',
' Db_prefix ' = ',
' Db_deploy_type ' + 1,//Database deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)
' Db_rw_separate ' + true,//database read/write whether separate master-slave valid
' Db_master_num ' + 1,//read/write separation of the number of servers
' Db_slave_no ' + ',//Specify the number from the server

With the above configuration, you can connect to the distributed database.

Let's look at a few options below

' Db_host '

Distributed database, there are several servers to fill in several server addresses, each address separated by commas. If it is a master -slave distribution, the previous address is the address of the primary database.

For the following user name and password there are listening ports and so on, of course, there are a few to write a few. If the user name and password are the same for each database, you can write only one.

The code for parsing these options is as follows

$_config[' username '] = explode (', ', $this->config[' username ']);
$_config[' password '] = explode (', ', $this->config[' password ']);
$_config[' hostname '] = explode (', ', $this->config[' hostname ']);
$_config[' hostport '] = explode (', ', $this->config[' Hostport ']);
$_config[' database ' = explode (', ', $this->config[' database ');
$_config[' DSN '] = explode (', ', $this->config[' DSN ']);
$_config[' charset '] = explode (', ', $this->config[' CharSet ']);

' Db_deploy_type ' =>1

The 1 representation is distributed, and 0 represents a centralized (that is, a single server).

The implementation of this option is in the class Think\db\dirver

protected function Initconnect ($master =true) {
if (!empty ($this->config[' deploy '))
Using a distributed database
$this->_linkid = $this->multiconnect ($master);
Else
Default Single Database
if (! $this->_linkid) $this->_linkid = $this->connect ();
}

$this->config[' Deploy ' represents the ' db_deploy_type ' configuration option, the above configuration has been parsed before use, and the configuration items are in the $this->config array. As to how to parse the configuration file, here we do not introduce, interested can refer to the Think\db class.

The $this->multiconnect () function is used for distributed connections, and if the ' db_deploy_type ' option is set to 1, the function executes. Otherwise, execute the $this->connect () function directly.

' Db_rw_separate ' =>true

True indicates read-write separation; False indicates that read/write is not separated.

It is important to note that the read-write separation is based on the master-slave database system. This option is set to True when the primary database is written and read from the database.

if ($this->config[' rw_separate ') {
Master-Slave using read/write separation
if ($master)
Primary server Write
$r = $m;
else{
if (Is_numeric ($this->config[' slave_no ')) {//Specify server Read
$r = $this->config[' slave_no ');
}else{
Read operation connection from server
$r = Floor (Mt_rand ($this->config[' Master_num '],count ($_config[' hostname '])-1); Every random connection to the database
}
}
}else{
Read and write operations do not differentiate between servers
$r = Floor (Mt_rand (0,count ($_config[' hostname ')-1)); Every random connection to the database
}

$this->config[' Rw_separate ') is true when read-write separation is used, and read/write is not separated when false. Why does the read-write separation have to be master-slave? Since the server cannot write to read only, if the data is written to the slave server, the data cannot be synchronized. This can result in inconsistent data. So, if our system is a master-slave, we have to use read-write separation. This means that the Db_rw_separate option must be configured to TRUE.

' Db_master_num ' =>1

The number after this option indicates the number of master servers after read-write separation. Therefore, this option is also used for master-slave database systems.

The following code is to select the primary server.

$m = Floor (Mt_rand (0, $this->config[' Master_num ']-1));

Select the core code to read from the server when the master-slave database reads

$r = Floor (Mt_rand ($this->config[' Master_num '],count ($_config[' hostname '])-1); Every random connection to the database

where $this->config[' Master_num ' represents the number of primary servers.

' Db_slave_no ' and ' = '

Specifies the sequence number from the server that is used to read the data. If not set, the number of books from the server is calculated based on the number of primary servers, and then a random selection is made to read from it.

if (Is_numeric ($this->config[' slave_no ')) {//Specify server Read
$r = $this->config[' slave_no ');
}else{
Read operation connection from server
$r = Floor (Mt_rand ($this->config[' Master_num '],count ($_config[' hostname '])-1); Every random connection to the database
}

The above is a simple explanation of the implementation code for each option's function.

Let's look at the parts of the connection

if ($m! = $r) {
$db _master = Array (
' Username ' = isset ($_config[' username '] [$m])? $_config[' username ' [$m]:$_config[' username '][0],
' Password ' = isset ($_config[' password '] [$m])? $_config[' Password ' [$m]:$_config[' password '][0],
' Hostname ' = isset ($_config[' hostname '] [$m])? $_config[' hostname ' [$m]:$_config[' hostname '][0],
' Hostport ' = isset ($_config[' hostport '] [$m])? $_config[' Hostport ' [$m]:$_config[' Hostport '][0],
' Database ' = isset ($_config[' database '] [$m])? $_config[' database ' [$m]:$_config[' database '][0],
' DSN ' = isset ($_config[' DSN '] [$m])? $_config[' DSN ' [$m]:$_config[' DSN '][0],
' CharSet ' = Isset ($_config[' charset '] [$m])? $_config[' CharSet ' [$m]:$_config[' CharSet '][0],
);
}
$db _config = Array (
' Username ' = isset ($_config[' username '] [$r])? $_config[' username ' [$r]:$_config[' username '][0],
' Password ' = isset ($_config[' password '] [$r])? $_config[' Password ' [$r]:$_config[' password '][0],
' Hostname ' = isset ($_config[' hostname '] [$r])? $_config[' hostname ' [$r]:$_config[' hostname '][0],
' Hostport ' = isset ($_config[' hostport '] [$r])? $_config[' Hostport ' [$r]:$_config[' Hostport '][0],
' Database ' = isset ($_config[' database '] [$r])? $_config[' database ' [$r]:$_config[' database '][0],
' DSN ' = isset ($_config[' DSN '] [$r])? $_config[' DSN ' [$r]:$_config[' DSN '][0],
' CharSet ' = Isset ($_config[' charset '] [$r])? $_config[' CharSet ' [$r]:$_config[' CharSet '][0],
);
return $this->connect ($db _config, $r, $r = = $m? false: $db _master);

To see this, I think we should be on the above in the introduction of the various configuration options of the code in which the "and $m is what role."

Now let's see $r = = $m? False: $db _master, if the database read and write is not separated, read and write is a server, the value passed to the Connect function is false. Or if it is a master-slave separate write, the value passed to connect is also false. By the above code we see that if "and $m are not equal, the $db_master will be set." In fact, it is equivalent to a standby, if the selected server fails to connect, will connect to $db_master.

The third parameter of the Connect () function actually indicates whether an alternate connection is selected when the $db_config server fails to connect. False means no reconnection, and other values indicate reconnection.

The core code is as follows

try{
if (Empty ($config [' DSN '])) {
$config [' dsn '] = $this->parsedsn ($config);
}
if (Version_compare (php_version, ' 5.3.6 ', ' <= ')) {
Disabling impersonation preprocessing statements
$this->options[pdo::attr_emulate_prepares] = false;
}
$this->linkid[$linkNum] = new PDO ($config [' DSN '], $config [' username '], $config [' Password '], $this->options);
}catch (\pdoexception $e) {
if ($autoConnection) {//$autoConnection not false, but default primary server
Trace ($e->getmessage (), ', ' ERR ');
return $this->connect ($autoConnection, $linkNum); An exception occurs and a recursive function is used to reconnect
}elseif ($config [' Debug ']) {
E ($e->getmessage ());
}
}

This way, for the master-slave, $r and $m certainly not the same. Therefore, if the selected server fails when it reads the data, the primary server is standby and the master server is eventually read. To ensure the timeliness of data read.

But the overall feeling is not perfect yet. If there are multiple slave servers, the one selected from the server and the primary server fails at the time of reading, then the data is not read failed. It should be more secure if you can read the other slave servers again. Of course, the current thinkphp function has been quite perfect, enough for us to use. But still hope thinkphp more perfect after.

thinkphp Distributed database Connection code Analysis

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.