0

I am using Drush 10.6.2 on a Drupal core version 9.5.11 on AWS EC2 instance, and I'm having trouble getting Drush to bootstrap Drupal if the settings.php database credentials are populated by AWS environment variables. When the database name, password, etc are hardcoded in the settings.php file, Drush works fine. Yet when they are populated via AWS environment variables, Drush does not bootstrap and can't find the database, returning this error:

SQLSTATE[HY000] [2002] No such file or directory

Current settings.php database configuration:

//Drush does not work unless database/UN/PW is hardcoded. 
$databases['default']['default'] = [
  'database' => $_SERVER['DB_NAME'],
  'username' => $_SERVER['DB_USER'],
  'password' => $_SERVER['DB_PASS'],
  'host' => $_SERVER['DB_HOST'],
  'port' => 3306,
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
  'init_commands' => [
    'isolation_level' => 'SET SESSION transaction_isolation=\'READ-COMMITTED\'',
    'sql_mode' => "SET sql_mode = 'ANSI,TRADITIONAL'"
  ]
];

I imagine I need to pipe these variables into Drush somehow, but after looking through different documentation I do not see a clear way how to do this. I have created a drush.yml in my local user home directory, but I'm not clear on what to enter and how to have Drush translate values to these variables. Any help would be appreciated!

7
  • $_SERVER does get populated with environment vars, but that's a bit of a side-effect and what's available depends on the web server/execution context - have you tried using $_ENV (or getenv() if you're concerned about code standards) directly?
    – Clive
    Feb 10 at 19:47
  • I have not, I did dump $_ENV and didn't see what I was looking for - but getenv() might be the right way to play. If it ends up working, I'll post back here affirmatively.
    – TripodC
    Feb 10 at 21:40
  • If you don't see the vars in $_ENV then they're probably just not there. You might have another step to take to set them up
    – Clive
    Feb 10 at 22:04
  • I've tried different servers and all display the environment variables when running php -r 'print_r($_SERVER);' from the command line. $_ENV is cleared in PHP default settings. But you can change any of this in php.ini. This is not Drush related.
    – 4uk4
    Feb 11 at 10:50
  • I can see the vars in getenv() and $_SERVER just fine. The problem is that Drush isn't reading them. The settings.php file can read them, but when Drush attempts to read those values, it comes up blank. I need to figure out a way to tell Drush what these variables are.
    – TripodC
    Feb 12 at 8:05

1 Answer 1

-2

Setting environment variables in Drush configuration files like drush.yml can be useful for managing different configurations across various environments.

Here's a general approach

Locate or Create drush.yml:

Find the drush.yml file in your Drush configuration directory. If it’s not there, create one. You might place it in the site-specific directory (sites/default/drush.yml) or globally (~/.drush/drush.yml).

Edit drush.yml:

Open the file in a text editor. Use a simple YAML syntax to define your environment variables. Read More

New contributor
Vikaas Arora is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.