Tanvir
Tanvir

Reputation: 151

Laravel 6 multiple database authentication problem

I have two DB connection as bellow:

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

DB_CONNECTION=sqlsrv
DB_HOST=192.168.102.11
DB_PORT=1433
DB_DATABASE=Some_Name
DB_USERNAME=XXXXXX
DB_PASSWORD=XXXXXX

I am authenticating with mysql also written code as bellow:

class User extends Authenticatable
{
    use Notifiable;
    protected $connection = 'mysql';

All DB connections are ok but still it takes too long time to login. It also does not show any result. Could you please help?

Upvotes: 0

Views: 565

Answers (2)

TsaiKoga
TsaiKoga

Reputation: 13394

Because your env db configuration key_name is the same,

It seems laravel choose the second connection, so it cannot find the connection mysql. The first connection is covered by second.

Change another connection key name for .env

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

DB_SRV_CONNECTION=sqlsrv
DB_SRV_HOST=Host_name
DB_SRV_PORT=1433
DB_SRV_DATABASE=DB_name
DB_SRV_USERNAME=user
DB_SRV_PASSWORD=password

In your config/database.php

        'default' => env('DB_CONNECTION', 'mysql'),

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            ...
        ],
        'sql_srv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_SRV_HOST', '127.0.0.1'),
            'port' => env('DB_SRV_PORT', '3306'),

You can check the connection configuration in your tinker:

config('database.connections.mysql')

If it still not work, you can clear the config cache:

php artisan config:clear
php artisan optimize

Upvotes: 2

apokryfos
apokryfos

Reputation: 40683

Change your .env file to:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sales_report
DB_USERNAME=root
DB_PASSWORD=

DB2_HOST=Host_name
DB2_PORT=1433
DB2_DATABASE=DB_name
DB2_USERNAME=user
DB2_PASSWORD=password

Then in your config/database.php change the SQL server configuration to use the new names:

...
 'connections' => [
        // ... 
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],       
        // ...
        'sqlsrv' => [
            'driver' => 'sqlsrv',

            'host' => env('DB2_HOST', 'localhost'),
            'port' => env('DB2_PORT', '1433'),
            'database' => env('DB2_DATABASE', 'forge'),
            'username' => env('DB2_USERNAME', 'forge'),
            'password' => env('DB2_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],
...

Doing this will set the default database to mysql but you can use the alternative connection in each model or on the query builder:

class User extends Authenticatable
{
    use Notifiable;
    protected $connection = 'sqlsrv';

or

DB::connection('sqlsrv')->table('users')->...

You can change the default by changing the DB_CONNECTION entry in your .env file but then you need to override the connection to mysql where needed.

Upvotes: 1

Related Questions