On several shared-hosting servers I have used for Drupal, a jailshell is provided for ssh access. Drush may not work on a such system if the jailshell does not provide access to /var/lib/mysql/mysql.sock.

Note (2015-11-06): As this issue is being referenced in Drush, it also serves as a guide on fixing the bootstrapping error in different, newer enviroments (AMP stacks on OSX, Linux + VMs).
Please refer to solutions in comments like #23 and #43 and #44 for this case.

You can see if drush is going to work just by trying mysql from the command line. If you get an error like below, it probably will not:

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

The usual workaround suggested is to change the 'host' setting in your settings.php. Before the change, it looks something like this (in D7: D6 is analogous but uses $db_url = 'mysql://username:password@localhost/databasename';):

$databases = array (
[...]
      'database' => 'yourdatabase',
      'username' => 'yourusername',
      'password' => 'yourpassword',
      'host' => 'localhost',
[...]

MySQL treats 'localhost' specially, and uses the Unix socket instead of a network socket. If you change the 'host' to 127.0.0.1:

'host' => '127.0.0.1',

then MySQL always uses a network socket, and drush will work. However, this is not the best idea, because it's more efficient to use the Unix socket (I've seen benchmarks of 6-10% faster).

I tried using a drush alias to change the $databases setting, but not successfully (cf. #1335626: alias db-url is ignored). So I am now using this simple bit of code in settings.php which works for m. Change the 'host' line to:

'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'localhost',

I have not seen suggested elsewhere. It is a simplified version of how drush checks for being run from the command line:

function drush_verify_cli() {
  return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER[
'argc'] > 0));
}

You may need the more complicated check for some hosts.

If someone has an alternate suggestion for getting drush to use 127.0.0.1 without messing with settings.php, I would be interested in hearing it. I have filed this as a support request, but it could turn into a documentation task.

Comments

dhalbert’s picture

Category: task » support

Somehow the end of the OP got truncated. To finish:

This is similar to what drush uses to check for being run from the command line:

function drush_verify_cli() {
  return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER[
'argc'] > 0));
}

For some hosts, the extra check may be necessary.

If someone has a better way of doing this with drush, I'd be interested in hearing it. Otherwise I'd suggest that some hint like this going into the drush documentation somewhere (and this issue would turn into a documentation task).

moshe weitzman’s picture

Status: Active » Fixed

Personally, I just use 127.0.0.1 and be done with it. But drush5 honors Drupal 7's unix socket support - #437642: support for different MYSQL socket

Status: Fixed » Closed (fixed)

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

dylanb’s picture

Version: » 7.x-5.9

Apologies if I'm just supplying subtitles for the hard of understanding but in case you need to edit your drupal 6 settings.php file to use the localhost socket for regular website activity and 127.0.0.1 for drush then this works for me...

if (php_sapi_name() == 'cli') {
  $db_url = 'mysqli://username:password@127.0.0.1/database';
}
else {
  $db_url = 'mysqli://username:password@localhost/database';
}
ceeweb’s picture

Issue tags: +PostgreSQL

Sorry to "reopen" this case, but I have the same problem except I am using Postgresql and this solution did not work for me....

My drupal installation is installed at: C:\SiteData\WebDev\gitproject (this is my DrupalRoot)
I successfully ran "drush status" in "[DrupalRoot]\sites\site1.example.com..

But when I try to enable a module using "drush en ccl" and I get ...
======================================

$ drush en ccl
Command pm-enable needs a higher bootstrap level to run - you will [error]

need to invoke drush from a more functional Drupal environment to run
this command.
The drush command 'en ccl' could not be executed. [error]

Drush was not able to start (bootstrap) the Drupal database. [error]

Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not have a
configured database. In this case you can select another site with a
working database setup by specifying the URI to use with the --uri
parameter on the command line. See `drush topic docs-aliases` for
details.
* connect the database through a socket. The socket file may be
wrong or the php-cli may have no access to it in a jailed shell. See
http://drupal.org/node/1428638 for details.

