Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too Many Connections w/ Laravel 4 and MySQL

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

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?

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.

like image 241
Gimli Avatar asked Nov 10 '22 12:11

Gimli


1 Answers

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.

  • 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.
  • 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)
  • 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

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.

like image 163
Ray Avatar answered Nov 15 '22 08:11

Ray