带有MySQL簇的PHP / PDO

时间:2022-04-03 19:22:47

I have been asked to re-develop an old php web app which currently uses mysql_query functions to access a replicated database (4 slaves, 1 master).

我被要求重新开发一个旧的php web应用程序,它当前使用mysql_query函数来访问一个复制的数据库(4个奴隶,1个主人)。

Part of this redevelopment will move some of the database into a mysql-cluster. I usually use PDO to access databases these days and I am trying to find out whether or not PDO will play nicely with a cluster, but I can't find much useful information on the web.

这种重新开发的一部分将把一些数据库移动到一个mysql集群中。我这些天经常使用PDO访问数据库,我试图找出PDO是否可以很好地与群集一起使用,但我在网上找不到很多有用的信息。

Does anyone have any experience with this? I have never worked with a cluster before ...

有人对这个有经验么?我以前从未使用集群...

3 个解决方案

#1


3  

I've done this a couple different ways with different levels of success. The short answer is that your PDO connections should work fine. The options, as I see them, are as follows:

我已经用不同的方式完成了这项工作,取得了不同程度的成功。简短的回答是您的PDO连接应该正常工作。我认为,这些选项如下:

If you are using replication, then either write a class that handles connections to various servers or use a proxy. The proxy may be a hardware or a software. MySQL Proxy (http://docs.oracle.com/cd/E17952_01/refman-5.5-en/mysql-proxy.html) is the software load balancer I used to use and for the most part it did the trick. It automatically routes traffic between your readers and writers, and handles failover like a champ. Every now and then we'd write a query that would throw it off and have to tweak things, but that was years ago. It may be in better shape now.

如果使用复制,则编写一个处理各种服务器连接的类或使用代理。代理可以是硬件或软件。 MySQL代理(http://docs.oracle.com/cd/E17952_01/refman-5.5-en/mysql-proxy.html)是我以前使用的软件负载均衡器,并且在大多数情况下它都可以实现。它会自动在您的读者和作者之间路由流量,并像冠军一样处理故障转移。我们偶尔写一个会抛弃它并且不得不调整内容的查询,但那是多年前的事了。它现在可能处于更好的状态。

Another option is to use a standard load balancer and create two connections - one for the writer and the other for the readers. Your app can decide which connection to use based on the function it's trying to perform.

另一个选择是使用标准负载均衡器并创建两个连接 - 一个用于写入器,另一个用于读取器。您的应用可以根据其尝试执行的功能决定使用哪个连接。

Finally, you could consider using the max db cluster available from MySQL. In this setup, the MySQL servers are all readers AND writers. You only need one connection, but you'll need a load balancer to route all of the traffic. Max db cluster can be tricky if the indexes become out of sync, so tread lightly if you go with this option.

最后,您可以考虑使用MySQL提供的max db cluster。在此设置中,MySQL服务器都是读者和编写者。您只需要一个连接,但您需要一个负载均衡器来路由所有流量。如果索引变得不同步,则最大数据库集群可能会非常棘手,因此如果使用此选项,则可能会轻易实现。

Clarification: When I refer to connections what I mean is an address and port to connect to MySQL on - not to be confused with concurrent connections running on the same port.

澄清:当我提到连接时,我的意思是连接到MySQL的地址和端口 - 不要与在同一端口上运行的并发连接混淆。

Good luck!

祝你好运!

#2


1  

Have you considered hiding the cluster behind a hardware or software load balancer (e.g. HAProxy)? This way, the client code doesn't need to deal with the cluster at all, it sees the cluster as just one virtual server.

您是否考虑将群集隐藏在硬件或软件负载平衡器(例如HAProxy)之后?这样,客户端代码根本不需要处理集群,它将集群视为一个虚拟服务器。

You still need to distinguish applications that write from those that read. In our system, we put the slave servers behind the load balancer, and read-only applications use this cluster, while writing applications access the master server directly. We don't try to make this happen automatically; applications that need to update the database simply use a different server hostname and username.

您仍然需要区分写入的应用程序和读取的应用程序。在我们的系统中,我们将从属服务器放在负载均衡器后面,只读应用程序使用此集群,而编写应用程序则直接访问主服务器。我们不会尝试自动实现这一点;需要更新数据库的应用程序只需使用不同的服务器主机名和用户名。

#3


-1  

Write a wrapper class for the DB that has your connect and query functions in it...

为具有连接和查询功能的DB编写一个包装类...

The query function needs to look at the very first word to detect if it's a SELECT and use the slave DB connection, anything else (INSERT, UPDATE, RENAME, CREATE etc...) needs to go the MASTER server.

查询函数需要查看第一个单词以检测它是否为SELECT并使用从数据库连接,其他任何内容(INSERT,UPDATE,RENAME,CREATE等...)都需要进入MASTER服务器。

The connect() function would look at the array of slaves and pick a random one to use.

connect()函数将查看从属数组并选择要使用的随机数。

You should only connect to the master slave when you need to do an update (Most webpages shouldn't be updating the DB, only reading data... make sure you don't waste time connecting to the MASTER db when you won't use it)

您只需要在需要更新时连接到主从(大多数网页不应该更新数据库,只读取数据...确保您不会浪费时间连接到MASTER数据库用它)

You can also use a static variable in your class to hold your DB connections, that way connections are shared between instances of your DB class (i.e. you only have to open the DB connection once instead of everytime you call '$db = new DB()')

您还可以在类中使用静态变量来保存数据库连接,这样就可以在数据库类的实例之间共享连接(即,您只需打开数据库连接一次,而不是每次调用'$ db = new DB( )“)

Abstracting the database functions into a class like this also makes it easier to debug or add features

将数据库函数抽象为类似这样的类也可以更容易地调试或添加功能

#1


3  

I've done this a couple different ways with different levels of success. The short answer is that your PDO connections should work fine. The options, as I see them, are as follows:

我已经用不同的方式完成了这项工作,取得了不同程度的成功。简短的回答是您的PDO连接应该正常工作。我认为,这些选项如下:

If you are using replication, then either write a class that handles connections to various servers or use a proxy. The proxy may be a hardware or a software. MySQL Proxy (http://docs.oracle.com/cd/E17952_01/refman-5.5-en/mysql-proxy.html) is the software load balancer I used to use and for the most part it did the trick. It automatically routes traffic between your readers and writers, and handles failover like a champ. Every now and then we'd write a query that would throw it off and have to tweak things, but that was years ago. It may be in better shape now.

如果使用复制,则编写一个处理各种服务器连接的类或使用代理。代理可以是硬件或软件。 MySQL代理(http://docs.oracle.com/cd/E17952_01/refman-5.5-en/mysql-proxy.html)是我以前使用的软件负载均衡器,并且在大多数情况下它都可以实现。它会自动在您的读者和作者之间路由流量,并像冠军一样处理故障转移。我们偶尔写一个会抛弃它并且不得不调整内容的查询,但那是多年前的事了。它现在可能处于更好的状态。

Another option is to use a standard load balancer and create two connections - one for the writer and the other for the readers. Your app can decide which connection to use based on the function it's trying to perform.

另一个选择是使用标准负载均衡器并创建两个连接 - 一个用于写入器,另一个用于读取器。您的应用可以根据其尝试执行的功能决定使用哪个连接。

Finally, you could consider using the max db cluster available from MySQL. In this setup, the MySQL servers are all readers AND writers. You only need one connection, but you'll need a load balancer to route all of the traffic. Max db cluster can be tricky if the indexes become out of sync, so tread lightly if you go with this option.

最后,您可以考虑使用MySQL提供的max db cluster。在此设置中,MySQL服务器都是读者和编写者。您只需要一个连接,但您需要一个负载均衡器来路由所有流量。如果索引变得不同步,则最大数据库集群可能会非常棘手,因此如果使用此选项,则可能会轻易实现。

Clarification: When I refer to connections what I mean is an address and port to connect to MySQL on - not to be confused with concurrent connections running on the same port.

澄清:当我提到连接时,我的意思是连接到MySQL的地址和端口 - 不要与在同一端口上运行的并发连接混淆。

Good luck!

祝你好运!

#2


1  

Have you considered hiding the cluster behind a hardware or software load balancer (e.g. HAProxy)? This way, the client code doesn't need to deal with the cluster at all, it sees the cluster as just one virtual server.

您是否考虑将群集隐藏在硬件或软件负载平衡器(例如HAProxy)之后?这样,客户端代码根本不需要处理集群,它将集群视为一个虚拟服务器。

You still need to distinguish applications that write from those that read. In our system, we put the slave servers behind the load balancer, and read-only applications use this cluster, while writing applications access the master server directly. We don't try to make this happen automatically; applications that need to update the database simply use a different server hostname and username.

您仍然需要区分写入的应用程序和读取的应用程序。在我们的系统中,我们将从属服务器放在负载均衡器后面,只读应用程序使用此集群,而编写应用程序则直接访问主服务器。我们不会尝试自动实现这一点;需要更新数据库的应用程序只需使用不同的服务器主机名和用户名。

#3


-1  

Write a wrapper class for the DB that has your connect and query functions in it...

为具有连接和查询功能的DB编写一个包装类...

The query function needs to look at the very first word to detect if it's a SELECT and use the slave DB connection, anything else (INSERT, UPDATE, RENAME, CREATE etc...) needs to go the MASTER server.

查询函数需要查看第一个单词以检测它是否为SELECT并使用从数据库连接,其他任何内容(INSERT,UPDATE,RENAME,CREATE等...)都需要进入MASTER服务器。

The connect() function would look at the array of slaves and pick a random one to use.

connect()函数将查看从属数组并选择要使用的随机数。

You should only connect to the master slave when you need to do an update (Most webpages shouldn't be updating the DB, only reading data... make sure you don't waste time connecting to the MASTER db when you won't use it)

您只需要在需要更新时连接到主从(大多数网页不应该更新数据库,只读取数据...确保您不会浪费时间连接到MASTER数据库用它)

You can also use a static variable in your class to hold your DB connections, that way connections are shared between instances of your DB class (i.e. you only have to open the DB connection once instead of everytime you call '$db = new DB()')

您还可以在类中使用静态变量来保存数据库连接,这样就可以在数据库类的实例之间共享连接(即,您只需打开数据库连接一次,而不是每次调用'$ db = new DB( )“)

Abstracting the database functions into a class like this also makes it easier to debug or add features

将数据库函数抽象为类似这样的类也可以更容易地调试或添加功能