Search code examples
laravelamazon-web-servicesenvironment-variablesamazon-elastic-beanstalklaravel-artisan

Database hosts array is empty


I am using elastic Beanstalk deployment for my Laravel project.I have set up my env properties in AWS beanstalk configuration. enter image description here

When i ssh to beanstalk ec2 and type any php artisan commands like php artsan migrate, i got the error "Database hosts array is empty. (SQL: select * from information_schema.tables where table_schema = ? and table_name = migrations and table_type = 'BASE TABLE')". I have a valid RDS database and working laravel project.Why can't i type any of the artisan commands.The artisan commands are working when i am manually copying to the beanstalk ec2.Why it is not listening to aws env properties ?


Solution

  • If the artisan commands work when you manually copy the project to the Elastic Beanstalk EC2 instance but not when you run them through SSH, it suggests that the environment variables are not being loaded properly when using SSH.

    When you manually copy the project, the environment variables might be sourced from the .env file present in your project directory. However, when running commands through SSH, the environment variables need to be set explicitly.

    A. SSH into the EC2 instance using the command:

    ssh -i your-key.pem ec2-user@your-instance-ip
    

    B. Change the current working directory to the root directory of your Laravel project. For example:

    cd /var/www/html/your-laravel-project
    

    C. Run the artisan commands prefixed with the export command to set the environment variables explicitly. For example:

    export DB_HOST=your-db-host
    export DB_USERNAME=your-db-username
    export DB_PASSWORD=your-db-password
    php artisan migrate
    

    By explicitly exporting the environment variables before running the artisan commands, you ensure that the variables are set and available for the Laravel application to use.

    If you have many environment variables to set, it might be cumbersome to manually export each one. In that case, you could consider creating a script that exports all the required environment variables and then runs the artisan command.