1

I'm trying to make multiple DB connections and saw: http://fideloper.com/laravel-multiple-database-connections

i also try this

I set my DB config in config/database.php as:

'default' => 'web',

'connections' => array(

    # Our primary database connection
    'web' => array(
        'driver'    => 'mysql',
        'host' => env('DB_HOST', 'host1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'database1'),
        'username' => env('DB_USERNAME', 'user1'),
        'password' => env('DB_PASSWORD', 'pass1'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'another' => array(
        'driver'    => 'mysql',
        'host' => env('DB_HOST', 'host2'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'database2'),
        'username' => env('DB_USERNAME', 'user2'),
        'password' => env('DB_PASSWORD', 'pass2'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

I built my schema as well as follow:

public function up()
    {
        Schema::connection('another')->create('some_table', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->string('test')->unique();
            $table->string('one')->index();
            $table->timestamp('created_at');
        });
    }

But then when I want to run artisan migrate it sais:

[InvalidArgumentException]

Database [mysql] not configured.

I understand that mysql is read from .env file (because if I change it to something, the name changes).

I tried to change .env file as follow:

DB_CONNECTION=web
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=username
DB_PASSWORD=secret

Then I run artisan migrate again, it didn't throw me the error anymore, but it seems like the table is not created as I want. The table from my schema above was created in 'web' connection instead.

If you see my schema:

Schema::connection('another')->create('some_table', function (Blueprint $table)

That table should be created in 'another' connection. Not in web connection.

I also run:

artisan migrate:install --database=web
artisan migrate:install --database=another

as well as:

artisan migrate --database="web" --path="database/migrations/web"
artisan migrate --database="another" --path="database/migrations/another"

all of them only creating tables in web connection

basically it only read configuration in .env files. it never listen to my: config/database.php

What did I miss here?

Community
  • 1
  • 1
AnD
  • 3,060
  • 8
  • 35
  • 63

0 Answers0