Drush was attempting to connect to:
Drupal version : 7.26
Site URI : http://site1.example.com
Database driver : pgsql
Database username : user
Database name : site1dev
Default theme : garland
Administration theme : garland
PHP executable : php
PHP configuration : "C:\Program Files
(x86)\Drush\Php\php.ini"
PHP OS : WINNT
Drush version : 6.0
Drush configuration :
Drush alias files :
Drupal root : c:/SiteData/WebDev/gitproject
Site path : sites/site1.example.com
File directory path : sites/site1.example.com/files

My settings.php file looks like this:
==============================

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'site1dev',
      'username' => 'user',
      'password' => 'secretpw',
      'host' => '127.0.0.1',
      'port' => '',
      'driver' => 'pgsql',
      'prefix' => 'dev_',
    ),
  ),
);


if (php_sapi_name() == 'cli') {
  $db_url = 'pgsql://user:secretpw@127.0.0.1/site1dev';
 }
 else {
   $db_url = 'pgsql://user:secretpw@localhost/site1dev';
 }

I have used both the "Drush Command Prompt" and "MINGW32" git command line tool and both work for "drush status" or "drush dl " but not for "drush en "

One last comment. looking at the settings.php file permissions in MINGW32 they show the unix permissions of -rw-r--r--.. Tried changing this in using chmod but doesn't work on a windows file (not even sure that makes a difference).

Any help is much appreciated!

helmo’s picture

Drush has moved to Github.

If this feature is still desired, you may copy it to our Github project. For best results, create a Pull Request that has been updated for the master branch. Post a link here to the PR, and please also change the status of this issue to closed (duplicate).

Please ask support questions on Drupal Answers.

ceeweb’s picture

Thanks helmo I opened a drush issues ticket on github at https://github.com/drush-ops/drush/issues/492 and on Drupal Answers at http://drupal.stackexchange.com/questions/105274/drush-enable-module-not.... This can be closed.

ceeweb’s picture

Status: Closed (fixed) » Closed (duplicate)
matt.c’s picture

Hello,

I am having difficulty using Drush on my Webdisk server, can somebody please assist me.

1) Drush is properly installed on my computer, when I type drush version I get: Drush Version : 7.0-dev

2) I can install modules, when I cd /sites/all/modules and type drush dl disqus for instance

3) I properly installed the theme omega in my /sites/all/themes

4) The error is occurring when I try to run the drush omega-wizard to create a new Omega sub theme

I am getting the following error message

MacBook-Pro:themes mattc$ drush dl omega
Project omega (7.x-4.2) downloaded to sites/all/themes/omega. [success]
drushProject omega contains 2 themes: omega, ohm.
MacBook-Pro:themes mattc$ drush owiz
The drush command 'owiz' could not be found. Run `drush [error]
cache-clear drush` to clear the commandfile cache if you have
installed new extensions.
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not
have a configured database. In this case you can select another
site with a working database setup by specifying the URI to use
with the --uri parameter on the command line. See `drush topic
docs-aliases` for details.
* connect the database through a socket. The socket file may
be wrong or the php-cli may have no access to it in a jailed
shell. See http://drupal.org/node/1428638 for details.

Drush was attempting to connect to:
Drupal version : 7.26
Site URI : http://default
Database driver : mysql
Database username : website_drup3
Database name : website_drup3
PHP executable : /usr/bin/php
PHP configuration : /private/etc/php.ini
PHP OS : Darwin
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : /Volumes/myserver.se
cureserver.net/public_html
Site path : sites/default

Can somebody please help me with why Drush is working for some commands and not for others? What do I need to change?

Thanks,
- Matt

helmo’s picture

@matt.c: This issue is already closes ... See https://drupal.org/comment/8533111#comment-8533111

Jaypan’s picture

Drush has moved to Github.

If this feature is still desired, you may copy it to our Github project. For best results, create a Pull Request that has been updated for the master branch. Post a link here to the PR, and please also change the status of this issue to closed (duplicate).

Please ask support questions on Drupal Answers.

This is so horrible for the Drupal community. It really disappoints me that the maintainers of Drush, such an integral part of Drupal, would choose to be so anti-community. Someone should branch Drush and keep it on Drupal.

RaulMuroc’s picture

