Detailed explanation of thinkphp distributed database
Jun 15, 2018 am 11:36 AM1. What is a distributed database:
The distributed database of tp is mainly configured through this configuration:
'DB_DEPLOY_TYPE' => 1,// Database deployment method: 0 centralized (single server), 1 distributed (master-slave server)
2. What is the read-write separation of the master-slave server:
The master-slave database means that a master database has n corresponding slave databases, while a slave database can only have one corresponding slave database. Write operations in the master-slave database require the use of the master database, while read operations use the slave database. The master database and the slave database always maintain data consistency. The principle of keeping the database consistent is that when the master database data changes, the operation will be written to the master database log, and the slave database will continue to read the master database log and save it to its own log system, and then execute it. , thus keeping the master-slave database consistent.
3. Detailed explanation
1. Single database Connection
When used, the connection configuration of a single database is very simple. We only need to configure some 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_',
It can be used after the setting is completed. The default is a single database connection.
2. Distributed database connection
The connection to a single database is very simple. Let’s focus on the analysis of the distributed database connection.
'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ù)器序號
Follow the above configuration to connect to the distributed database.
Let’s look at the following options
'DB_HOST'
Distributed database, if there are several servers, you need to fill in several server addresses. Separate each address with a comma. If it is master-slave distribution, the previous address must be the address of the master database.
對于下面的用戶名和密碼還有監(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)); // 每次隨機連接的數(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ù)量,然后從中隨機選取一臺進(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)); // 每次隨機連接的數(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ù)庫的一些基本操作
The above is the detailed content of Detailed explanation of thinkphp distributed database. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
