How to connect multiple database in Laravel ^8.0.0

I need to integrate MySQL, MongoDB, PostgreSQL and a few other ones. how to connect multiple database in laravel
I need a way to limit/restrict the number of accesses.I want to create and view all the pages with different roles/ID’s.I need to have a query builder for expressing data-sets.The database orchestration tools also expose a nice-to-have capability called the database tier which can take care of some of these.
The database tier ensures that for every table there’s an associated database entry and a standalone database instance that’s configured for each specific role. So, in our example above we have a Post.

Solution For: How to connect multiple database in laravel?

How to connect multiple database in Laravel, How to connect multiple database in Laravel?

sing .env >= 5.0  (Works on 8)

In .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databaseone
DB_USERNAME=root
DB_PASSWORD=personal

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=databasetwo
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=personal

In config/database.php

'mysql' => [
    'driver'    => env('DB_CONNECTION'),
    'host'      => env('DB_HOST'),
    'port'      => env('DB_PORT'),
    'database'  => env('DB_DATABASE'),
    'username'  => env('DB_USERNAME'),
    'password'  => env('DB_PASSWORD'),
],

'mysql2' => [
    'driver'    => env('DB_CONNECTION_SECOND'),
    'host'      => env('DB_HOST_SECOND'),
    'port'      => env('DB_PORT_SECOND'),
    'database'  => env('DB_DATABASE_SECOND'),
    'username'  => env('DB_USERNAME_SECOND'),
    'password'  => env('DB_PASSWORD_SECOND'),
],

Solution Skip For Now: How to connect multiple database in laravel?

Without .env <5.0

app/config/database.php

return array(

    'default' => 'mysql',

    'connections' => array(

        # Primary/Default database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'databaseone',
            'username'  => 'root',
            'password'  => 'personal'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Secondary database connection
        'mysqltwo' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'databasetwo',
            'username'  => 'root',
            'password'  => 'personal'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

: Schema :

Schema::connection('mysqltwo')->create('any_table', function($table)
{
    $table->increments('id'):
});

: Query Builder :

$users = DB::connection('mysqltwo')->select(...);

: Eloquent :

Set the $connection variable in your model

class AnyModel extends Eloquent {

    protected $connection = 'mysqltwo';

}

Useful Links

  1. Laravel 5 multiple database connection FROM laracasts.com
  2. Connect multiple databases in laravel FROM tutsnare.com
  3. Multiple DB Connections in Laravel FROM fideloper.com
  4. [Solved] – Mysql Out Of Sort Memory, Consider Increasing Server Sort Buffer Size
  5. How to connect multiple database in Laravel?

Leave a Comment