Agree with Jaypan.

protools’s picture

1) try run mysql from command line:
$ mysql
If not found error, create symbol link to bash:
$ sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/bin/mysql

try drush en

2) If not working and in setting.php you using unix socket like: 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

try create symbol link to unix socket:

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

ore

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

work for me on OS X yosemite + MAMP 3

Anonymous’s picture

Damn, setting up Drush is soooooo hard
For such a highly recommended tool it is so elusive and reserved only for the technically gifted.
It's taken me a week to get to the stage where I can install a module
Now, I have to reserve another week to work out how to enable it
It's all character building I suppose :)

protools offered me a glimmer of hope, but his option 2) has me hit a wall as I do not have
"/Applications/MAMP/tmp/mysql/mysql.sock "

So now have to learn about that...

blimey, is it worth it? YES, I hear you cry

jazzper’s picture

I am not able to get drush to work properly:

1) I can download modules & themes through drush but I cannot enable them (more details below).
2) Also I cannot use the omega commands even though they are listed (more details below).

= I installed Composer & Drush as per http://docs.drush.org/en/master/install/
= Drush is properly installed on my computer, when I type drush version I get: Drush Version : 7.0-dev

My system setup:
Linux mint 17 kde
Xampp 1.8.3-4

I guess my problems are (partly?) caused because Drush was not able to start (bootstrap) the Drupal database. So I run mysql from command line:
user@mylaptop:/opt/lampp/htdocs/drush/sites/all/modules > mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

In my system mysqld.sock is in a different location, namely /opt/lampp/var/mysql/mysql.sock. So I created a symbolic link in /var/run/mysqld/ but this has no effect. Still have the same problems. Maybe caused by permissions?

I have spend a few days of and on (had to stop out of frustration) and I cannot get drush to work. I know this issue is closed and I will post my problems on git but maybe somebody here knows what goes wrong. Any help would be much appreciated.

Details of my problems:
1) I can download modules & themes through drush but I cannot enable them:

user@mylaptop:/opt/lampp/htdocs/drush/sites/all/modules > drush en disqus
Command pm-enable needs a higher bootstrap level to run - you will need to invoke [error]
drush from a more functional Drupal environment to run this command.
The drush command 'en disqus' could not be executed. [error]
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not have a configured
database. In this case you can select another site with a working database setup by
specifying the URI to use with the --uri parameter on the command line. See `drush
topic docs-aliases` for details.
* connect the database through a socket. The socket file may be wrong or the
php-cli may have no access to it in a jailed shell. See
http://drupal.org/node/1428638 for details.

Drush was attempting to connect to:
Drupal version : 7.34
Site URI : http://default
Database driver : mysql
Database hostname : localhost
Database port :
Database username : root
Database name : drush
PHP executable : /usr/bin/php
PHP configuration : /etc/php5/cli/php.ini
PHP OS : Linux
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : /opt/lampp/htdocs/drush
Site path : sites/default

When I add
'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'localhost',
OR
'host' => '127.0.0.1',
to settings.php as per http://drupal.org/node/1428638 I get following error:

user@mylaptop:/opt/lampp/htdocs/drush/sites/all/modules > drush en disqus
PHP Fatal error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in /opt/lampp/htdocs/drush/includes/database/mysql/database.inc on line 46
Drush command terminated abnormally due to an unrecoverable error. [error]
Error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in
/opt/lampp/htdocs/drush/includes/database/mysql/database.inc, line 46

2) Also I cannot use the omega commands even though they are listed:
user@mylaptop:/opt/lampp/htdocs/drush/sites/all/modules > drush help --filter=omega
All commands in omega: (omega)
omega-export (oexp) Exports the theme settings of a given theme from the database to the
.info file.
omega-guard (ogrd) Runs guard for the given theme including Compass and LiveReload by
default.
omega-revert (orev) Reverts the theme settings of a given theme by deleting them from the
database.
omega-subtheme Creates a Omega subtheme.
(osub)
omega-wizard (owiz) Guides you through a wizard for generating a subtheme

achton’s picture

