1. ?? ???????:
tp? ???? ??????? ?? ?? ??? ?? ?????.
'DB_DEPLOY_TYPE' => 1,// ?????? ?? ??: 0 ?? ???(?? ??) , 1? ??(???-???? ??)
2. ???-???? ??? ??-?? ??? ??????
master ?????? n?? ?? ???? ??????? ???, ???? ??????? ?? ???? ??????? ??? ?? ? ????. ???-???? ??????? ?? ???? ??? ??????? ???? ??, ?? ???? ???? ??????? ?????. ??? ??????? ???? ??????? ?? ??? ???? ?????. ?????? ???? ???? ??? ?? ??????? ???? ???? ??? ?? ?????? ??? ???? ???? ??????? ???? ?? ??????? ??? ?? ?? ??? ???? ????. ???? ??? ?? ?? ???? ???-???? ??????? ???? ?????.
3. ??? ??
1. ?? ?????? ??
?? ? ?? ??????? ?? ??? ?? ?????. ?? ???? ?? ??? ???? ???.
'DB_TYPE' => 'mysql', 'DB_HOST' => '192.168.5.102', 'DB_NAME' => 'databasename', 'DB_USER' => 'user', 'DB_PWD' => 'password', 'DB_PORT' => '3306', 'DB_PREFIX' => 'onmpw_',
??? ??? ? ????? ???. ???? ?? ?????? ?????.
2. ?? ?????? ??
?? ?????? ??? ?? ?????. ?? ??????? ??? ????? ???????.
'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ù)器序號
? ??? ?? ?? ??????? ?????.
?? ??? ???????
'DB_HOST'
?? ??????? ?? ??? ?? ?? ?? ?? ?? ??? ???? ? ??? ??? ???? ???. ???-???? ??? ?? ?? ??? ?? ??????? ???? ???.
對于下面的用戶名和密碼還有監(jiān)聽端口之類的,當然是有幾個就寫幾個。如果各個數(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 // 默認單數(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ù)就是用來進行分布式連接的,如果’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)); // 每次隨機連接的數(shù)據(jù)庫 } } }else{ // 讀寫操作不區(qū)分服務(wù)器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次隨機連接的數(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)); // 每次隨機連接的數(shù)據(jù)庫
其中$this->config[‘master_num’]表示主服務(wù)器的數(shù)量。
'DB_SLAVE_NO'=> ''
指定從服務(wù)器的序號,用于讀取數(shù)據(jù)。如果不設(shè)置,則根據(jù)主服務(wù)器的數(shù)量計算書從服務(wù)器的數(shù)量,然后從中隨機選取一臺進行讀取。
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)); // 每次隨機連接的數(shù)據(jù)庫
}
以上是對每個選項的作用的實現(xià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。其實也就是相當于一臺備用的,如果選擇的$r服務(wù)器出現(xiàn)故障不能連接,將會去連接$db_master。
connect()函數(shù)的第三個參數(shù)其實是表示當$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,而是默認的主服務(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 ?? ??????? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Apple? ?? iOS18, iPadOS18 ? macOS Sequoia ??? ????? ?? ??????? ??? ??? ???????. ? ??? ???? ??? ??? ????? ??? ??? ???? ?? ??? ? ??? ???????. ??? ???? ?? ?? ?? ??? '???'??? ??? ???????. ? ??? ???? ??? ?? ?????? ???? ?? ???? ???? ??? ?? ? ???? ?????. "???" ??? ??? ?????? ???? ?? ??? ??? ???, ?? ?????? ???? ???? ?? ??? ?? ???? ?? ?? ?????? ???? ?? ?? ????? ?? ???? ?????. ???? ? ?? ??? ??? ??? ???.

MySQL? ?? ?? ??? ?????? ?? ??????. 1) ?????? ? ??? ?? : CreateAbase ? CreateTable ??? ??????. 2) ?? ?? : ??, ????, ?? ? ??. 3) ?? ?? : ??, ?? ?? ? ?? ??. 4) ??? ?? : ??, ??? ?? ? ??? ??????. 5) ??? ?? : ??? ??, ??? ??? ??? ??????.

Oracle? ?????? ?? ?? ? ??? ???? ??? ? ERP ???? ????????. 1. Oracle? ???????? ???? ??? ? ERP ???? ????? ??? ? ???? ?????. 2. OracleCloud? AWS? Azure? ???? IAA, PAAS ? SAAS ???? ?????. 3. E-BusinessSuite ? FusionApplications? ?? Oracle? ERP ???? ??? ??? ????? ? ??????.

MySQL? ?? ?? ??? ?????? ?? ?????, ?? ???? ???? ????? ???? ???? ? ?????. ?? ???? ????? ??, ?? ???, ?? ?? ? ?? ??? ?????. ??? ??? ??? ??, ??? ?? ? ?? ? ?? ??? ?? ?? ??? ?????. ???? ???? SQL ??, ??? ?? ? ??? ???? ??? ???? ??? ??, ??? ? ?? ? ??? ??? ?????.

MySQL? ? ?? ???? ? ??? ?? ???? ???? ?? ??, ??? ? ?? ???? ??? ????. 1) PostgreSQL? ???? MySQL? ??? ?? ? ?? ?? ?? ???? ? ? ?????. 2) Oracle? ??? ? MySQL? ?? ??? ??? ???? ?? ?? ???? ? ??? ????. 3) Microsoft SQL Server? ???? MySQL? ??? ??? ?? ????? ? ?????. 4) MongoDB? ?? MySQL? ??? ? ??? ? ???? ??? ? ?????.

Laravel? ThinkPHP? ?? ???? PHP ??? ???? ??? ?? ? ??? ??? ????. ? ??? ? ?? ??? ???? ??, ?? ? ?? ??? ???? ???? ?? ???? ??? ?? ??? ?? ? ??? ? ? ??? ?????.

MySQL? ???? ?? ???? ??? ????? ?????. 1.MySQL? ??? ???????? CRUD ??? SQL? ?????. 2. ??? ???? ?? ??? ????? ???????. 3. ??, ????, ?? ? ???? ??? ??? ??????. 4. Orderby, Where and Join? ??? ??? ??? ? ????. 5. ???? ??? ???? ??? ???? ?? ??? ???????. 6. ??? ???? ??? ??, ??? ??? ?? ?? ? ??? ????? ??? ?????.

MySQL? ??, ???, ?? ??? ? ???? ??? ?? ?????. 1.MYSQL? ???? ??? ?? ? ?? ??? ???? ?? ??? ?? ? ?? ?? ??? ?????. 2. ??-?? ???? ? ?? ???? ??? ???? ???? ? ?? ???? ?????. 3. ???? ?? ??? ?? ?? ? ????? ??? ?????. 4. ??? ?? ?? ??? ?? ??? ??? ???? ?????.
