0

I am developing a laravel application. I connect my laravel application with a mysql database. But I need a table data that is in another server and that database is sql database is in IIS server.

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





   'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        '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' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],


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

I need help to use two databases from different server (one from mysql and another from sql) on my same laravel application.

2
  • this question already answered in page: stackoverflow.com/questions/31847054/… Commented Aug 11, 2022 at 7:07
  • @FiroozMeysami yes I found it but it was about two mysql database. But I want to use one mysql and one sql database. Commented Aug 11, 2022 at 7:12

1 Answer 1

1

In your config/database.php

 'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        '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' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

// Add this (Connect sql server)
//TCP port 1433 is typically the port used by a default instance of SQL Server
'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env(''),
            'host' => env('DB_HOST', 'X.X.X.X'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'database_name'),
            'username' => env('DB_USERNAME', 'user'),
            'password' => env('DB_PASSWORD', 'password'),
            'charset' => 'utf8mb4',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

Get your mysql table

$mysql = DB::connection('mysql')->table('xxx')->select('*')->get();

Get your sql server table

$sqlsrv= DB::connection('sqlsrv')->table('xxx')->select('*')->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.