Fruit fly: the Drush folks do not give support here any longer (I think), and also, this issue is for a separate (albeit) related problem. Furthermore, you seem to have multiple seperate issues (MySQL connectivity, missing extension, Omega) - it might be worth it to focus on one problem at a time.

As Moshe says in comment #2: use '127.0.0.1' for your hostname and be done with it. If you are not knowledgeable enough to fix Unix sockets (the localhost special case) in your setup, then don't waste time on it.

The error you are having when using 127.0.0.1 (Error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY..) is probably related to an extension you are missing. See this Google seach.

jazzper’s picture

Hi achton,

Thank you for your reply. You make some good suggestions to get me back on trying to solve this problem:)

jazzper’s picture

OK I've got drush working:) For other people who might have a similar problem (hope I use the right terms):

I had to install "php5-mysql" (and dependencies) in Linux (for example through "synaptic package manager"), php5-mysql is a MYSQL module for PHP5 which contains mysql PDO support. With this I was able to enable modules and use the omega commands. Drush uses the PHP installed in Linux and not in Xammp, I had this module in Xampp but not in Linux.

You can see which php.ini file drush uses by typing "drush status" in your terminal.

You can see which php.ini file is used by your web server by navigating to admin/reports/status of your drupal site. Next click the "more information" link on Drupal's status page's PHP section. This shows the phpinfo() page. Locate the Configuration File (php.ini).

To check that the mysql PDO support is installed (part of "php5-mysql"), type the following in a terminal:
php -i|grep PDO
If support is installed you should see something like this:
PDO
PDO support => enabled
PDO drivers => mysql
PDO Driver for MySQL => enabled

pierrozone’s picture

For anyone facing this issue with WAMP, I suggest you add mysql to your Path enviroment.
This article provides the best instructions for windows: http://drupalistasgroup.com/installing-drush-7-windows-xampp

bsergiu’s picture

Well I'm having the same issues with Command pm-disable needs a higher bootstrap level to run. For my project I'm using php5.3 and its running form my project when i run 'drush status'

Drupal version : 7.34
Site URI : http://default
Database driver : mysql
Database hostname : 127.0.0.1
Database port :
Database username : drupforma19
Database name : drupforma19
PHP executable : /Applications/AMPPS/php-5.3/bin/php
PHP configuration : /Applications/AMPPS/php-5.3/etc/php.ini
PHP OS : Darwin
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : /Applications/AMPPS/www/forma
Site path : sites/default

Ive changed the hostname, the mysql port is 3306 and I've already run sudo ln -s /Applications/AMPPS/mysql/tmp/mysql.sock /var/mysql/mysql.sock and that is ok but i still get the message Command pm-disable needs a higher bootstrap level to run. Any help would be appreciated.

bsergiu’s picture

Well I'm having the same issues with Command pm-disable needs a higher bootstrap level to run. For my project I'm using php5.3 and its running form my project when i run 'drush status'

Drupal version : 7.34
Site URI : http://default
Database driver : mysql
Database hostname : 127.0.0.1
Database port :
Database username : drupforma19
Database name : drupforma19
PHP executable : /Applications/AMPPS/php-5.3/bin/php
PHP configuration : /Applications/AMPPS/php-5.3/etc/php.ini
PHP OS : Darwin
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : /Applications/AMPPS/www/forma
Site path : sites/default

Ive changed the hostname, the mysql port is 3306 and I've already run sudo ln -s /Applications/AMPPS/mysql/tmp/mysql.sock /var/mysql/mysql.sock and that is ok but i still get the message Command pm-disable needs a higher bootstrap level to run. Any help would be appreciated.

Patrick Storey’s picture

I'm getting the same error as bsergiu although I'm using Drush 5.10.0.

Using a Mac OS, I will report back if I can figure out how to solve this.

Drupal version : 7.34
Site URI : http://default
Database driver : mysql
Database hostname : 127.0.0.1
Database username :
Database name :
Default theme : garland
Administration theme : garland
PHP configuration :
Drush version : 5.10.0
Drush configuration :
Drush alias files :
Drupal root :
Site path : sites/default
File directory path : sites/default/files

Patrick Storey’s picture

