SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

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

Using fresh install of Drush drush-6.x-3.3.tar.gz and removed prior version

the update command retrieves the db connect settings etc

also using jailshell, PHP Version 5.2.13, MySQL 5.0.87, site is running D 6.16

I replaces some sensitive info with 'user'

user@decibelplaces.com [~/public_html/netsperience]# drush update
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)  [warning]

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    : 6.16
  Site URI          : http://default
  Database driver   : mysqli
  Database hostname : localhost
  Database username : correct username
  Database name     : correct db name
  Default theme     : garland
  Administration theme: garland
  PHP configuration : /usr/local/lib/php.ini
  Drush version     : 3.3
  Drush configuration:
  Drupal root       : /home/user/public_html/netsperience
  Site path         : sites/default
  Modules path      : sites/all/modules
  Themes path       : sites/all/themes
  %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.

Command pm-update needs a higher bootstrap level to run - you will   [error]
need invoke drush from a more functional Drupal environment to run
this command.
Command pm-update needs the following modules installed/enabled to   [error]
run: update.
The drush command 'update' could not be executed.                    [error]

Comments

decibel.places’s picture

Status: Active » Closed (duplicate)

change 'localhost' to '127.0.0.1' in $db_url in settings.php

#703454: Drush Bootstrap Problem Using Localhost in Settings.php file

I looked for a drush or mysql config solution so I would not have to change the settings.php for each site, didn't find one but I'm sure it exists

sanguis’s picture

just wanted to say taht this worked for me as well

josedazagz’s picture

Confirmed. Solution from decibel.places did work for me too. Thank you very much.

amirtaiar’s picture

Not working for me. Have the samae issue

sanguis’s picture

amirtaiar: did you try the solution found in #1

aerozeppelin’s picture

Thank you, this worked for me!

anewday’s picture

Changed the following in settings.php

$db_url = 'mysql://username:password@127.0.0.1/databasename';

It still doesn't work.

decibel.places’s picture

@ anewday

I presume you put in the actual values for the databasename, user and password (?)

maybe try mysqli?

$db_url = 'mysqli://username:password@127.0.0.1/databasename';

also the db connection for D7 is an array, not a string; this thread pertains to Drupal 5.x and 6.x

bsnodgrass’s picture

In my case I did not get any errors similar to "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [warning]" and My Drush version is 4.5 which evidently still has issues with localhost vs. 127.0.0.1

However I am on a shared server for this dev site...

my ssh session to update my dev site:

# drush status
 Drupal version         :  6.20                                  
 Site URI               :  http://default                        
 Database driver        :  mysqli                                
 Database hostname      :  localhost                             
 Database username      :  USERNAME                       
 Database name          :  net2comm_promisdv                     
 Default theme          :  garland                               
 Administration theme   :  garland                               
 PHP configuration      :  /usr/local/lib/php.ini                
 Drush version          :  4.5                                   
 Drush configuration    :                                        
 Drush alias files      :                                        
 Drupal root            :  /home/net2comm/domains/dev6/promisdev 
 Site path              :  sites/default                         

