undefinedfix
Sign in

Laravel (5.5) operating multiple databases

njntq edited in Sun, 26 Sep 2021

Question: when laravel 5.5 uses ORM mode to operate multiple dB on the same MySQL machine, how to set the link gracefully?

Laravel will link to the database configured in Env by default, but in different models, I need to specify another database instead of using the default database in evn;

After consulting the API manual, you can specify $connection in the model, but it is found that the value of this $connection needs to be in / config/ database.php Add an array configuration in.

/config/ database.php :

//默认,会读取env里的mysql配置
'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' => '',
            'strict' => true,
            'engine' => null,
        ],
        //自己新增的
        'introduction_processQualityReport' => [
            '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' => '',
            'strict' => true,
            'engine' => null,
        ],

After testing, it is found that the new data key must be the same as the database name I want to specify in the model, otherwise an error will be reported, such as database not XXX.

Question 1: the new array introduction_ Processqualityreport, I feel that there is no difference between a lot of values and the default one. It's a bit cumbersome to write in this way. I wonder if there is another more elegant way? (both databases are on the same MySQL)

2 Replies
openHBP
commented on Sun, 26 Sep 2021

Laravel can only operate multiple databases in the same project in this way. In fact, it is not so cumbersome.

ChristianOConnor
commented on Sun, 26 Sep 2021

I didn't have the problem that the key in the database configuration must be the database name, database.php

'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' => '',
        'strict' => true,
        'engine' => null,
    ],
    'mysql_main' => [
        'driver'    => 'mysql',
        'host'      => env('DB_MAIN_HOST', 'localhost'),
        'port'      => env('DB_MAIN_PORT', '3306'),
        'database'  => env('DB_MAIN_DATABASE', 'forge'),
        'username'  => env('DB_MAIN_USERNAME', 'forge'),
        'password'  => env('DB_MAIN_PASSWORD', ''),
        'charset'   => 'utf8mb4',
        'collation' => 'utf8mb4_bin',
        'prefix'    => '',
        'strict' => true,
    ],
    'mysql_uc' => [
        'driver'    => 'mysql',
        'host'      => env('DB_UC_HOST', 'localhost'),
        'port'      => env('DB_UC_PORT', '3306'),
        'database'  => env('DB_UC_DATABASE', 'forge'),
        'username'  => env('DB_UC_USERNAME', 'forge'),
        'password'  => env('DB_UC_PASSWORD', ''),
        'charset'   => 'utf8mb4',
        'collation' => 'utf8mb4_bin',
        'prefix'    => '',
        'strict' => true,
    ],

.env

helpers.php The definition is used to write a form to verify a direct specified connection.

//
define('a_DATABASE_NAME', env('DB_DATABASE'));
//
define('b_DATABASE_NAME', env('DB_MAIN_DATABASE'));
//
define('c_DATABASE_NAME', env('DB_UC_DATABASE'));
lock This question has been locked and the reply function has been disabled.