douweng3564 2017-04-10 09:33
浏览 47
已采纳

在多个数据库中进行迁移

I try to set multiple databases for run a migration. In the file "config/database.php" this is the code:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'first_database'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_SEC_HOST', '127.0.0.1'),
        'port' => env('DB_SEC_PORT', '3306'),
        'database' => env('DB_SEC_DATABASE', 'second_database'),
        'username' => env('DB_SEC_USERNAME', 'root'),
        'password' => env('DB_SEC_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

In the file "env" this is the code:

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

DB_SEC_CONNECTION=mysql2
DB_SEC_HOST=127.0.0.1
DB_SEC_PORT=3306
DB_SEC_DATABASE=second_database
DB_SEC_USERNAME=root
DB_SEC_PASSWORD=

When I run the migration with this code:

 Schema::connection('second_database')->create('test', function(Blueprint $table) {
        $table->increments('id');
    });

The terminal shows this error message:

[InvalidArgumentException]
Database [second_database] not configured.

What am I doing wrong?

Thanks!

  • 写回答

1条回答 默认 最新

  • duanmu6231 2017-04-10 09:38
    关注

    use the array key in your config not the database name. That is

    Schema::connection('mysql2')->create('test', function(Blueprint $table) {
        $table->increments('id');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解