I was working with a local drupal instance and watned to manage it with drush (4.0 HEAD) . I got that funky SQLSTATE warning as shown

yuval@jinbocho:/srv/sites/udch6workflow/blog$ ./drush status
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) [warning]
Drupal version : 6.4
Site URI : http://udch6workflow:10105
Database driver : mysqli
Database hostname : localhost
Database username : yuval
Database name : udch6workflow
Default theme : garland
Administration theme : garland
PHP configuration : /usr/local/zendphp52/zend/etc/php.ini
Drush version : 4.0-dev
Drush configuration : /home/yuval/apps/drush/drushrc.php
Drupal root : /srv/sites/udch6workflow/blog
Site path : sites/default

Here's the drushrc.php i used:

  // Drupal root directory to use.
  $options['r'] = '/srv/sites/udch6workflow/blog';

  // URI of the drupal site to use.
  $options['l'] = 'http://udch6workflow:10105';

  //
  $options['u'] = 'yuval';

  // Load a drushrc.php configuration file from the current working directory.
  $options['c'] = '.';

  // Specify additional directories to search for *.drush.inc files
  // Use POSIX path separator (':')
  //$options['i'] = 'includes/';



either way, i tracked down the error complaint to drush/includes/environment.inc and found that if i adjusted the $type line like so the warning went away:

$type = ($creds['driver'] == 'mysqli') ? 'mysqli' : $creds['driver'];

A patch with this attachment is attached below.
Is the patch fixing something or did I just miss something? :)

Thanks!

Yuval

CommentFileSizeAuthor
drush_type_mysqli.patch943 bytesyuvilio
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drewish’s picture

Seems like based on your experience that the line should just be removed entirely. Since it's turning into a no-op in your case.

greg.1.anderson’s picture

Yes, the patch is the same as removing the modified line. I'm not sure why the original code switches from 'mysqli' to just 'mysql', or what the affect for others would be if this modification was removed. I'm guessing that it's not a good idea.

Anonymous’s picture

Component: Code » PM (dl, en, up ...)

I ran into this issue when running updb. Every other Drush command was fine, except updating the database. The quick fix for me was to simply make a symbolic link from my mysql.sock file (/Applications/MAMP/tmp/mysql/mysql.sock) to /var/mysql/mysql.sock. For certain commands, Drush wants the socket file to be located in /var/mysql

jonhattan’s picture

Component: PM (dl, en, up ...) » Base system (internal API)
Assigned: Unassigned » jonhattan
Status: Active » Postponed (maintainer needs more info)