For me it required a little more work to change my local settings.php to get the drush en to work. Using Drupal 7, MAMP Pro, PHP 5.3.29, Drush 5.10.0

In the local settings.php I had to change the following in the file for the database. Adding the unix socket and port, and then changing the host to 127.0.0.1 as shown below.

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'host' => '127.0.0.1',
'port' => '8889',

bsergiu’s picture

I've tried that as well the unix socket path is defined in my bash profile but it doesnt seem to use that sql path. The strange thing is that i managed to get it working on MAMP3, but mamp has a bug where it wont let you sign in to your durpal website after a couple of hours, so i prefer using ampps.

bsergiu’s picture

I've tried that as well the unix socket path is defined in my bash profile but it doesnt seem to use that sql path. The strange thing is that i managed to get it working on MAMP3, but mamp has a bug where it wont let you sign in to your durpal website after a couple of hours, so i prefer using ampps.

kevinquillen’s picture

Same exact issue here. I installed Drush via homebrew (6.5.0) and it works perfectly. I cannot get it to work with Drush 7 at all, installed globally over composer. I can't figure it out at all.

donalfitz’s picture

Having recently updated to MAMP pro 3.1 running on Yosemite Drush no longer worked. The Drupal site ran fine, command-line MySQL was ok, but Drush would not connect. I followed the recommendations in comment No. 13 above (mirrored elsewhere on different sites) but still no joy. Eventually, I did:

cd /tmp
sudo ln -s /Applications/MAMP/mysql/mysql.sock mysql.sock

This solved the problem for me, so I hope it is useful for others.

pianodavid’s picture

donalfitz: Thank you so much! I tried everything in #13 too, but I had to do what you did to make it work. I am on OS X 10.9.5 (Mavericks) and it was the update to MAMP PRO 3.1 that made drush stop working.

kevinquillen’s picture

What if you are using Vagrant stacks and not MAMP? And why would Drush 6.5.0 work and not 7?

basvredeling’s picture

I'm having similar issues as #26 and #27. Tried everything and then some.

My environment is a Homebrew installed Drush (7), just upgraded to Mamp 3.1 on Yosemite. Since the upgrade to 3.1 things are messed up on the drush front.
I reverted back to Drush 6 (brew switch drush 6.2.0) and now everything's fine. Changing localhost to 127.0.0.1 also works. But since I have 100s of settings.php files on my multiple development machines I won't just "change it to 127.0.0.1 and be done with it".
It looks like it's related to a combination of Drush 7 and MAMP 3.1 (php 5.6.6).

For now I'll run Drush 6 but that means no Drupal 8 development with Drush without switching versions.

fredklopper’s picture

Same problems after upgrading to MAMP 3.1. I use Drush version 6.5.0 (homebrew).

This symlink solved it for me:

/tmp/mysql.sock -> /Applications/MAMP/tmp/mysql/mysql.sock

nick.morahan’s picture

For me it wasn't restricted to Drush 7 & Mamp 3.1, as I downgraded Drush to version 6 and still got problems. The symbolic link at #27 still isn't working, but for now #23 is doing the trick.

nick.morahan’s picture

Thought I'd have another look at the symlink solution and got it working:

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

kevinquillen’s picture

Does anyone know why that works though?

basvredeling’s picture

#34 exactly! and why it suddenly stopped working.

kevinquillen’s picture

I don't use MAMP, I have a Vagrant stack, and Drush will not work like before, even when using the hostname.

basvredeling’s picture

Perhaps Drush 7 is only trying to access the database via the network. That would explain why localhost doesn't work but an ip address does.

kevinquillen’s picture

Sorry, the hostname as in something like 'local.mysite.com' in my hosts file, which maps to a Vagrant site - so it should work just the same as a local IP. Nothing does, though.

Drush 7 works from within the VM, but it should be able to work from both sides. I also tried using a Drush alias.

seanbfuller’s picture

Seems that the issue queue has moved over to github and this was marked as a dupe. However, since this is coming up for searches regarding localhost aliases for drush I thought I'd point people to a potentially relevant issue: https://github.com/drush-ops/drush/pull/546

