Search code examples
mysqlmongodbdatabase-connectionenvironmentlaravel-5.3

How to use multiple database connections in laravel project?


I want to use mysql and mongoDB in my laravel project, I know I can define multiple connections array in database.php file and call them like :

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

but my problem is how can I use mongoDB and mysql alongside each other in a project?

The real problem here is .env file, because it only uses one database configurations.

so let me clear this for you this is my database.php file in laravel v5.3 :

 'connections' => [

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'iranad'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'mysql'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '27017'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'options' => [
                'database' => 'admin'
            ]
        ],

    ],

And this is my .env file :

APP_ENV=local
APP_KEY=base64:NN3Me+qA1UOfdYW2SQyAXtxODazCAYBAKfFdRAqcakg=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=mysql

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=

As you can see my default database connection is mysql, and in .env file configuration is set to mysql, now how can I use mongoDB in my application ?

FYI : I want mysql to be default connection and I use mongodb in some cases.


Solution

  • I've found the solution thanks to @astroanu

    you can change the the default env variable names, it will not break the functionality of you appication

    you can change your database.php to something like this:

    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('MYSQL_DB_HOST', 'localhost'),
            'port' => env('MYSQL_DB_PORT', '3306'),
            'database' => env('MYSQL_DB_DATABASE', 'iranad'),
            'username' => env('MYSQL_DB_USERNAME', 'root'),
            'password' => env('MYSQL_DB_PASSWORD', 'mysql'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
    
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGO_DB_HOST', 'localhost'),
            'port' => env('MONGO_DB_PORT', '27017'),
            'database' => env('MONGO_DB_DATABASE'),
            'username' => env('MONGO_DB_USERNAME'),
            'password' => env('MONGO_DB_PASSWORD'),
            'options' => [
                'database' => 'admin'
            ]
        ],
    
    ],
    

    then on the .env define each variable name

    #sql 
    MYSQL_DB_HOST=...
    MYSQL_DB_PORT=...
    MYSQL_DB_DATABASE=...
    MYSQL_DB_USERNAME=...
    MYSQL_DB_PASSWORD=...
    
    # mongo
    MONGO_DB_HOST=...
    MONGO_DB_PORT=...
    MONGO_DB_DATABASE=...
    MONGO_DB_USERNAME=...
    MONGO_DB_PASSWORD=...
    

    on your models define protected connection attribute: this should be either of your connection names you defined on the database.php

      protected $connection = 'mongodb';
    

    the only problem for you here would be implementing relations between databases, which is impossible, you will need to write your own queries for that.

    FYI : For testing your connections in tinker :

    DB::connection('mongodb')->collection('migrations')->get();
    

    Notice mongodb in here is the connection name in database.php file, and migrations is the collection name.