drushddev

SQLSTATE[HY000] [2002] Drush commands not working - DDEV vanilla Drupal 8 install


Just created a new Drupal 8 install using ddev, however, I'm having issues with drush. Whenever I run the command drush cr it returns the error:

 [error]  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

If I run the command drush en admin_toolbar with the --debug flag it returns the error:

Executing: mysql --defaults-file=/private/tmp/drush_ty1hL4 --database=db --host=db --port=3306 --silent < /private/tmp/drush_OSFtCb ERROR 2005 (HY000): Unknown MySQL server host 'db' (0)

[Symfony\Component\Console\Exception\CommandNotFoundException] Command pm:enable was not found. Drush was unable to query the database.

The only solutions I was able to find in regards to this issue was changing host in settings.php from localhost to 127.0.0.1 but since the settings.php file was generated by ddev during configuration, host is actually db and changing it to anything causes the site to break.


Solution

  • There are two ways to run drush in recent versions of ddev.

    The way most likely to work is to run drush commands inside the container.

    ddev ssh and drush cr or ddev exec drush cr

    You can also run drush on the host with ddev. If you're in the project directory and have drush 8 installed on the host, commands like drush sql-cli and drush uli "just work". Your mileage may vary.

    There are a few things to note about drush usage in general though, especially with Drupal 8 and drush 9+: