Laravel 7- AWS - Elastic Beanstalk + RDS Error when running "php artisan migrate"

Viewed 1558


I am trying to run a Laravel Application in Elastic Beanstalk on AWS.
I am using Laravel Version 7 and PHP 7.4 on AWS Elastic Beanstalk.
Every time when I try to run "php artisan migrate" I get this error:
My database RDS credentials are all correct.

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_sche ma.tables where table_schema = forge and table_name = migrations and table_type
= 'BASE TABLE')

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671 667| // If an exception occurs when attempting to run a query, we'll format the error 668| // message to include the bindings with SQL, which will make th is exception a 669| // lot more helpful to the developer instead of just the databa se's errors. 670| catch (Exception $e) { 671| throw new QueryException( 672| $query, $this->prepareBindings($bindings), $e 673| ); 674| } 675|

1 Answers

I solved my own question.

I ran in the /var/www/html directory via SSH

php artisan migrate -vv

to get the debug log and the stacktrace of the migration process.
I noticed that Laravel is not using the Environment variables (DB_HOST etc. ) when running the migration command.
So I added the .ebextensions directory and created a new file:
migration.config

container_commands:
     01migrations:
        command: "php artisan migrate"

Now the migration is working, because Laravel gets the access to the environment variables. So even the newest versions Laravel 7, MYSQL 8.0.17 and PHP 7.4 are working on Elastic Beanstalk RDS.

EDIT:
If you have specified in AWS as environment variable:

APP_ENV = "production"

use php artisan migrate --force instead of php artisan migrate

Regards NKol

Related