Laravel 4和MySQL的连接太多

时间:2022-10-15 19:51:01

I have a question about handling multiple DB connections in Laravel 4.1. Say I have one DB host with 3 DBs on that host

我有一个关于在Laravel 4.1中处理多个数据库连接的问题。假设我在该主机上有一个带有3个DB的数据库主机

eg:

例如:

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'DB_1',
        'username'  => $_ENV['MYSQL_USER'],
        'password'  => $_ENV['MYSQL_PASS'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'mysql2' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'DB_2',
        'username'  => $_ENV['MYSQL_USER'],
        'password'  => $_ENV['MYSQL_PASS'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'mysql3' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'DB_3',
        'username'  => $_ENV['MYSQL_USER'],
        'password'  => $_ENV['MYSQL_PASS'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

Should I be making 3 different connections to those DBs?

我应该与这些数据库建立3种不同的连接吗?

Or should I simply have one connection and in each model specify the table name to something like:

或者我应该只有一个连接,并在每个模型中指定表名称,如:

public $table = "DB_2.table_name";

The reason I ask, is that I have noticed that I it is easier to exhaust the database connections as it creates a new DB connection when ever it needs to connect to a different database.

我问的原因是,我注意到我更容易耗尽数据库连接,因为它在需要连接到不同数据库时创建新的数据库连接。

I know both will work, but I'm interested in what is considered to be "best practice" in this sort of situation.

我知道两者都有效,但我对在这种情况下被认为是“最佳实践”感兴趣。

Thanks in advance for the feedback.

提前感谢您的反馈。

Cheers.

干杯。

2 个解决方案

#1


1  

Specify to the table, not the DB implementation.

指定表,而不是数据库实现。

I'm not a big fan of the 'feature' that allows cross DB querying on MySQL between DB's on the same server. This is what would allow the thing you mention.

我不是“功能”的忠实粉丝,它允许在同一服务器上的DB之间跨MySQL查询MySQL。这是允许你提到的东西。

  • If you change the name of your db you'll need to refactor your table names in your code.
  • 如果更改数据库的名称,则需要在代码中重构表名。
  • If you create a test db like table_name_test to test your code or a db modification, you'll have to modify all your tables tames in your code each time you jump between db's vs. just editing the config file.
  • 如果您创建一个像table_name_test这样的测试数据库来测试您的代码或数据库修改,那么每次在db和只编辑配置文件之间跳转时,您都必须修改代码中的所有表名。
  • What if you move to a differnt DB technology without DB names (one Db per server/fuile, you'll need to refactor your table name properties in each class)
  • 如果您在没有DB名称的情况下转向不同的DB技术(每个服务器/ fuile一个Db,您需要在每个类中重构您的表名属性),该怎么办?
  • It leads down the dark, tempting yet vile path of cross db queries like select * from db1.foo f Join db2.bar b on b.id = f.id;. Congratulations you just turned your 2 dbs into actually one big db that might not run outside MySQL
  • 它引导了黑暗,诱人而又卑鄙的交叉数据库查询路径,例如select * from db1.foo f在b.id = f.id;上加入db2.bar b。恭喜你刚刚将你的2 dbs变成了一个可能无法在MySQL之外运行的大型数据库

Also, even though you're specifying three connections, you're probably not making a connection and using all three at once... I hope. If you do use all 3 connection per request to you laravel app/site saving 2 connections is probably, micro optimization.

此外,即使您指定了三个连接,您可能也没有建立连接并同时使用所有三个连接...我希望。如果你确实使用了每个请求的所有3个连接laravel app / site节省2个连接可能是微优化。

#2


0  

This code made the trick for me when I needed.

当我需要时,这段代码为我提供了诀窍。

Config::set('database.default', 'database_name');

In your case, replace the database_name with the ones you may need. I call it in the controller each time I see a need to make the switch. Before a query, for exemple.

在您的情况下,将database_name替换为您可能需要的database_name。每次看到需要进行切换时,我都会在控制器中调用它。在查询之前,例如。

I don't know if is the best way ever to do it, but it worked for me and I thought it worth sharing! =D

我不知道这是否是最好的方式,但它对我有用,我认为值得分享! = d

#1


1  

Specify to the table, not the DB implementation.

指定表,而不是数据库实现。

I'm not a big fan of the 'feature' that allows cross DB querying on MySQL between DB's on the same server. This is what would allow the thing you mention.

我不是“功能”的忠实粉丝,它允许在同一服务器上的DB之间跨MySQL查询MySQL。这是允许你提到的东西。

  • If you change the name of your db you'll need to refactor your table names in your code.
  • 如果更改数据库的名称,则需要在代码中重构表名。
  • If you create a test db like table_name_test to test your code or a db modification, you'll have to modify all your tables tames in your code each time you jump between db's vs. just editing the config file.
  • 如果您创建一个像table_name_test这样的测试数据库来测试您的代码或数据库修改,那么每次在db和只编辑配置文件之间跳转时,您都必须修改代码中的所有表名。
  • What if you move to a differnt DB technology without DB names (one Db per server/fuile, you'll need to refactor your table name properties in each class)
  • 如果您在没有DB名称的情况下转向不同的DB技术(每个服务器/ fuile一个Db,您需要在每个类中重构您的表名属性),该怎么办?
  • It leads down the dark, tempting yet vile path of cross db queries like select * from db1.foo f Join db2.bar b on b.id = f.id;. Congratulations you just turned your 2 dbs into actually one big db that might not run outside MySQL
  • 它引导了黑暗,诱人而又卑鄙的交叉数据库查询路径,例如select * from db1.foo f在b.id = f.id;上加入db2.bar b。恭喜你刚刚将你的2 dbs变成了一个可能无法在MySQL之外运行的大型数据库

Also, even though you're specifying three connections, you're probably not making a connection and using all three at once... I hope. If you do use all 3 connection per request to you laravel app/site saving 2 connections is probably, micro optimization.

此外,即使您指定了三个连接,您可能也没有建立连接并同时使用所有三个连接...我希望。如果你确实使用了每个请求的所有3个连接laravel app / site节省2个连接可能是微优化。

#2


0  

This code made the trick for me when I needed.

当我需要时,这段代码为我提供了诀窍。

Config::set('database.default', 'database_name');

In your case, replace the database_name with the ones you may need. I call it in the controller each time I see a need to make the switch. Before a query, for exemple.

在您的情况下,将database_name替换为您可能需要的database_name。每次看到需要进行切换时,我都会在控制器中调用它。在查询之前,例如。

I don't know if is the best way ever to do it, but it worked for me and I thought it worth sharing! =D

我不知道这是否是最好的方式,但它对我有用,我认为值得分享! = d