配置具有多个数据库连接的 php 应用程序
我有一个包含多个数据库的 php 页面。我一直读到多个数据库不好,你应该将它们全部合并为一个,但我想将它们分开以便更好的组织。
假设我使用多个数据库。那很糟糕吗?现在我发现 mysql_max_connections 有问题。所以我正在研究用于获取连接的单例方法。但单例类似乎仅用于一个数据库连接。是这样吗?我需要为每个数据库创建一个单独的单例类吗?
澄清:我预计数据库会变得相当大。我在一个数据库上已经有数百个表,并且还添加了更多新内容。我听说您不希望任何特定数据库中的表超过数千个。也许最好的答案是以某种方式将所有内容组合起来,但这需要进行重大修改,所以我想坚持使用多个数据库。我只想知道组织多个数据库连接的最有效方法是什么。
I have a php page with several databases. I keep reading that multiple databases is bad and you should combine them all into one, but I want to keep things separate for better organization.
So let's say I stay with multiple databases. Is that bad? Now I'm seeing problem with mysql_max_connections. So I'm looking into singleton methods for getting connections. But the singleton classes seem to be only for one database connection. Is that the case? Do I need to create a separate singleton class for each database?
Clarification: I anticipate the databases getting fairly large. I already have several hundred tables on one database and more are added with new content. I have heard that you don't want more then several thousand tables in any particular database. Maybe the best answer is to get everything combined somehow, but it would take a major overhaul so I would like to stick with multiple databases. I just want to know what is the most efficient way to organize connections for multiple databases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
典型的单例看起来像:
但是你可以通过拥有多个实例来扩展它,但彼此之间略有不同:
这样你就可以用一个类来统治它们;)并且仍然遵循单例模式
Typical singleton looks like:
But you can extend it by having multiple instances but slightly different from each other:
That way you can have one class to rule them all ;) and still follow singleton pattern
使用多个数据库相当容易。 mysql_connect() 返回一个资源值,该值标识连接。例如,您可以将该值添加到 mysql_query():
using multiple databases is fairly easy. mysql_connect() returns a Resource value, which Identifies the connection. You can add that value to, for example, mysql_query():
我不确定我是否理解这个问题,你的意思是“所以我正在研究用于获取连接的单例方法”,我知道单例是什么,但我真的不明白你打算做什么。更多,我不明白 mysql_max_connections 与所有这些有何关系,它与您的要求完全无关。
我可能建议您为所有数据库创建一个通用接口,例如一个名为“DB”的包装类(作为单例,如果您愿意的话),并定义一些方法来查询数据库,以便被调用者不必“知道”从哪个数据库获取数据。
但所有这些似乎毫无意义...
编辑:(另请参阅注释)
1)要显着减少连接数,请使用 mysql_pconnect()。但仔细阅读文档,我知道存在一些担忧。
2)通用代码。这需要大量的工作,但这是值得的。我这样做:
如果将来您要放弃 mysql 来执行其他操作,那么它会在不更改被调用者代码的情况下工作。两个数据库。
编辑(2):呃哦,看来我终于明白了你的问题。你的数据库(或者更好的是模式)不止一个,但DBMS只是一个运行的MySQL实例。在本地主机上?如果是这样,您可以简单地编辑您的查询。
“从数据库.表中选择*,其中...”
I'm not sure I understood the question, what you mean for "so I'm looking into singleton methods for getting connections", I know what a singleton is but I really don't see what you are planning to do. More, I don't see how mysql_max_connections is related to all of this, it has quite nothing to do with what you ask.
I may suggest you to create a common interface to all the databases, like a wrapping class named "DB" (as a singleton, if you want), and define some methods to query the databases so that the callee doesn't have to "know" what DB to get the data from.
But all of this seems pointless...
EDIT: (see comments, also)
1) To significantly reduce the number of connections, use mysql_pconnect(). But read the docs, carefully, I know there are some concerns.
2) Generic code. It'll take a bunch of work, but it's worth. I do this way:
If in the future you'll drop mysql for anything else, it'll work without changing callee code. The same if you merge the two databases.
EDIT (2): uh oh, it seems I've finally understood what you ask. Your databases (or better, schemata) are more than one, but the DBMS is just a single instance of MySQL running on localhost? If it's like this you can simply edit your queries.
"SELECT * FROM database.table WHERE ..."