Drush Not Working With MAMP? Here's How To Fix It! | Modules Unraveled

Here's the issue

If you're using MAMP, you might have experienced an issue where a website loads just fine in the browser, but when you try to use a Drush command, you get an error like the following:

exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:146

To be completely honest, I'm not sure what causes this issue. I think it has to do with the way Drush accesses MySQL. As far as I can tell, Drush is trying to access the system MySQL, instead of the one that comes with MAMP.

Here's the fix!

Luckily, this can be easily fixed by changing:

'host' => 'localhost',

to

'host' => '127.0.0.1',

and/or by adding the following line to the database credentials in your settings.php (or settings.local.php) file.

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

Example

So, for example, if your database credentials look like this in settings.php:

$databases['default']['default'] = array (
  'database' => 'drupal',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

Change it to this (differences highlighted for clarity):

$databases['default']['default'] = array (
  'database' => 'drupal',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
  'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
);

With those updates in the database settings, Drush should work as expected!

Hope that helps!

Comments

Thanks so much for this. I was JUST tearing my hair out over this. the mysql socket was the kicker. bravo!

Awesome! I'm so glad I could help!