0

I started new Laravel project development.
I set .env file and database.php file, migration and seeder file.
I got success in migration but failed in seeding. I got the following error when I ran php artisan db:seed.

    SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: insert into `caeru_local_companies` (`code`, `name`, `furigana`, `date_separate_type`, `u   
  se_address_system`, `updated_at`, `created_at`) values ............

I checked that caeru_local_companies table exists in my database.
And, my .env file is

APP_ENV=local
APP_KEY=base64:ONZDpAFdncYcyqYontvOB4kjwoioEzIHjF5QKDAaO4E=
APP_URL=http://localhost

APP_DEBUG=true
APP_LOG=single
APP_LOG_LEVEL=debug
LOG_MAX_FILES=5

MAIN_DB_CONNECTION=main
MAIN_DB_HOST=127.0.0.1
MAIN_DB_PORT=3306
MAIN_DB_DATABASE=caeru_main
MAIN_DB_PREFIX=caeru_local_
MAIN_DB_USERNAME=root
MAIN_DB_PASSWORD=

SUB_DB_HOST=127.0.0.1
SUB_DB_PORT=3306
SUB_DB_USERNAME=root
SUB_DB_PASSWORD=
SUB_DB_PREFIX=caeru_local_

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

DEBUGBAR_ENABLED=false
REDIRECT_HTTPS=false

And, my database.php config file is

<?php

return [

/*
|----------------------------------------------------------------------
| Default Database Connection Name
|----------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => env('MAIN_DB_CONNECTION', 'main'),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    // 'sqlite' => [
    //     'driver' => 'sqlite',
    //     'database' => env('DB_DATABASE', database_path('database.sqlite')),
    //     'prefix' => '',
    // ],

    'main' => [
        'driver' => 'mysql',
        'host' => env('MAIN_DB_HOST', '127.0.0.1'),
        'port' => env('MAIN_DB_PORT', '3306'),
        'database' => env('MAIN_DB_DATABASE', 'caeru_main'),
        'username' => env('MAIN_DB_USERNAME', 'root'),
        'password' => env('MAIN_DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => env('MAIN_DB_PREFIX', 'caeru_local_'),
        'strict' => true,
        'engine' => null,
    ],

    'sub' => [
        'driver' => 'mysql',
        'host' => env('SUB_DB_HOST', '127.0.0.1'),
        'port' => env('SUB_DB_PORT', '3306'),
        'database' => '',
        'username' => env('SUB_DB_USERNAME', 'root'),
        'password' => env('SUB_DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => env('SUB_DB_PREFIX', 'caeru_'),
        'strict' => true,
         'modes' => [
         //'ONLY_FULL_GROUP_BY', // Disable this to allow grouping by one column
            'STRICT_TRANS_TABLES',
            'NO_ZERO_IN_DATE',
            'NO_ZERO_DATE',
            'ERROR_FOR_DIVISION_BY_ZERO',
            'NO_AUTO_CREATE_USER',
            'NO_ENGINE_SUBSTITUTION'
        ],
        'engine' => null,

        // This is a customized config item, should set it exactly the same as 'prefix'
        'default_prefix' => env('SUB_DB_PREFIX', 'caeru_'),
    ],

    // 'pgsql' => [
    //     'driver' => 'pgsql',
    //     'host' => env('DB_HOST', '127.0.0.1'),
    //     'port' => env('DB_PORT', '5432'),
    //     'database' => env('DB_DATABASE', 'forge'),
    //     'username' => env('DB_USERNAME', 'forge'),
    //     'password' => env('DB_PASSWORD', ''),
    //     'charset' => 'utf8',
    //     'prefix' => '',
    //     'schema' => 'public',
    //     'sslmode' => 'prefer',
    // ],

],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

],

];

I tried many times which other developers offered, but there remains error.

How can I fix this error?

YannPl
  • 1,312
  • 1
  • 14
  • 30
blue pine
  • 472
  • 6
  • 17

1 Answers1

0

I think you use MAIN_DB_PREFIX in a wrong way. It didn't add prefix to the database name, instead it adds an prefix to database's tables. So you have to add complete database name in the MAIN_DB_DATABASE section.

Mohammad Akbari
  • 446
  • 2
  • 9