'"Database hosts array is empty." after deploying Laravel project on AWS Elastic Beanstalk

I deployed my project to an AWS EC2 instance using Elastic Beanstalk. I used this tutorial https://www.youtube.com/watch?v=ISVaMijczKc as a reference while deploying. I am following everything as it is in the tutorial but I ended up with an error.

Database hosts array is empty. (SQL: select * from resource_categories)

The following are my codes.

database.php

<?php

define('RDS_HOSTNAME', $_SERVER['RED_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RED_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RED_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RED_DB_NAME']);

return [

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

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

        'mysql' => [
            'driver' => 'mysql',
            'host' => RDS_HOSTNAME,
            'port' => env('DB_PORT', '3306'),
            'database' => RDS_DB_NAME,
            'username' => RDS_USERNAME,
            'password' => RDS_PASSWORD,
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        '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' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],
    ],

    'migrations' => 'migrations',

    'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'predis'),
        ],

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

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

.ENV

APP_NAME="MyProject"
APP_ENV=development
APP_KEY=base64:FlVBd61BUEzVx6ACa6OOn3Jrp4z+VRpug+F1K1ZeJOs=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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


Solution 1:[1]

I've gone through a similar problem and MarkB helped me. There's a specific procedure you must follow when you SSH into the instance.

If you run

export

you can see that there's no variable named RDS_HOSTNAME in that Linux shell and that's why you're getting that error.

If you run

/opt/elasticbeanstalk/bin/get-config environment

you can see an object with the list of properties, including that RDS_HOSTNAME.

If you run

/opt/elasticbeanstalk/bin/get-config environment -k RDS_USERNAME

you get the value associated with that particular property. This value needs to be saved in a variable and exported so that other commands can recognize it.

If you run

export RDS_USERNAME="value"

then when you run

export

you can see that this is now available.

Now if you run the command you wanted it's likely gonna work (you might need to repeat this for RDS_USERNAME, RDS_PASSWORD and RDS_DB_NAME).

Note: if that didn't work, then your problem might be similar to this one.

Solution 2:[2]

This is a bug report which might be related to this problem. The long and the short is that if the ConnectionFactory gets passed a NULL value it will (erroneously) trigger that error. I suspect that all of your RDS variables are empty.

To set these values, you can follow instructions here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2