23

I installed Drush for the first time (so it may be a configuration error) and I'm trying to update to the latest core version of Drupal.

Drush is giving me this error - I'm on an OSX box.

dev5:clientnamedirectory my.name$ drush pm-update projects drupal-7.25
<h1>Additional uncaught exception thrown while handling exception.</h1>

<h2>Original</h2><p>PDOException: SQLSTATE[HY000] [2002] No such file or directory in drupal_get_installed_schema_version() (line 155 of /Users/my.name/Sites/proface/includes/install.inc).</p>

<h2>Additional</h2>

<p>PDOException: SQLSTATE[HY000] [2002] No such file or directory in system_list() (line 165 of /Users/my.name/Sites/clientnamedirectory/includes/module.inc).</p><hr />

Drush command terminated abnormally due to an unrecoverable error.
12
  • 1
    Have you completed these steps? I know it may not be exactly your issue, but it may help.
    – Chapabu
    Jan 3, 2012 at 15:05
  • Which version of drush are you using? There is issue that seem to be identical with yours: drupal.org/node/832472
    – F1234k
    Jan 3, 2012 at 15:06
  • I'm using version drush-7.x-4.5 and drupal 7.9
    – FLY
    Jan 3, 2012 at 15:15
  • added the steps that I followed to my question. Seams like similar staps you are sugessting @Chapabu
    – FLY
    Jan 3, 2012 at 15:36
  • 1
    You have a PHP/MySQL connection problem (not Drupal related) - stackoverflow.com/questions/2412009/…
    – Clive
    Jan 9, 2014 at 18:22

12 Answers 12

45

In your database configuration in the file settings.php for the Drupal site, try changing the host to 127.0.0.1 instead of localhost.

replace the default: 'host' => 'localhost',

with: 'host' => '127.0.0.1',

I've had this error on OS X XAMPP, but the solution should apply here as well. As Clive mentioned, its a MySQL connection issue. This post has more on the underlying issue.

