配置具有多个数据库连接的 php 应用程序

发布于 2024-10-31 05:28:33 字数 338 浏览 1 评论 0原文

我有一个包含多个数据库的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月打碎记忆 2024-11-07 05:28:33

典型的单例看起来像:

class Singleton{
    private static $instance;
    private function __construct(){}
    private function __clone(){}
    public function Instance(){
        if(!self::$instance){
            self::$instance = new self;
        }
        return self::$instance;
    }
}

但是你可以通过拥有多个实例来扩展它,但彼此之间略有不同:

class MultiSingleton{
    private static $instance = array();
    private function __construct(){}
    private function __clone(){}
    public function Instance($index = 'default'){
        if(!isset(self::$instance[$index])){
            self::$instance[$index] = new MultiSingleton($index);
        }
        return self::$instance[$index];
    }
}

这样你就可以用一个类来统治它们;)并且仍然遵循单例模式

Typical singleton looks like:

class Singleton{
    private static $instance;
    private function __construct(){}
    private function __clone(){}
    public function Instance(){
        if(!self::$instance){
            self::$instance = new self;
        }
        return self::$instance;
    }
}

But you can extend it by having multiple instances but slightly different from each other:

class MultiSingleton{
    private static $instance = array();
    private function __construct(){}
    private function __clone(){}
    public function Instance($index = 'default'){
        if(!isset(self::$instance[$index])){
            self::$instance[$index] = new MultiSingleton($index);
        }
        return self::$instance[$index];
    }
}

That way you can have one class to rule them all ;) and still follow singleton pattern

不及他 2024-11-07 05:28:33

使用多个数据库相当容易。 mysql_connect() 返回一个资源值,该值标识连接。例如,您可以将该值添加到 mysql_query():

$db1 = mysql_connect("localhost");    
$db2 = mysql_connect("google.com");

$query1 = mysql_query("select ....", $db1);
$query2 = mysql_query("select ....", $db2);

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():

$db1 = mysql_connect("localhost");    
$db2 = mysql_connect("google.com");

$query1 = mysql_query("select ....", $db1);
$query2 = mysql_query("select ....", $db2);
野侃 2024-11-07 05:28:33

我不确定我是否理解这个问题,你的意思是“所以我正在研究用于获取连接的单例方法”,我知道单例是什么,但我真的不明白你打算做什么。更多,我不明白 mysql_max_connections 与所有这些有何关系,它与您的要求完全无关。

我可能建议您为所有数据库创建一个通用接口,例如一个名为“DB”的包装类(作为单例,如果您愿意的话),并定义一些方法来查询数据库,以便被​​调用者不必“知道”从哪个数据库获取数据。

但所有这些似乎毫无意义...

编辑:(另请参阅注释)

1)要显着减少连接数,请使用 mysql_pconnect()。但仔细阅读文档,我知道存在一些担忧。

2)通用代码。这需要大量的工作,但这是值得的。我这样做:

  • 我创建一个查询存储库,其中包含一堆查询,每个查询都用敏感标题命名,还有一些 SQL 代码,例如“SELECT foo, bar FROM foobar WHERE bar = '?'
  • 我创建一个带有名为 query (或类似名称)的函数的数据库类,该类接受查询名称、查询参数作为数组,并透明地构建实际查询字符串,在该查询类型的适当数据库上执行它
  • !函数返回一些可以迭代查询结果的对象的句柄,与 mysql_fetch_array 非常相似,但更好的是,

如果将来您要放弃 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:

  • I create a query repository, with a bunch of queries everyone named with a sensitive caption and some SQL code like "SELECT foo, bar FROM foobar WHERE bar = '?'
  • I create a DB class with a function named query (or something like that), that accepts the query name, the query parameters as an array, and transparently builds the acual query string, executing it on the appropriate database for that query type!
  • The function returns some handle to an object that can iterate through the query results, pretty like mysql_fetch_array, but better.

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 ..."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文