Laravel 6 - 在 database.php 中配置了第二个数据库,但仍收到未配置的数据库连接 []
Laravel 6 - 2nd Database configured in database.php but still received Database connection [] not configured
我想创建第二个数据库作为备份。我一直在关注 解决方案和其他导致相同问题的网站。我在 .env 中添加了一个新的数据库连接并在 database.php 中配置了它,使用 php artisan config:cache
清除了缓存。当我尝试使用 php artisan migrate --database=backup_literature_review_management
进行迁移时,它显示 InvalidArgumentException,未配置数据库连接 [backup_literature_review_management]。
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=literature_review_management
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_BACKUP=mysql
DB_HOST_BACKUP=127.0.0.1
DB_PORT_BACKUP=3306
DB_DATABASE_BACKUP=backup_literature_review_management
DB_USERNAME=root
DB_PASSWORD=
database.php
'connections' => [
'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' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_BACKUP', '127.0.0.1'),
'port' => env('DB_PORT_BACKUP', '3306'),
'database' => env('DB_DATABASE_BACKUP', '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'),
]) : [],
],
],
迁移前清除缓存
php artisan config:cache
正在尝试迁移到第二个数据库
php artisan migrate --database="backup_literature_review_management"
错误
InvalidArgumentException,数据库连接 [backup_literature_review_management] 未配置。
您打错电话了。使用这个:
php artisan migrate --database="mysql2"
您还可以在迁移文件中定义连接,例如:
Schema::connection('mysql2')->create('table', function (Blueprint $table) {...
使用最后一条命令,您可以在迁移命令中省略 --database="mysql2
。我个人更喜欢在迁移文件中声明连接。
希望对您有所帮助!
我想创建第二个数据库作为备份。我一直在关注 php artisan config:cache
清除了缓存。当我尝试使用 php artisan migrate --database=backup_literature_review_management
进行迁移时,它显示 InvalidArgumentException,未配置数据库连接 [backup_literature_review_management]。
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=literature_review_management
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_BACKUP=mysql
DB_HOST_BACKUP=127.0.0.1
DB_PORT_BACKUP=3306
DB_DATABASE_BACKUP=backup_literature_review_management
DB_USERNAME=root
DB_PASSWORD=
database.php
'connections' => [
'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' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_BACKUP', '127.0.0.1'),
'port' => env('DB_PORT_BACKUP', '3306'),
'database' => env('DB_DATABASE_BACKUP', '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'),
]) : [],
],
],
迁移前清除缓存
php artisan config:cache
正在尝试迁移到第二个数据库
php artisan migrate --database="backup_literature_review_management"
错误
InvalidArgumentException,数据库连接 [backup_literature_review_management] 未配置。
您打错电话了。使用这个:
php artisan migrate --database="mysql2"
您还可以在迁移文件中定义连接,例如:
Schema::connection('mysql2')->create('table', function (Blueprint $table) {...
使用最后一条命令,您可以在迁移命令中省略 --database="mysql2
。我个人更喜欢在迁移文件中声明连接。
希望对您有所帮助!