In that thread is documented a new flag:

  '#check-local' => TRUE,

After setting up a new laptop and grabbing the latest version of drush, I was having issues getting drush to recognize the localhost alias. It kept trying to access the site via ssh, which resulted in this error: "ssh: connect to host example.local port 22: Connection refused." Adding that flag to my alias file under the local site entry got it working again. Hope that helps others who stumble across this thread.

mobius_time’s picture

When running Drush 6.6 and MAMP PRO 3.1, I couldn't get drush to work (PATH was set correctly, I could bring up mysql, etc.). Upgraded to MAMP PRO 3.2.1 and now drush works.

twooten’s picture

@Fruit Fly, thank you for your work in figuring this out for a Linux Mint + XAMPP setup. This has been a pain for me for many months. I'm running the same setup (mostly) as you but just had no time to investigate.

sudo apt-get install php5-mysql did the trick for me.

torgosPizza’s picture

The fix in #23 worked for me as well, using the latest MAMP Pro on OSX Yosemite.

dave bruns’s picture

With MAMP Pro 3.2.1, OSX 10.7.5, PHP 5.6.7

I ended up at this issue because I was installing a D7 site with drush make and couldn't get it to work. Finally I noticed that I could switch to Drush 7 with composer and it worked fine. But then, Drush en and dis failed with a "Drush was not able to start (bootstrap) the Drupal database. " message. Same commands worked perfectly with Drush 6.5.

Drush status with Drush 7 *looked* OK, but I didn't notice until later that it was missing things like

Drupal bootstrap : successful.

After too long, I used the Drush --debug flag and noticed this in the output:

sh: mysql: command not found

So, once I added this to .profile file, Drush 7 started to work fine:

export PATH=$PATH:/Applications/MAMP/Library/bin

Still not sure exactly why things worked well with Drush 6 vs Drush 7, but hopefully this is helpful to someone.

Helpful links:

http://www.webbykat.com/2012/06/solving-sh-mysql-command-not-found-mamp-...
http://drupal.stackexchange.com/questions/125345/drush-was-not-able-to-s...
http://www.razorsql.com/docs/support_mysql_mac_mamp.html

kevinquillen’s picture

For people who don't use MAMP and get the same error, what is the resolution?

pio.fernandes’s picture

Hi. I was getting some problems, and with --debug I saw that mysql was not being executed/recognized by drush, so my fix was to add mysql.exe path to the PATH var, in my Windows7. Hope it helps.

mhamed’s picture

Hello
the same errors I had for the socket and drush --version 7
first solved the socket problem since i could not enter mysql from code :
Gave me the socket error:
1-here on this site i tried changing my /etc/my.cnf

mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

changing this socket=/var/lib/mysql/mysql.sockto this socket=/var/mysql/mysql.socksince i ve got no lib folder.
So my final my.cnf is

# The MySQL server

[mysqld]
datadir=/usr/local/mysql/data
socket=/var/mysql/mysql.sock
 max_allowed_packet = 128M

[mysql.server]
user=mysql
basedir=/usr/local/mysql


 [client] 
 port=3306 

2-adding the symlink of the socket if it does not exist in : /var/mysql/like this:
sudo ln -s /Applications/MAMP/mysql/mysql.sock /var/mysql/mysql.sock

3-This solved the mysql link but the problem of drush remain :
renamed the host :127.0.0.1
but has no effect the same error when want to enable a module.
-added export PATH=/Applications/MAMP/Library/bin/:$PATHinto .bash_profile file .
-but the error the same :

Command pm-enable needs a higher bootstrap level to run - you will need to invoke drush from a more functional Drupal    [error]
environment to run this command.
The drush command 'pm-enable colorbox' could not be executed.                                                            [error]
Drush was not able to start (bootstrap) the Drupal database.                                                             [error]
Hint: This may occur when Drush is trying to:
 * bootstrap a site that has not been installed or does not have a configured database. In this case you can select
another site with a working database setup by specifying the URI to use with the --uri parameter on the command line. See
`drush topic docs-aliases` for details.  .......

4-added as advised in the page

by #25
copying him in :

added line

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

in settings.php

for instance

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

And with this drush 7 is in a good mood to brew
thanks

kevinquillen’s picture

Drush works on a VM if I specify the site alias now.

patrickwatzeels’s picture

Make sure that mysql is available in the PATH environment so you can access mysql everywhere. That solves the problem for me.

dasginganinja’s picture

For those who were experiencing a database-related drush error such as:

Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not have a configured
database. In this case you can select another site with a working database setup by
specifying the URI to use with the --uri parameter on the command line. See `drush
topic docs-aliases` for details.
* connect the database through a socket. The socket file may be wrong or the
php-cli may have no access to it in a jailed shell. See
http://drupal.org/node/1428638 for details.

I found on a non-MAMP system (Digital Ocean Ubuntu Droplet) that ~/.my.cnf existed with a password directive set. When I removed this file it was able to work. I found this odd because executing the output of drush sql-connect worked. I hope this helps somebody.

dobie_gillis’s picture

My database settings are correct, and I'm getting this error with drush 7.0.0, php 5.3.10.

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'db_name',
      'username' => 'drupaluser',
      'password' => '********',
      'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'localhost',
      'port' => '3306',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
)

Is there a resolution for this issue?

EDIT: figured it out. I had to do chmod a+x /etc/php5

Stolzenhain’s picture

Issue summary: View changes
delacosta456’s picture

Hi
#13 .. 1) solved on mac os el captain with drupal 7.41 and drush v7 installed trought homebrew

Thanks

Andru’s picture

Got same errors as #15, able to download modules but not enable them.

Solution: had to CD to run on the correct local site directory (multi-site local install on Linux).

Went from /var/www/drupal7 to /var/www/drupal7/sites/example.com and then Drush enabled modules just fine.

bluesky_still’s picture

[Site gone and replaced with gambling site] works for me!!!!

deajan’s picture

#49 worked for me.
Had ~/.my.cnf file with password. Think this is a drush bug.

zakmahboub22’s picture

Issue summary: View changes
pascalli’s picture

Hi bluesky_still your link in #54 did the trick for me. Cool !!!! Feel so releaved.

ragavendra_bn’s picture

Comment #49 , made it work like a charm, just renaming or removing ~/.my.cnf helped on LAMP hosting.

knalstaaf’s picture

I didn't have to make any modifications at all, like changing localhost to 127.0.0.1. These three commands in Terminal solved the issue:

cd /usr/local/bin
sudo ln -s /Applications/MAMP/Library/bin/mysql
sudo ln -s /Applications/MAMP/Library/bin/mysqldump

(Source)

mfuller526’s picture

#59 works perfectly. Thank you!!!

AndraeRay’s picture

I'm on windows here using composer and I was facing the same problem.

By adding mysql to my path from comment #43 fixed it.

I added it to my user environment variables.

zorz’s picture

In my case (SQLSTATE[HY000] [2002] Connection refused) this article helped
https://modulesunraveled.com/blog/drush-not-working-mamp-heres-how-fix-it
added the following line in settings.php
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

nitin.k’s picture

Thanks #13. This link can help the needy one.

few commands to debug..
drush en statistics
drush status

Common messages:
"No Drupal site found, only 'Drush' cache was cleared."
"Drush was not able to start (bootstrap) the Drupal database."

In drupal root folder make sure we have correctly configured the database connection.
Use 127.0.0.1 in place of localhost, make changes in settings.php or in files which are included in settings.php.
MySql should be correctly linked to Drush which is done in case of Mamp. #13 and #43 works so well.

scott859’s picture

#43 Worked for me - thank you.

TristanTheKnightAway’s picture

Just wanted to pop in to say I couldn't resolve my drush7 issue using any of the above solutions until a colleague suggested just using drush6, which worked. Good article from Lullabot on how to add drush versions https://www.lullabot.com/articles/switching-drush-versions

Everything works now. For Drupal 7 sites, you can just use drush6 and for Drupal 8 sites you can just use Drupal console and life might be simpler.

p.s. using MAMP and PHP 7.1.1

trinhminhtriet’s picture

#43 worked for me too.

HansKuiters’s picture

