如下均是使用翻譯軟件翻譯的!php
Note: This is part one in our Extending MySQL with PHP's MySQLnd Series, read part 2 on Advanced Read/Write Splitting, and part 3 for an Introduction to MySQL's innodb memcached interface.mysql
注意:這是一個在咱們的MySQL和PHP擴展的MySQLnd系列,讀第2部分先進的讀/寫分離,和第3部分介紹MySQL的innodb memcached接口。web
MySQL has always been the default go-to database server for pairing with PHP—it has been the go-to database since almost the inception of the language. Sure, some people use PostgreSQL, or SQL Server, or Oracle, but for the web workload, MySQL is usually the relational database of choice.sql
MySQL一直默認首選數據庫服務器與PHP-it配對以來一直成爲數據庫幾乎初始的語言。固然,有些人使用PostgreSQL,或SQL服務器,或甲骨文,但對於網絡負載,MySQL一般是關係數據庫的選擇。數據庫
This was due mostly in part because it was so easy to get going. Libmysqlclient was bundled with PHP itself until it was re-licensed under the GPL. This change meant it was no longer possible to bundle with PHP and it was removed.json
主要緣由是它是那麼容易實現。Libmysqlclient捆綁了PHP自己直到GPL從新許可。這種變化意味着不再可能與PHP捆綁,它被刪除了。bash
This made the compilation process for PHP slightly more difficult, requiring that libmysqlclient be available on the host system.服務器
這使得PHP的編譯過程稍微困難,若是要求libmysqlclient在主機系統可用的話。網絡
Given the widespread nature of PHP, and the fact it was the most popular single language to use MySQL, this wasn’t ideal for Oracle (then Sun) and so they came to an agreement: they’d build the MySQL Native Driver—a PHP licensed contribution to PHP that would allow access to MySQL without libmysqlclient.session
鑑於PHP的廣泛性和事實上這是最受歡迎的使用MySQL的單一語言,這並非甲骨文(當時太陽)所理想的,因此他們來到了一個協議:他們會創建原生的MySQL驅動——符合PHP許可的但不須要libmysqlclient 而容許訪問MySQL。
MySQL Native Drive (mysqlnd) was added in PHP 5.3 and has been the default since PHP 5.4 (though you can still compile against libmysqlclient). It brings extra features, better performance, and better memory usage than libmysqlclient.
MySQL(mysqlnd)是被添加到PHP 5.3和PHP 5.4以來一直默認(儘管你仍然能夠對libmysqlclient編譯)。它帶來了額外的功能,更好的性能,比libmysqlclient更好的內存使用狀況。
From the MySQL manual:
The mysqlnd library is using PHP internal C infrastructure for seamless integration into PHP. In addition, it is using PHP memory management, PHP Streams (I/O abstraction) and PHP string handling routines. The use of PHP memory management by mysqlnd allows, for example, memory savings by using read-only variables (copy on write) and makes mysqlnd apply to PHP memory limits.
mysqlnd庫是使用PHP內部C基礎設施無縫集成到PHP。此外,它是使用PHP內存管理、PHP流抽象(I / O)和PHP字符串處理例程。mysqlnd容許使用PHP內存管理,例如,儲蓄的內存使用只讀變量(複製寫),使mysqlnd適用於PHP內存限制。
Additionally it has a plugin architecture, and a number of plugins are available.
另外它有一個插件架構,和許多插件同樣可用。
To install, just compile one of the three MySQL extensions. In each instance, do not explicitly specify the path to the libmysqlclient library.
The three libraries are:
Note: If you install ext/mysql or ext/mysqli, ext/pdo_mysql is enabled automatically.
You can select an extension by choosingone or more of the following configure flags:
--with-mysql
--with-mysqli
--with-pdo-mysql
If you are using Debian, or Ubuntu, you can easily install the php5-mysqlnd package:
$ sudo apt-get install php5-mysqlnd
This will remove the libmysqlclient-based php5-mysql package, and includes all three extensions.
Aside from the performance benefits, the biggest benefit to mysqlnd are it’s plugins. These plugins are available via PECL, and can be installed easily using:
除了性能好處,最大的好處是mysqlnd插件。這些插件能夠經過PECL,能夠輕鬆地安裝使用:
$ pecl install mysqlnd_<name>
The available (stable) plugins are:
Because these plugins are for mysqlnd itself, they apply to all three extensions.
The most useful plugin is mysqlnd_ms, or master/slave. This plugin allows you to transparently—albeit somewhat naively—split reads and writes between different servers.
最有用的插件mysqlnd_ms或master/slave。這個插件容許您 顯然—儘管 在有些 自然—分離 不一樣服務器之間的讀和寫。
Once you have installed using pecl, you need to configure both the php.ini
, and the mysqlnd_ms configuration file.
In php.ini
(or mysqlnd_ms.ini
on Debian-like systems):
extension=mysqlnd_ms.so mysqlnd_ms.enable=1 mysqlnd_ms.config_file=/path/to/mysqlnd_ms.json
Then you need to create the mysqlnd_ms.json
file. This file defines the master and slave servers, as well as the read/write splitting and load balancing strategies.
而後您須要建立mysqlnd_ms.json文件。這個文件定義了主、從服務器,以及讀/寫分離和負載平衡策略。
1 { 2 "appname": { 3 "master": { 4 "master_0": { 5 "host": "master.mysql.host", 6 "port": "3306", 7 "user": "dbuser", 8 "password": "dbpassword", 9 "db": "dbname" 10 } 11 }, 12 "slave": { 13 "slave_0": { 14 "host": "slave.mysql.host", 15 "port": "3306" 16 "user": "dbuser", 17 "password": "dbpassword", 18 "db": "dbname" 19 }, 20 } 21 } 22 }
The only required setting is the host. All others are optional.
惟一須要設置主機。其餘都是可選的。
Additionally, mysqlnd_ms can do simple load balancing in one of several strategies:
另外,mysqlnd_ms能夠作爲簡單的負載均衡策略之一:
It is important to understand that unless you use the last strategy and maintain state yourself, every single request will execute the load balancing strategy in isolation. So, round-robin applies to each query within the same request, and it isn’t that one server is picked per request in sequential order as you might expect of an actual hardware or software load balancer.
重要的是要理解,除非你使用過去的策略和維護本身的狀態,每個獨立請求將被隔離執行負載均衡策略。因此,循環適用於每一個查詢在相同的請求裏,並且那不是一個服務器被按順序執行每一個請求的你可能會認爲像實際的硬件或軟件負載平衡器。
With that in mind, I would recommend that youdo not use the load balancing aspect of this plugin, and instead use an actual load balancer for your slaves—such as haproxy—and simply point the configuration to the load balancer as the only slave.
記住這一點,我建議不使用這個插件的負載均衡策略,而是爲從服務器使用一個實際負載均衡器--做爲高可用代理--簡單點配置負載平衡器做爲惟一的奴隸。
By default mysqlnd_ms will transparently route all queries starting with SELECT
to the slave servers, and anything else to the master.
默認狀況下mysqlnd_ms將透明地路由全部查詢 從把 ‘SELECT’路由到從服務器開始,和其餘查詢路由到的主服務器。
This is both good and bad. Being transparent, it means zero changes to your code. But it is also simplistic, and does not analyze the query to ensure it is actually a read-only query.
這樣有好有壞。是透明的,這意味着零修改您的代碼。但它也簡單,不分析查詢,以確保它確實是一個只讀查詢。
Not only will it not send a query that starts with (SELECT to the master, it will also send a write query using SELECT … INTO to the slave, which could be a disaster.
並非全部以SELECT開始的查詢都要到從服務器,好比SELECT ...INTO 對於從服務器來講 是一場災難!
Luckily, the plugin includes the ability to hint which server the query should be sent to. This is done by placing one of three SQL hint constants in the query:
幸運的是,這個插件包括暗示的能力用來指定應該發送到哪一個服務器查詢。這是經過將SQL查詢中包含三種提示常量:
MYSQLND_MS_MASTER_SWITCH
—Run the statement on the masterMYSQLND_MS_SLAVE_SWITCH
—Run the statement on the slaveMYSQLND_MS_LAST_USED_SWITCH
—Run the statement on whichever server was last used
These three constants are simple placeholders for strings, ms=master, ms=slave, and ms=last_usedrespectively. However these strings may change in the future and therefore the constants should be used.
這三個簡單的佔位符字符串常量,ms=master, ms=slave, and ms=last_usedrespectively。然而這些字符串在將來可能會改變,所以應該使用常量。
To use a SQL hint, add a comment before the query with whichever one you wish to use. The easiest way to do this is to use sprintf() which will replace placeholders (in this case %s,the string placeholder) for the given arguments.
使用SQL提示,添加一個評論以前,與任何一個您但願使用的查詢。最簡單的方法是使用sprintf()將替換佔位符(在本例中% s,字符串佔位符)爲給定的參數。
For example, to send a SELECT
to the master:
$sql = sprintf("/*%s*/ SELECT * FROM table_name;", MYSQLND_MS_MASTER_SWITCH);
Or, to send a non-SELECT to a slave:
$sql = sprintf("/*%s*/ CREATE TEMPORARY TABLE `temp_table_name` SELECT * FROM table_name;", MYSQLND_MS_SLAVE_SWITCH);
The last hint will let you ensure that the same connection is used as for the previous query. This is particularly useful for ensuring that you switch to reading from the master after data has been modified but potentially not yet replicated, or when performing transactions that include both read and write statements.
最後提示會讓你確保使用以前的查詢相同的鏈接。這是特別有用的,以確保您切換到主服務器讀取,好比數據修改後但可能沒有複製,或當執行事務,包括讀和寫語句。
1 if ($request->isPost() && $form->isValid()) { 2 $user>setValues($form->getValues()); 3 $user->save(); 4 } 5 6 $sql = sprintf("/*%s*/ SELECT * FROM user_session WHERE user_id = :user_id", MYSQLND_LAST_USED_SWITCH);
This will use the master for the query if—and only if—the master was previously used. In this case the master is used if the user data is updated (with $user->save()).
The mysqlnd_ms plugin is incredibly useful, especially when you want to move large legacy applications to using distributed read/write. While it’s not perfect, it should get you 80-90% of the way there for most applications without changing a single line of code.
mysqlnd_ms插件很是有用,尤爲是當您想要移動大型遺留應用程序使用分佈式讀/寫。雖然不是完美的,它應該讓你80 - 90%的方式沒有改變,對於大多數應用程序一行代碼。
In the second installment in this series, we’ll look at more advanced usage of the mysqlnd_ms plugin.
在本系列的第二部分中,咱們將看看mysqlnd_ms插件的更高級的用法。
Note: This is part one in our Extending MySQL with PHP's MySQLnd Series, read part 2 on Advanced Read/Write Splitting, and part 3 for an Introduction to MySQL's innodb memcached interface.
原文地址:
https://blog.engineyard.com/2014/easy-read-write-splitting-php-mysqlnd