1.分布式數(shù)據(jù)庫是什么:
tp的分布式數(shù)據(jù)庫主要是通過該配置:
'DB_DEPLOY_TYPE'??????? =>? 1,// 數(shù)據(jù)庫部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器)
2.主從服務(wù)器的讀寫分離是什么:
? 主從數(shù)據(jù)庫即為一個主數(shù)據(jù)庫會有對應(yīng)n個從數(shù)據(jù)庫,而從數(shù)據(jù)庫只能有一個對應(yīng)的從數(shù)據(jù)庫。主從數(shù)據(jù)庫中寫的操作需要使用主數(shù)據(jù)庫,而讀操作使用從數(shù)據(jù)庫。主數(shù)據(jù)庫與從數(shù)據(jù)庫始終保持?jǐn)?shù)據(jù)一致性。其中保持?jǐn)?shù)據(jù)庫一致的原理即為當(dāng)主數(shù)據(jù)庫數(shù)據(jù)發(fā)生變化時,會將操作寫入到主數(shù)據(jù)庫日志中,而從數(shù)據(jù)庫會不停的讀取主數(shù)據(jù)庫的日志保存到自己的日志系統(tǒng)中,然后進(jìn)行執(zhí)行,從而保持了主從數(shù)據(jù)庫一致。
3.詳解
一、單一數(shù)據(jù)庫的連接
在使用的時候,單一數(shù)據(jù)庫的連接配置非常簡單。我們只需要在配置文件中配置一下的信息即可。
'DB_TYPE' => 'mysql', 'DB_HOST' => '192.168.5.102', 'DB_NAME' => 'databasename', 'DB_USER' => 'user', 'DB_PWD' => 'password', 'DB_PORT' => '3306', 'DB_PREFIX' => 'onmpw_',
設(shè)置完成以后就可以使用了。默認(rèn)情況下就是單一的數(shù)據(jù)庫連接。
二、分布式數(shù)據(jù)庫的連接
單一數(shù)據(jù)庫的連接很簡單,我們著重分析一下分布式數(shù)據(jù)庫的連接。
'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, // 數(shù)據(jù)庫部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器) 'DB_RW_SEPARATE' => true, // 數(shù)據(jù)庫讀寫是否分離 主從式有效 'DB_MASTER_NUM' => 1, // 讀寫分離后 主服務(wù)器數(shù)量 'DB_SLAVE_NO' => '', // 指定從服務(wù)器序號
按照以上配置就可以連接分布式數(shù)據(jù)庫了。
下面我們看下面幾個選項
‘DB_HOST’
分布式數(shù)據(jù)庫,有幾臺服務(wù)器就要填寫幾個服務(wù)器地址,每個地址之間用逗號隔開。如果是主從式分布的話,前面的地址要是主數(shù)據(jù)庫的地址。
對于下面的用戶名和密碼還有監(jiān)聽端口之類的,當(dāng)然是有幾個就寫幾個。如果各個數(shù)據(jù)庫的用戶名和密碼都一樣的話,可以只寫一個。
對于這些選項的解析的代碼如下
$_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
1 表示是分布式, 0 表示的是集中式(也就是單一服務(wù)器)。
該選項的實現(xiàn)是在類 Think\Db\Dirver中
protected function initConnect($master=true) { if(!empty($this->config['deploy'])) // 采用分布式數(shù)據(jù)庫 $this->_linkID = $this->multiConnect($master); else // 默認(rèn)單數(shù)據(jù)庫 if ( !$this->_linkID ) $this->_linkID = $this->connect(); }
$this->config[‘deploy’]表示的就是’DB_DEPLOY_TYPE’這個配置選項,上面的配置在使用之前都已經(jīng)經(jīng)過解析了,配置項都在$this->config數(shù)組中。至于是如何解析配置文件的,這里我們不做介紹,感興趣的可以參考Think\Db類。
$this->multiConnect()函數(shù)就是用來進(jìn)行分布式連接的,如果’DB_DEPLOY_TYPE’選項設(shè)置為1,該函數(shù)就會執(zhí)行。否則直接執(zhí)行$this->connect()函數(shù)。
‘DB_RW_SEPARATE’=>true
true 表示讀寫分離;false表示讀寫不分離。
這里需要注意的是,讀寫分離是以主從式數(shù)據(jù)庫系統(tǒng)為前提的。該選項設(shè)置為true的時候主數(shù)據(jù)庫寫,從數(shù)據(jù)庫讀。
if($this->config['rw_separate']){ // 主從式采用讀寫分離 if($master) // 主服務(wù)器寫入 $r = $m; else{ if(is_numeric($this->config['slave_no'])) {// 指定服務(wù)器讀 $r = $this->config['slave_no']; }else{ // 讀操作連接從服務(wù)器 $r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1)); // 每次隨機(jī)連接的數(shù)據(jù)庫 } } }else{ // 讀寫操作不區(qū)分服務(wù)器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次隨機(jī)連接的數(shù)據(jù)庫 }
$this->config[‘rw_separate’] 為true的時候采用讀寫分離,為false的時候讀寫不分離。讀寫分離為什么必須是主從式的呢?因為從服務(wù)器不能寫只能讀,如果向從服務(wù)器寫入數(shù)據(jù)的話,數(shù)據(jù)是沒法同步的。這樣就會造成數(shù)據(jù)不一致的情況。所以說,如果我們的系統(tǒng)是主從式的話,我們必須采用讀寫分離。也就是說DB_RW_SEPARATE選項必須配置為true。
'DB_MASTER_NUM'=>1
該選項后面的數(shù)字表示讀寫分離后,主服務(wù)器的數(shù)量。因此該選項也是用于主從式數(shù)據(jù)庫系統(tǒng)。
下面的代碼是選擇主服務(wù)器。
$m? =? floor(mt_rand(0,$this->config['master_num']-1));
主從式數(shù)據(jù)庫讀取的時候選擇從服務(wù)器讀的核心代碼
$r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1));?? // 每次隨機(jī)連接的數(shù)據(jù)庫
其中$this->config[‘master_num’]表示主服務(wù)器的數(shù)量。
'DB_SLAVE_NO'=> ''
指定從服務(wù)器的序號,用于讀取數(shù)據(jù)。如果不設(shè)置,則根據(jù)主服務(wù)器的數(shù)量計算書從服務(wù)器的數(shù)量,然后從中隨機(jī)選取一臺進(jìn)行讀取。
if(is_numeric($this->config['slave_no'])) {// 指定服務(wù)器讀
?? $r = $this->config['slave_no'];
}else{
?? // 讀操作連接從服務(wù)器
?? $r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1));?? // 每次隨機(jī)連接的數(shù)據(jù)庫
}
以上是對每個選項的作用的實現(xiàn)代碼進(jìn)行了一個簡單的說明。
下面我們來看其連接的部分
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);
看到這,我覺得大家應(yīng)該對上面在介紹各個配置選項的代碼的時候其中的$r和$m是什么作用了。
現(xiàn)在我們來看 $r == $m ? false : $db_master ,如果數(shù)據(jù)庫讀寫不分離的情況下,讀寫是一臺服務(wù)器的話 傳給connect函數(shù)的值為false?;蛘呤侨绻侵鲝姆蛛x的寫的情況下傳給connect的值也為false。通過上面代碼我們看到,如果$r和$m不相等的情況下,會設(shè)置$db_master。其實也就是相當(dāng)于一臺備用的,如果選擇的$r服務(wù)器出現(xiàn)故障不能連接,將會去連接$db_master。
connect()函數(shù)的第三個參數(shù)其實是表示當(dāng)$db_config這臺服務(wù)器連接故障時是否選擇備用的連接。false表示不重連,其它值即表示重新連接。
其核心代碼如下
try{ if(empty($config['dsn'])) { $config['dsn'] = $this->parseDsn($config); } if(version_compare(PHP_VERSION,'5.3.6','<=')){ // 禁用模擬預(yù)處理語句 $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不為false,而是默認(rèn)的主服務(wù)器 trace($e->getMessage(),'','ERR'); return $this->connect($autoConnection,$linkNum); //出現(xiàn)異常,使用遞歸函數(shù)重新連接 }elseif($config['debug']){ E($e->getMessage()); } }
這種方式,對于主從式來說,$r和$m肯定不會相同。因此如果說在讀取數(shù)據(jù)的時候,選擇的那臺從服務(wù)器出現(xiàn)故障的話,那主服務(wù)器即是備用的,最后會去主服務(wù)器讀取。能保證數(shù)據(jù)讀取的時效性。
本文講解了thinkphp 分布式數(shù)據(jù)庫詳解,更多相關(guān)內(nèi)容請關(guān)注php中文網(wǎng)。
相關(guān)推薦:
關(guān)于ThinkPHP 5.數(shù)據(jù)庫的一些基本操作
以上就是thinkphp 分布式數(shù)據(jù)庫詳解的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號