本文實例分析了ThinkPHP框架分布式數據庫連接方法。分享給大家供大家參考,具體如下:
Thinkphp作為國內的一款流行框架,相信使用的人一定不在少數。本篇我們來分析一下Thinkphp中比較重要的一部分——分布式數據庫的連接。
當然了,我們在這里不是去將如何使用模型去對數據庫進行增刪改查。我們是對其底層的連接代碼做一個分析,可以幫助大家更好的理解thinkphp對數據庫的操作。方便我們以后的使用。
一、單一數據庫的連接
在使用的時候,單一數據庫的連接配置非常簡單。我們只需要在配置文件中配置一下的信息即可。
1
2
3
4
5
6
7
|
'DB_TYPE' => 'mysql' , 'DB_HOST' => '192.168.5.102' , 'DB_NAME' => 'databasename' , 'DB_USER' => 'user' , 'DB_PWD' => 'password' , 'DB_PORT' => '3306' , 'DB_PREFIX' => 'onmpw_' , |
設置完成以后就可以使用了。默認情況下就是單一的數據庫連接。
二、分布式數據庫的連接
單一數據庫的連接很簡單,我們著重分析一下分布式數據庫的連接。
1
2
3
4
5
6
7
8
9
10
11
|
'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, // 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器) 'DB_RW_SEPARATE' => true, // 數據庫讀寫是否分離 主從式有效 'DB_MASTER_NUM' => 1, // 讀寫分離后 主服務器數量 'DB_SLAVE_NO' => '' , // 指定從服務器序號 |
按照以上配置就可以連接分布式數據庫了。
下面我們看下面幾個選項
'DB_HOST'
分布式數據庫,有幾臺服務器就要填寫幾個服務器地址,每個地址之間用逗號隔開。如果是主從式分布的話,前面的地址要是主數據庫的地址。
對于下面的用戶名和密碼還有監聽端口之類的,當然是有幾個就寫幾個。如果各個數據庫的用戶名和密碼都一樣的話,可以只寫一個。
對于這些選項的解析的代碼如下
1
2
3
4
5
6
7
|
$_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 表示的是集中式(也就是單一服務器)。
該選項的實現是在類 Think\Db\Dirver中
1
2
3
4
5
6
7
8
|
protected function initConnect( $master =true) { if (! empty ( $this ->config[ 'deploy' ])) // 采用分布式數據庫 $this ->_linkID = $this ->multiConnect( $master ); else // 默認單數據庫 if ( ! $this ->_linkID ) $this ->_linkID = $this ->connect(); } |
$this->config['deploy']
表示的就是'DB_DEPLOY_TYPE'
這個配置選項,上面的配置在使用之前都已經經過解析了,配置項都在$this->config
數組中。至于是如何解析配置文件的,這里我們不做介紹,感興趣的可以參考Think\Db類。
$this->multiConnect()
函數就是用來進行分布式連接的,如果'DB_DEPLOY_TYPE'
選項設置為1,該函數就會執行。否則直接執行$this->connect()
函數。
'DB_RW_SEPARATE'=>true
true 表示讀寫分離;false表示讀寫不分離。
這里需要注意的是,讀寫分離是以主從式數據庫系統為前提的。該選項設置為true的時候主數據庫寫,從數據庫讀。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
if ( $this ->config[ 'rw_separate' ]){ // 主從式采用讀寫分離 if ( $master ) // 主服務器寫入 $r = $m ; else { if ( is_numeric ( $this ->config[ 'slave_no' ])) { // 指定服務器讀 $r = $this ->config[ 'slave_no' ]; } else { // 讀操作連接從服務器 $r = floor (mt_rand( $this ->config[ 'master_num' ], count ( $_config [ 'hostname' ])-1)); // 每次隨機連接的數據庫 } } } else { // 讀寫操作不區分服務器 $r = floor (mt_rand(0, count ( $_config [ 'hostname' ])-1)); // 每次隨機連接的數據庫 } |
$this->config['rw_separate']
為true的時候采用讀寫分離,為false的時候讀寫不分離。讀寫分離為什么必須是主從式的呢?因為從服務器不能寫只能讀,如果向從服務器寫入數據的話,數據是沒法同步的。這樣就會造成數據不一致的情況。所以說,如果我們的系統是主從式的話,我們必須采用讀寫分離。也就是說DB_RW_SEPARATE選項必須配置為true。
'DB_MASTER_NUM'=>1
該選項后面的數字表示讀寫分離后,主服務器的數量。因此該選項也是用于主從式數據庫系統。
下面的代碼是選擇主服務器。
1
|
$m = floor (mt_rand(0, $this ->config[ 'master_num' ]-1)); |
主從式數據庫讀取的時候選擇從服務器讀的核心代碼
其中$this->config['master_num']
表示主服務器的數量。
'DB_SLAVE_NO'=> ''
指定從服務器的序號,用于讀取數據。如果不設置,則根據主服務器的數量計算書從服務器的數量,然后從中隨機選取一臺進行讀取。
1
2
3
4
5
6
|
if ( is_numeric ( $this ->config[ 'slave_no' ])) { // 指定服務器讀 $r = $this ->config[ 'slave_no' ]; } else { // 讀操作連接從服務器 $r = floor (mt_rand( $this ->config[ 'master_num' ], count ( $_config [ 'hostname' ])-1)); // 每次隨機連接的數據庫 } |
以上是對每個選項的作用的實現代碼進行了一個簡單的說明。
下面我們來看其連接的部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 ); |
看到這,我覺得大家應該對上面在介紹各個配置選項的代碼的時候其中的$r和$m是什么作用了。
現在我們來看 $r == $m ? false : $db_master ,如果數據庫讀寫不分離的情況下,讀寫是一臺服務器的話 傳給connect函數的值為false?;蛘呤侨绻侵鲝姆蛛x的寫的情況下傳給connect的值也為false。通過上面代碼我們看到,如果$r和$m不相等的情況下,會設置$db_master。其實也就是相當于一臺備用的,如果選擇的$r服務器出現故障不能連接,將會去連接$db_master。
connect()函數的第三個參數其實是表示當$db_config這臺服務器連接故障時是否選擇備用的連接。false表示不重連,其它值即表示重新連接。
其核心代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
try { if ( empty ( $config [ 'dsn' ])) { $config [ 'dsn' ] = $this ->parseDsn( $config ); } if (version_compare(PHP_VERSION, '5.3.6' , '<=' )){ // 禁用模擬預處理語句 $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,而是默認的主服務器 trace( $e ->getMessage(), '' , 'ERR' ); return $this ->connect( $autoConnection , $linkNum ); //出現異常,使用遞歸函數重新連接 } elseif ( $config [ 'debug' ]){ E( $e ->getMessage()); } } |
這種方式,對于主從式來說,$r和$m肯定不會相同。因此如果說在讀取數據的時候,選擇的那臺從服務器出現故障的話,那主服務器即是備用的,最后會去主服務器讀取。能保證數據讀取的時效性。
但是,總感覺現在還不太完善。如果說有多臺從服務器,在讀取的時候選擇的那臺從服務器和主服務器都出現了故障,那數據豈不是就讀取失敗了。這時候如果能再次讀取其它的從服務器的話那應該是更有保障。當然了,目前的thinkphp的功能已經相當完善,足夠我們使用了。但是還是希望thinkphp以后越來越完善。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。