简体   繁体   English

从SLAVE PDO SELECT并INSERT到MASTER

[英]PDO SELECT from SLAVE and INSERT into MASTER

is there any chance to set in PDO settings that SELECT's will be executed on SLAVE DB server and Insert & Update & DELETE will be executed on MASTER DB server, or I need to create PHP handler to do that? 是否有机会在PDO设置中设置SELECT将在SLAVE DB服务器上执行,并且Insert&Update&DELETE将在MASTER DB服务器上执行,或者我需要创建PHP处理程序来执行此操作?

Situation: 情况:

We have Master - Master replication for MySQL. 我们有Master - Master复制MySQL。 We are going to add two new servers so it will be - Master / Slave - Master / Slave . 我们将添加两个新服务器,因此它将是 - / - /

I want to create some handling for SELECT queries. 我想为SELECT查询创建一些处理。 I want execute SELECT queries on SLAVE instead of MASTER and all UPADTE&INSERT&DELETE queries will be executed on MASTER . 我希望在SLAVE而不是MASTER上执行SELECT查询,并且所有UPADTE和INSERT&DELETE查询都将在MASTER上执行。 Is this possible with some setting? 这有可能通过一些设置吗?

Thanks! 谢谢!

No, you can't configure PDO or any of PHP's database extensions to do this. 不,您无法配置PDO或任何PHP的数据库扩展来执行此操作。 That is simply because each PDO (or MySQLi, etc.) instance represents a single connection, to a single server. 这仅仅是因为每个PDO(或MySQLi等)实例代表单个服务器的单个连接。

So yes, you'll need a handler that is aware of multiple connections to do that. 所以是的,你需要一个知道多个连接的处理程序来做到这一点。 Some popular ORMs and other database-abstraction layers do provide such functionality. 一些流行的ORM和其他数据库抽象层确实提供了这样的功能。

I recommend not doing it even if you could. 即使你可以,我建议不要这样做。 Replication is "asynchronous". 复制是“异步”的。 That is, when you insert into the Master, there is no assurance that it will arrive at the Slave before you try to read it. 也就是说,当你插入Master时,无法保证在你尝试阅读它之前它会到达Slave。 Nor even any guarantee that it will arrive today! 甚至不保证今天会到货!

If you user posts a comment on a blog, and then goes to a page that shows the comment, they will be annoyed if the comment does not show. 如果您的用户在博客上发布评论,然后转到显示评论的页面,则如果评论未显示,则会感到恼火。 They may assume that the comment was lost and then repost it. 他们可能会认为评论已丢失,然后重新发布。 This causes you grief when users complain about double-posting. 当用户抱怨双重发布时,这会让感到悲伤。

This is called "critical read". 这被称为“批判性阅读”。 This simple way to avoid the mess is to be careful about what you send to the Slaves -- namely nothing that would lead to "disappearing" posts. 这种避免混乱的简单方法是要小心你发送给奴隶的东西 - 即没有任何会导致“消失”的帖子。

There are various "proxy" packages that allow from the read-write split you describe; 有各种“代理”包允许你描述的读写分割; some try to avoid the "critical read", but I don't trust them. 有些人试图避免“批判性阅读”,但我不相信他们。

A Galera Cluster (see PXC, MariaDB), does synchronous reads, so it can avoid the critical read problem. Galera集群(参见PXC,MariaDB)执行同步读取,因此可以避免关键的读取问题。 (There is, however, a setting you need to apply.) (但是,您需要应用一个设置。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM