0

I want to create a second database to act as a backup. I've been following another SO question solution and other websites which leads to the same thing. I've added a new DB connection in .env and configured it in database.php, cleared out the cache using php artisan config:cache. When I try to migrate using php artisan migrate --database=backup_literature_review_management, it says InvalidArgumentException, Database connection [backup_literature_review_management] not configured.

.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'),
            ]) : [],
        ],

    ],

Cleared cache before migrating

php artisan config:cache

Trying to migrate to 2nd DB

php artisan migrate --database="backup_literature_review_management"

Error

InvalidArgumentException, Database connection [backup_literature_review_management] not configured.

Afiq Rosli
  • 357
  • 1
  • 7
  • 22

1 Answers1

3

You are calling the wrong connection. Use this:

php artisan migrate --database="mysql2"

You can also define the connection inside the migration file like:

Schema::connection('mysql2')->create('table', function (Blueprint $table) {...

With this last command, you can ommit --database="mysql2 in the migration command. I personally prefer to declare the connection inside the migration file.

Hope it helped!

hhelderneves
  • 925
  • 8
  • 24