# drush up
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.
Command pm-update needs the following modules installed/enabled to run: update.                                    [error]
The drush command 'up' could not be executed.                                                                      [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    : 6.20
  Site URI          : http://default
  Database driver   : mysqli
  Database hostname : localhost
  Database username : USERNAME
  Database name     : net2comm_promisdv
  Default theme     : garland
  Administration theme: garland
  PHP configuration : /usr/local/lib/php.ini
  Drush version     : 4.5
  Drush configuration: 
  Drush alias files : 
  Drupal root       : /home/net2comm/domains/dev6/promisdev
  Site path         : sites/default
  Modules path      : sites/all/modules
  Themes path       : sites/all/themes
  %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.

I found another post referring to the first direction drush provided above "Command pm-update needs the following modules installed/enabled to run: update. [error]"

# drush en update
Command pm-enable needs a higher bootstrap level to run - you will need invoke drush from a more functional Drupal [error]
environment to run this command.
The drush command 'en update' could not be executed.                                                               [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    : 6.20
  Site URI          : http://default
  Database driver   : mysqli
  Database hostname : localhost
  Database username : USERNAME
  Database name     : net2comm_promisdv
  Default theme     : garland
  Administration theme: garland
  PHP configuration : /usr/local/lib/php.ini
  Drush version     : 4.5
  Drush configuration: 
  Drush alias files : 
  Drupal root       : /home/net2comm/domains/dev6/promisdev
  Site path         : sites/default
  Modules path      : sites/all/modules
  Themes path       : sites/all/themes
  %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.

Hmmm took a look at a site where drush was working properly:

# drush status
 Drupal version         :  6.20                                     
 Site URI               :  http://default                           
 Database driver        :  mysqli                                   
 Database hostname      :  127.0.0.1                                
 Database username      :  USERNAME                         
 Database name          :  net2comm_n2cnet                          
 Database               :  Connected                                
 Drupal bootstrap       :  Successful                               
 Drupal user            :  Anonymous                                
 Default theme          :  rubik                                    
 Administration theme   :  rubik                                    
 PHP configuration      :  /usr/local/lib/php.ini                   
 Drush version          :  4.5                                      
 Drush configuration    :                                           
 Drush alias files      :                                           
 Drupal root            :  /home/net2comm/domains/net2community.net 
 Site path              :  sites/default                            
 File directory path    :  sites/default/files                      

Noticed the difference localhost and 127.0.0.1 and two additional lines in the status
Database : Connected
Drupal bootstrap : Successful
I had heard of this before so I edited the settings.php database connect string to use 127.0.0.1 for the following results:


# cd ../dev6/promisdev
# drush status
 Drupal version         :  6.20                                  
 Site URI               :  http://default                        
 Database driver        :  mysqli                                
 Database hostname      :  127.0.0.1                             
 Database username      :  USERNAME                       
 Database name          :  net2comm_promisdv                     
 Database               :  Connected                             
 Drupal bootstrap       :  Successful                            
 Drupal user            :  Anonymous                             
 Default theme          :  comesa_regional                       
 Administration theme   :  garland                               
 PHP configuration      :  /usr/local/lib/php.ini                
 Drush version          :  4.5                                   
 Drush configuration    :                                        
 Drush alias files      :                                        
 Drupal root            :  /home/net2comm/domains/dev6/promisdev 
 Site path              :  sites/default                         
 File directory path    :  sites/default/files                   

net2comm@net2community.com [~/domains/dev6/promisdev]# drush en update
update is already enabled.                                                                                         [ok]
There were no extensions that could be enabled.                                                                    [ok]
net2comm@net2community.com [~/domains/dev6/promisdev]# drush up
Update information last refreshed: Tue, 20/09/2011 - 04:33

Update status information on all installed and enabled Drupal projects:
 Name                              Installed version  Proposed version  Status                                   
 Admin                             6.x-2.0            6.x-2.0           Up to date                               
 Admin role                        6.x-1.3            6.x-1.3           Up to date                               
 Advanced help                     6.x-1.2            6.x-1.2           Up to date                               
 Ajax Load                         6.x-1.3            6.x-1.4           Update available                         
 Apply for role                    6.x-1.9            6.x-1.9           Up to date                               
 Backup and Migrate                6.x-2.4            6.x-2.4           Up to date                               
 Drupal core                       6.20               6.22

Life is good...

Thanks folks hope this helps someone.

bserem’s picture

In my shared host, with Jailshell, drush stops working whenever I add a php.ini file in ~/drush.

Scenario and steps:
I install drush in ~/drush
I copy drush.ini from examples and change line 69 to: disable_functions = ''
Everything is fine, but I get out of memory errors. Changing the memory limit in drush.ini doesn't affect anything.

I add a php.ini with memory_limit = 128M inside it.
Drush stops working, it is now unable to bootstrap!

I can reproduce this on all of my shared servers!

henrikakselsen’s picture

#1 worked for me also. Thx.

mar111’s picture

Status: Closed (duplicate) » Active

I have this issue with :

7.x-5.0 ---- 2012-Mar-23

All-versions-5.x-dev ------ 2012-Apr-01

I tried to change localhost by number but does not work.

But it is working perfect with drush 4.5

7.x-4.5 ----- 2011-Aug-12

cainrus’s picture

I have same issue with:

  • drush/drush-5.1.0
  • drush/drush-5.0.0
  • drush/drush-4.5.0

These all pear version.

Comment #1 did not helped

jonhattan’s picture

Status: Active » Fixed

If changing localhost to 127.0.0.1 won't fix for you this is probably other problem. Open a new issue with all relevant details.

Status: Fixed » Closed (fixed)

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

trangunghoa’s picture

Thank you bsnodgrass.
I have solved the problem.

lgomezma’s picture

Version: » 7.x-1.x-dev

In case you have a multi-site installation and you have multiple settings.php files, just go to the site directory where the right settings.php is and run drush there.

matthewv789’s picture

Thank you lgomezma, #17 worked for me. Navigated to /sites/[sitename.local]/ where my relevant settings.php was, and was able to run drush from there. (I don't really have multiple sites, just a local settings file so I don't have to modify the default settings.php for the site pulled from git.)

pal4life’s picture

Can some one point to this issue discussion for Drupal 7?

Thanks

decibel.places’s picture

Set the path to drush in your drush path-aliases as explained in example.aliases.drushrc.php

Relative aliases are always taken from the Drupal root.
* '%drush-script': The path to the 'drush' script, or to 'drush.php' or
* 'drush.bat', as desired. This is used by backend invoke when drush
* runs a drush command. The default is 'drush' on remote machines, or
* the full path to drush.php on the local machine.
* '%drush': A read-only property: points to the folder that the drush script
* is stored in.

I'm not sure whether both %drush and %drush-script are necessary, but since %drush-script defaults to "drush" or "drush.php" you need to specify it as "drush.bat" on a Windows environment.

my example with absolute (not relative) paths:

'path-aliases' => array(
'%drush' => '/home/decibelp/bin/drush',
'%drush-script' => '/home/decibelp/bin/drush/drush',
... more path-aliases: files etc.
),

hawkeye126’s picture

I cannot find

$db_url

in /sites/default/settings.php or anywhere in the drupal or drush install folders.
I am running drupal 7.

What am I missing or doing wrong?

Please help, my site works fine but drush cannot access it:

[prompt]# drush pm-enable bootstrap
Command pm-enable needs a higher bootstrap level to run - you will need to invoke drush from a more functional Drupal environment to run this command. [error]
The drush command 'pm-enable bootstrap' 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.28
Site URI : http://default
PHP executable : /usr/bin/php
PHP configuration : /etc/php.ini
PHP OS : Linux
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :
Drupal root : {REMOVED}
Site path : sites/default

When I try to connect to mysql:

[prompt]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[prompt]# drush status
PHP executable : /usr/bin/php
PHP configuration : /etc/php.ini
PHP OS : Linux
Drush version : 7.0-dev
Drush temp directory : /tmp
Drush configuration :
Drush alias files :

I am on a linux vps with 1and1.
Thank you!

decibel.places’s picture

@hawkeye126

In D7 the database connections are defined by an array, instead of the $db_url string that was used in D6

There's a very good Guided Tour of Drupal's settings.php for Drupal 7 by OSTraining
I recommend that you check it out, it might answer questions you don't know you have yet ;)

The pertinent part is changing the default setting to 'host' => '127.0.0.1'

$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'databasename',
  'username' => 'username',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => '',
);

If you continue to have issues with D7, please try to find an issue about this for D7, or start one yourself if you cannot find any appropriate one.
This is an old thread about D6.

raulmuroc’s picture

It didn't work because mysql.exe was not there in the system PATH. So I added H:\xampp\mysql\bin to PATH and then it worked! (in Mac Os X usually in /usr/local/mysql/bin)

sparker888’s picture

I struggled with this issue for a long time, and through reading many articles and forums, a lot of blood, sweat and tears, I finally figured out this solution:

NOTE: I am on Win 8.1, but most of these instructions came from various Win 7 installs and they still worked.

My Dev Environment: For the stack I'm using Dev Desktop from Acquia (I do NOT recommend DD2), Git for Windows (git bash), and Drush for Windows

There are several resources for installing Dev Desktop, Git, and Drush together, namely: http://friendlydrupal.com/screencasts/windows-tools-git-drush-acquia-dev... and I also found this helpful (but note differences below): http://wiki.drupalwxt.org/architecture/getting-started-win/

This last article also references this one which is very helpful when installing Drush for Windows: https://www.drupal.org/node/1843176

1. You MUST update your Environmental Variables User $PATH to include: C:\Program Files\acquia-drupal\mysql\bin;

2 You MUST also include several drush paths in the same User $PATH IF the drush installer didn't already do this for you (it usually does, but there are some reported problems on certain Windows installations)

Here is the exact code I use (including the one mentioned in 1. above):

C:\ProgramData\Drush\;C:Program Files (x86)\Drush\GnuWin32\bin;C:\Program Files (x86)\Drush\Php;C:\Program Files (x86)\Drush\cwRsync\bin;C:\Program Files\acquia-drupal\mysql\bin;

3. You MUST add a new entry in your hosts file (C:\Windows\System32\drivers\etc\hosts): 127.0.0.1 yoursitename

Note: If you have a site name such as yoursitename.localhost from when you configured/named your site in Dev Desktop, it DOES NOT MATTER. Keep the basic site name as shown above.

4. You MUST create a local drush alias for the site using the following format:

<?php
/**
 * Local alias
 * Set the root and site_path values to point to your local site
 */
$aliases['yoursitename'] = array(
  'uri'  => 'http://yoursitename:8082',
  'root' => 'C:/Dev_Server/Dev_Sites/yoursitename',
  'db-url' => 'mysql://yourdatabaseusername:password@127.0.0.1:3306/yoursitename',
    'databases' =>
      array (
        'default' =>
        array (
          'default' =>
          array (
            'driver' => 'mysql',
            'username' => 'yourdatabaseusername',
            'password' => 'yourpassword',
            'port' => '',
            'host' => '127.0.0.1',
            'database' => 'yourdatabasename',
          ),
       ),
    ),
);

NOTE: In the URI, if your site is named mysite.localhost, change this to mysite.localhost here, but NOT in db-url

NOTE: If you are using root with no password in the database (not a good practice btw), you can leave out the password

5. Be sure and follow the naming convention for naming aliases: yoursitename.aliases.drushrc.php (typically you put the alias files in the ~/.drush/ folder, but there are other options that you can read about).

6. Assuming you're using sites/all/yoursitename, make sure and change default.settings.php to settings.php (no need to modify this one)

7. Open Git Bash (or close and re-start it)

8. Test to see that drush can read your aliases using:

drush sa

(you should see @yoursitename and perhaps others in a list)

9. Next, test your alias is working using:

drush @yoursitename status

(you should see a bunch of info related to your site with no errors)

If that works, you can test to make sure you're hooked up to the database by using:

drush @yoursite cc

You should get a list of 10 caches you can clear. Just choose one (like 1) and hit Enter. If that works, you should be in business

10. Navigate to your site directory and perform an update:

drush up

If all works correctly, drush will take you through the update process [success], and you have successfully connected to your database and updated your site through drush! xD

11. If you get an error related to missing the update module, you need to enable the core Update Manager module on your site.

If you have any problems between 8-11, try:

drush cc drush

This clears the drush cache and you can try the drush commands again. If that fails, try quitting and re-starting your git bash shell as well. If you are still receiving errors at this point, you need to go back and check to make sure everything is perfectly coded. The smallest thing (especially path settings or incorrect database paths, site name or credentials in the drush alias file) will break it.

Also, I renamed my folder in C:\Program Files (x86)\acquia-drupal\drush to 'old-drush' so Dev Desktops internal drush wouldn't interfere. I tried many things to make this drush work, and never succeeded, so I ditched it.

I'll try to answer any questions and update this procedure as needed. If there is something that I'm missing, doing wrong, or a better fix, please let me (and the community) know. However after weeks of trial and error and meticulous documentation, I believe this should be accurate. Good Luck!

bhupendra_kanojiya’s picture

Thanks RaulMuroc.... #23 worked for drupal 7.

macmladen’s picture

Note to myself and (probably) other Mac users out there: when you install mysql from Oracle/Sun/MySQL from regular .DMG file, you will end up with temporary path to mysql being in path but upon restart path will be lost.

So you have to make symbolic link to binaries, most importantly, mysql mysqldump.

Upon installation, do the following in terminal:

cd /usr/local/bin
sudo ln -s /usr/local/mysql/bin/mysql
sudo ln -s /usr/local/mysql/bin/mysqldump

If you need other commands (and there are many useful there in /usr/local/mysql/bin/) link them too. This links shouldn't break even upon mysql upgrade.

Does anyone think MariaDB and brew works better?

Juanpgs’s picture

#23 , it works!

gdev2018’s picture

to #24 by sparky888.

Hi! Add to your collection:

12. Edit Sqlpgsql.php (...\composer-root\vendor\drush\drush\lib\Drush\Sql\Sqlpgsql.php )

line 9:
//public $query_extra = "--no-align --field-separator='\t' --pset tuples_only=on";
public $query_extra = "--no-align --pset tuples_only=on";

line 45:
//$environment = "PGPASSFILE={$pw_file} ";
$environment = "SET PGPASSFILE={$pw_file} & ";

(Windows 7, drush 8.1-dev)

Good luck!

dblais’s picture

I'm inside a CPANEL/WHM environement.

You can also check if you have the file ".my.cnf" inside your home directory. If yes, mysql will use the user/password inside this file event if Drush use the good one. Good to know!

Try to rename or delete your ".my.cnf".

ltrain’s picture

Just wanted to add that for MAMP users, these are the symbolic links you need to add:

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

Thanks all!

speedyqueenie’s picture

Did #1 - no dice. Keeping the 127.0.0.1 setting, I tried #30 and it worked!

knalstaaf’s picture

I was getting this back:

 * 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.

#30 solved it - cheers!

MarkBGH’s picture

Just piling on to say that #30 was the ticket!

W.M.’s picture

What solved a similar issue for me under Windows was to add the \mysql\bin directory to PATH

zorphdark’s picture

#34 solved adding `mysql/bin` path to Windows PATH. thanks :)

ezkhimo’s picture

I'm under WAMP, Drupal 8 / Drush 8 and the error occured when trying to drush runserver an SQLite-based installation. The path-related comments gave me the obvious hint that I had to add the path to my sqlite3 executable to Windows PATH, accordingly. It worked after that. Thanks!

sozonov’s picture

#30 solved the problem! Thanks!

frazac’s picture

and this solved for xampp ;)
thanks @itrane with #30