At present the code does not use PDO (#571352: false positives verifying database credentials in validation of DRUSH_BOOTSTRAP_DRUPAL_DATABASE) and looks like:

      // Verify connection settings.
      switch ($type) {
        case 'mysql':
        case 'mysqli':
          $connection = @mysql_connect($creds['host'], $creds['user'], $creds['pass']);
          if (!$connection || !mysql_select_db($creds['name'])) {
            drush_log(mysql_error(), 'bootstrap');
            return FALSE;
          }
          break;
        case 'pgsql':

I was lazy to add explicit mysqli support. Test with HEAD version. If it doesn't work I'll move to implement the mysqli case.

jonhattan’s picture

Status: Postponed (maintainer needs more info) » Fixed

mysqli support was already implemented: http://drupal.org/node/571352#comment-3856212

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hass’s picture

Priority: Normal » Critical
Status: Closed (fixed) » Active

This bug is back. For me drush worked well with 4.x and not with 5.x it's broken. I'm in a jailed linux shell and I do not have permission to access mysqld.sock. This worked all flawless in past and the database is configured and running :-). The update module is also enabled and works in the website.

:/web$ drush up --debug
Bootstrap to phase 0. [0.01 sec, 3.69 MB]                            [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drush() [0.01 sec, 3.91 MB] [bootstrap]
Cache HIT cid: 5.4-commandfiles-0-d617a3ae39a107c58e49bc9e629d9ca6 [0.02 sec, 3.93 MB]                                     [debug]
Bootstrap to phase 0. [0.06 sec, 8.74 MB]                                                                              [bootstrap]
Bootstrap to phase 6. [0.07 sec, 8.74 MB]                                                                              [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drupal_root() [0.07 sec, 8.75 MB]                                             [bootstrap]
Initialized Drupal 7.14 root directory at /web [0.09 sec, 11.9 MB]                                                        [notice]
Drush bootstrap phase : _drush_bootstrap_drupal_site() [0.09 sec, 11.91 MB]                                            [bootstrap]
Initialized Drupal site default at sites/default [0.09 sec, 11.91 MB]                                                     [notice]
Cache HIT cid: 5.4-commandfiles-2-c32acd775c18a90c5b41d0fc9accf5ab [0.1 sec, 11.92 MB]                                     [debug]
Drush bootstrap phase : _drush_bootstrap_drupal_configuration() [0.1 sec, 11.92 MB]                                    [bootstrap]
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) [0.1 sec,[bootstrap]
11.95 MB]
Bootstrap to phase 5. [0.1 sec, 11.94 MB]                                                                              [bootstrap]
Command pm-update needs a higher bootstrap level to run - you will need invoke drush from a more functional Drupal     [error]
environment to run this command. [0.1 sec, 11.94 MB]
Command pm-update needs the following modules installed/enabled to run: update. [0.1 sec, 11.94 MB]                    [error]
The drush command 'up' could not be executed. [0.1 sec, 11.94 MB]                                                      [error]
Drush was not able to start (bootstrap) the Drupal database.                                                           [error]
Hint: This error often occurs when Drush is trying to bootstrap a site that has not been installed or does not have a
configured database.

Drush was attempting to connect to :
  Drupal version    : 7.14
  Site URI          : http://default
  Database driver   : mysql
  Database hostname : localhost
  Database username : foo
  Database name     : foo
  Default theme     : garland
  Administration theme: garland
  PHP configuration : /etc/php5/cli/php.ini
  Drush version     : 5.4
  Drush configuration:
  Drupal root       : /web
  Site path         : sites/default
  Modules path      : sites/all/modules
  Themes path       : sites/all/themes
  File directory path: sites/default/files
  %paths            : Array

You can select another site with a working database setup by specifying the URI to use with the --uri parameter on the
command line or $options['uri'] in your drushrc.php file.
 [0.11 sec, 11.95 MB]
hass’s picture

I also believe that %paths : Array is a bug, too.

hass’s picture

Title: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) » Run mysqldump by default with "-h hostname" / Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Mysql should not use the sock way by default as this may not allowed in jailed environments. -h must work always with the configured site settings or the site would otherwise not work.

greg.1.anderson’s picture

A patch that fixed the problem would be very useful.

jonhattan’s picture

Title: Run mysqldump by default with "-h hostname" / Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) » Can't connect to local MySQL server through socket '/tmp/mysql.sock'
Assigned: jonhattan » Unassigned
Category: bug » support
Priority: Critical » Normal
Status: Active » Postponed (maintainer needs more info)

It seems your jailed shell has no access to the mysql socket file. I don't see the relation with mysqldump and -h cli option since the problem happens when drush tries to connect the database, with php. Also it already passes the hostname to the connection function. See drush_valid_db_credentials()

"localhost" means socket connection. To use tcp/ip hostname must be 127.0.0.1(*). So I don't see how it was working for you with drush 4.x and not with 5.x. Anyway the difference between 4.x and 5.x in this respect is the addition of support for unix_socket added in #437642: support for different MYSQL socket. Is this related to your setup?

(*) reference issue #1428638: Forcing 127.0.0.1 instead of localhost for drush

hass’s picture

Title: Can't connect to local MySQL server through socket '/tmp/mysql.sock' » SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) if "localhost" is used
Status: Postponed (maintainer needs more info) » Closed (fixed)

The reason that it no longer works is my hoster has limited the permissions to socks file. In past I had permission and now he suggested to use the IP address with -h 127.0.0.1. I was not aware that localhost is a special case for mysql dump.

Like the original reporter, who has 100% the same situation (localhost) including the same error message, I'm verbose now, we should configure Drupal to use "127.0.0.1" for the database connection and NOT "localhost. I'm fine with this, but it's something that should be documented!

The better would be if Drush adds the special case documented in #1428638: Forcing 127.0.0.1 instead of localhost for drush.

jonhattan’s picture

Component: Base system (internal API) » Documentation
Status: Closed (fixed) » Active

I agree we should add some notice in the error output near this:

Hint: This error often occurs when Drush is trying to bootstrap a site that has not been installed or does not have a configured database.

perhaps show the message conditionally based on the error number and/or hostname = localhost.

jonhattan’s picture

Title: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) if "localhost" is used » Give advice on the "can't connect to local MySQL server through socket" error
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.