如下均是使用翻译软件翻译的!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