This is how we handle multiple DB Connections with Laravel, which is a PHP Framework not Python (For whom thinks that this is a duplicate post)
<?php
return array(
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
And this is how you connect to this database.
$user1 = User::on('mysql1')->where(/* ... */)->get()
$user2 = User::on('mysql2')->where(/* ... */)->get()
These are only SELECT queries. Therefore Eloquent works flawlessly.
However, when I want to execute a JOIN query operation between these 2 databases, this seems not possible.
If these 2 databases are on different servers, you can't do this. At least not simply. I read about a storage engine. But I have never tried to do this before.
BUT
If there databases are on the same server, you can do it with a simple join, IF the user has the right permission!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With