cd /usr/local/bin
sudo ln -s /Applications/XAMPP/xamppfiles/bin/mysql
sudo ln -s /Applications/XAMPP/xamppfiles/bin/mysqldump
dendrmx’s picture

#26 Works great for me.
Thank's MacMladen!

maitak’s picture

yosia_ken’s picture

#23 works for me, thanks!!

ronit29’s picture

#30 works perfectly for me with XAMPP on Ubuntu

cd /usr/local/bin
sudo ln -s /opt/lampp/bin/mysql
sudo ln -s /opt/lampp/bin/mysqldump

omrmankar’s picture

#1 works for me thanks in Drush Version: 8.1.16

drupalbabaji’s picture

#23 worked for me

serialmother’s picture

#1 works for me. Thank you Decibel.places!

ashique12009’s picture

#23 worked for me, thanks.

kunal_singh’s picture

#23 worked for me, Thank you.

bisw’s picture

#23 worked for me. Thank you very much.

santosh8943’s picture

#1 worked for me. Thank you Decibel.places!
Just changed localhost to 127.0.1 in file located at sites/default/settings.php

jweirather’s picture

In my case I was running a production environment with a separate database server. Drush required mysql-client to be installed on the same server it's running on, so I had to install the mysql-client from the command line: sudo apt install mariadb-client in my case. Noting that your command and specific client may vary depending on your setup and whether you're using mysql or mariadb.

leolopre’s picture

after executing the command yum install mysql drush works fine

timwhelanatl’s picture

I found this to work for me with MAMP,
sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/local/bin/mysql

skatheeth’s picture

#23 worked for me! Thank you very much!

loopy1492’s picture

I was attempting to spin up an old d7 site locally with the prod db and got this error. As it turns out, the last time modules and core was updated on production, updatedb hadn't been run. After getting the correct site uri set in local.drushrc.php, running `drush updatedb` cleared this up.