Search code examples
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+:

    • drush 9+ can never be installed globally, it's always installed per-project using composer.
    • As recommended by the drush project, the global /usr/local/bin/drush inside the container is actually "drush launcher". It first attempts to use the site-local composer-installed drush. If that doesn't exist or can't be found, drush launcher will use /usr/local/bin/drush8, the global installation of drush 8 inside the container.
    • I definitely know very experienced and famous Drupal 8 developers who never rely on drush launcher, they'll run drush commands like vendor/bin/drush sql-cli inside the container (or ddev exec /var/www/html/vendor/bin/drush sql-cli on the host) to get the exact site-local drush they want.