Drush Not Working With MAMP? Here's How To Fix It!
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 (<br> 'database' => 'drupal',<br> 'username' => 'root',<br> 'password' => 'root',<br> 'prefix' => '',<br> 'host' => 'localhost',<br> 'port' => '3306',<br> 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',<br> 'driver' => 'mysql',<br>);
Change it to this (differences highlighted for clarity):
$databases['default']['default'] = array (<br> 'database' => 'drupal',<br> 'username' => 'root',<br> 'password' => 'root',<br> 'prefix' => '',<br> <span style="color:white;"><strong>'host' => '127.0.0.1',</strong></span><br> 'port' => '3306',<br> 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',<br> 'driver' => 'mysql',<br> <span style="color:white;"><strong>'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',</strong></span><br>);
With those updates in the database settings, Drush should work as expected!
Hope that helps!
Tags: DrushMAMPplanet-drupal