3
  • Neat and simple solution. It works on a per-site basis, though, but it made the trick here. Nov 23, 2015 at 9:41
  • The more popular answer does not work for me (I'm using MAMP Pro), but this one does. Thanks! Jun 12, 2017 at 21:53
  • 1
    No! This might get Drush to work... but it subsequently causes Drupal to fail
    – sea26.2
    Jun 18, 2018 at 19:19
45

This was how I solved it on macOS:

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Source: Working with Drush on Mamp.

1
  • For me it was looking in "/tmp" for the mysql.sock file. So I had to create a symlink in there to my MAMP install. Just FYI for anyone that runs into that.
    – Patrick
    Mar 21, 2017 at 15:36
12

Explanation

The PDOException - error 2002 means that your drush can't connect to MySQL local database server through the socket file (e.g. /tmp/mysql.sock) as configured in your php.ini.

This is actually not much related to drush it-self, it's your PHP configuration and that error can be reproduced by:

php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8;dbname=dbname', 'root', 'root');"

Fix

If your MySQL works correctly, make sure these two files:

  1. mysql_config --socket

    or: mysqladmin variables | grep socket

  2. php -i | grep -w default_socket

    or: php -r 'phpinfo();' | grep -w default_socket

matching and points to the same file and they both exists.

If not, check which one is correct by:

mysql --socket=/path/to/mysql.sock

then fix the problem by one of following solutions:

  • make the symbolic link to point to the working mysql.sock unix socket (for MAMP, see Andrew answer),

    • for example, if you're using MAMP, you can make a symbolic link to the default location:

      sudo ln -vs /Applications/MAMP/tmp/mysql /var/mysql
      
  • correct path of your pdo_mysql.default_socket, mysql.default_socket or mysqli.default_socket in your php.ini

  • specify unix_socket in your settings.php in your $databases variable, e.g.

    $databases['default']['default'] = array(
      // ...
      'unix_socket' => '/var/mysql/mysql.sock',
    

Troubleshooting

Other edge-cases could be:

  • wrong permissions (e.g. to the parent folders),
  • you're out of space,
  • you've multiple PHP versions, check which exactly are you using and which config are you changing,
  • Debug with XDebug:

    • echo '<?php xdebug_start_trace("/tmp/foo.log");' > init.php
    • drush -c init.php ev 'Database::getConnection("default", "default");'
    • or set xdebug.show_exception_trace=1 in your xdebug.ini
  • Debug: on OS X with sudo dtruss -fn mysqld, on Linux with strace
  • See also: MySQL connection not working: 2002 No such file or directory at SO
6

I'm using MAMP Pro and had this same issue. The easiest way I could find to fix it was to add the following line to your $database array in the settings.php file.

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

In full it should look like this:

$databases = array (
    'default' =>
     array (
        'default' =>
        array (
            'database' => 'your_database_name',
            'username' => 'username',
            'password' => 'password',
            'host' => 'localhost',
            'port' => '',
            'driver' => 'mysql',
            'prefix' => '',
            'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
        ),
    ),
);

The exact error I was getting looked like this:

Additional uncaught exception thrown while handling exception.

Original

PDOException: SQLSTATE[HY000] [2002] No such file or directory in drupal_is_denied() (line 2193 of ...

Additional

PDOException: SQLSTATE[HY000] [2002] No such file or directory in _registry_check_code() (line 3477 of ...


Drush command terminated abnormally due to an unrecoverable error.

1
  • 2
    this was the quickest and easiest fix for me using MAMP
    – aek
    Feb 8, 2018 at 22:13
3

I solved this with the following steps, similar to previous replies but not the same.

Add a symlink:

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Find the php.ini being used:

php -i | grep php.ini

Add the following settings to php.ini:

pdo_mysql.default_socket= /var/mysql/mysql.sock
mysql.default_socket = /var/mysql/mysql.sock
mysqli.default_socket = /var/mysql/mysql.sock
1

This is due to a configuration error in your PHP/SQL configurations. Somewhere the socket that your webserver uses and the command line uses is configured differently. I recommend checking your MySQL configuration to see what unix socket is in use, and then make sure the same value is used in all php.ini configurations and compiles.

If you can't fix the configuration the following steps should work:

  1. Get the development version of Drush 5.x (http://ftp.drupal.org/files/projects/drush-All-versions-5.x-dev.tar.gz) and install that instead. This might cause other issues, but overall its pretty stable.
  2. In your Drupal sites settings.php remove the host and port settings for the database connection and add in a 'unix_socket' => '/path/to/mysql.sock' to replace them.

If all this seems to complicated, an alternative is to change the host in setting.php from localhost to 127.0.0.1. This will make the site slower, but for a development site it may not be noticeable or important. For a production site you want to use the unix socket and should sort out the configuration.

1
  • This is really what does it. I have tested it countless times.
    – asiby
    Sep 7, 2015 at 15:06
1

Found an easier / alternative way to install drush on a Mac:

Install homebrew by pasting this in a stand alone terminal

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Then add drush by running the following from the same terminal

brew install drush

Running drush status runs fine now.

1

If you're working with the MySQL.com installation of MySQL, you just need to point PHP at the right location for the .sock file.

In your php.ini, add/amend these two lines:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

To find the location of your php.ini (it may not exist yet), use this

php -i | grep php.ini

If the php.ini file doesn't exist there yet, create it.

1

I forgot that my local mysql runs on a unique port number so I was confused why I was getting this until I reviewed my configuration on another local site.

$databases['default']['default'] = array (
  'database' => 'hou',
  'username' => 'root',
  'password' => '',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'driver' => 'mysql',
  'collation' => 'utf8mb4_general_ci',
);
1
  • I don't know why, but I changed the 'host' => 'localhost', with 'host' => '127.0.0.1', and it is working now. Nov 24, 2021 at 5:33
0

Agree with other answers that Drupal cannot find the mysql socket. The other answers helped me but were incomplete for installing with the official Oracle Mysql service installed. Namely, the default location of the mysql.sock file. So here is a recap of my recommended answers depending on your use case:

If using mysql as included by the official MAMP package, use @Andrew Alexander's answer:

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

For the official Oracle MYSQL:

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
0

For a MAMP 4 setup, the solution was checking "Make this version available on the command line."

MAMP configuration

0

I got the following message in terminal:

(MAMP PRO + PHP 7)

Blockquote Drush command terminated abnormally due to an unrecoverable error.
[error] PDOException: SQLSTATE[HY000] [2002] No such file or directory in Drupal\Component\DependencyInjection\PhpArrayContainer->createService() Blockquote

I solved the problem with the help of these instructions

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