Laravel-无法设置默认连接

时间:2019-04-30 15:12:37

标签: laravel eloquent

[更新] php artisan cache:clear解决了它!

我有两个数据库,分别称为mysql1和mysql2 由于某些原因,如果我不指定连接,则会得到“未配置数据库[mysql]”。

在database.php中,我有这个

'default' => env('DB_CONNECTION', 'mysql1'),      *// here I replaced it by 'default' => 'mysql1' to be sure, same effect*

'connections' => [

        'mysql1' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'MYDB1'),
            'username' => env('DB_USERNAME', 'root'),
            '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'),
            ]) : [],
        ],

        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'MYDB2'),
            'username' => env('DB_USERNAME', 'root'),
            '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'),
            ]) : [],
        ],

在我的.env文件中

DB_CONNECTION=mysql1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=races
DB_USERNAME=root
DB_PASSWORD=null

DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=itra
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=null
DEBUGBAR_ENABLED=true

所以我总是需要指定

DB::connection('mysql1')->select

知道我忘记了什么吗?对于信息,mysql根本没有设置在任何地方...

0 个答案:

没有答案