#59 worked for me. Thanks.

pyim’s picture

change database setting from 'localhost' to '127.0.0.1' in settings.php works for me. Jeez, took me 4 hours, why didn't I find this post sooner.

mikeytown2’s picture

FYI Drush now requires the mysql client to be installed on your web head box. To see if it's there run
sudo find / -type f -name "mysql"
If not then

CentOS/Redhat
sudo yum install mysql

Ubuntu/Debian
sudo apt-get install mysql

Well that was fun.

davidandaman’s picture

I still have the same problem with AMPPS for MacOX Sierra.

iMac-de-David:i_resto_theme davidjulien$ drush omega-guard
The drush command 'omega-guard' could not be found. Run `drush [error]
cache-clear drush` to clear the commandfile cache if you have
installed new extensions.
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not have a
configured database. In this case you can select another site with a
working database setup by specifying the URI to use with the --uri
parameter on the command line. See `drush topic docs-aliases` for
details.
* connect the database through a socket. The socket file may be
wrong or the php-cli may have no access to it in a jailed shell. See
http://drupal.org/node/1428638 for details.

Drush was attempting to connect to:
Drupal version : 7.59
Site URI : http://default
Database driver : mysql
Database hostname : 127.0.0.1
Database port :
Database username : root
Database name : irestolotus2018new2
PHP executable : /usr/bin/php
PHP configuration :
PHP OS : Darwin
Drush script : /Users/davidjulien/.composer/vendor/drus
h/drush/drush.php
Drush version : 7.4.0
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : /Applications/AMPPS/www/LOTUSBAR
Site path : sites/default

Any help would be appreciated.

joehudson’s picture

For anyone still flummoxed by this issue, using D6 with PHP7 in cli, then the issue I found was that mysql_connect is not defined in PHP7 (replaced with alternatives).
This is why Drush reports mysql not installed, when using PHP7 on a D6 core, even though db modules are loaded.
So a quick fix to enable core update (D6LTS) was to use shims for mysql and also ereg.
I used https://github.com/dshafik/php7-mysql-shim/blob/master/lib/mysql.php
and
https://github.com/bbrala/php7-ereg-shim/blob/master/lib/ereg.php

joelpittet’s picture

Thanks @joehudson that worked for me I added them with composer:
composer require --dev dshafik/php7-mysql-shim bbrala/php7-ereg-shim

Dru18’s picture

I came back in using MAMP (v 5.x) on Mac. I faced the same issue (D7) I used to have before.
As the above comment (somewhere), adding the following line in .profile (or .bash_profile) resolves my issue.

export PATH="/Applications/MAMP/Library/bin/:$PATH"

Then,
$ source ~/.profile
or $ source ~/.bash_profile

Tried both 127.0.0.1 and localhost: The hostname doesn't matter to me.

fonant’s picture

I came across this issue when trying to use drush in a chroot jail. Drush working fine, but couldn't connect to MySQL/MariaDB.

Mounting a copy of the mysql.sock file into the equivalent location in the chroot jail sorted the issue nicely.

Outline method, for a socket called /var/lib/mysql/mysql.sock, and chroot at /var/chroot/jailname/:

mkdir -p /var/chroot/jailname/var/lib/mysql
touch /var/chroot/jailname/var/lib/mysql/mysql.sock
mount --bind /var/lib/mysql/mysql.sock /var/chroot/jailname/var/lib/mysql/mysql.sock

Then drush can connect using "localhost" instead of needing to change to "127.0.0.1".

kvu@csusm.edu’s picture

@mikeytown2
I tried changing localhost to 127.0.0.1, but that didn't work. But reading your comment gives me an idea. I check my php7 modules and I was missing many:

sudo yum list installed | grep --color php
sudo yum install php71w-pdo php71w-ldap php71w-mbstring php71w-devel php71w-gd php71w-intl php71w-mysqlnd

I think the error goes away now, because I was missing php71w-mysqlnd

web226’s picture

Thanks for the tip AndraeRay

I'm using AMPPS on Windows 10 and adding the mysql path to the User path environment variables worked

i.e. D:\Ampps\mysql\bin