Drush does not seem to be connecting to the database. I'm actually getting HTML on the command line.
This is what it looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Unable to connect to database server</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="all">@import "/misc/maintenance.css";</style>
<style type="text/css" media="all">@import "/modules/system/defaults.css";</style>
<style type="text/css" media="all">@import "/modules/system/system.css";</style>
<link rel="shortcut icon" href="/misc/favicon.ico" type="image/x-icon" />
</head><body><h1>Unable to connect to database server</h1>
<!-- begin content -->
<p>If you still have to install Drupal, proceed to the <a href="/install.php">installation page</a>.</p>
<p>If you have already finished installing Drupal, this either means that the username and password information in your <code>settings.phpfile is incorrect or that we can't connect to the MySQL database server. This could mean your hosting provider's database server is down.
The MySQL error was: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2).
Currently, the username is root and the database server is localhost.
- Are you sure you have the correct username and password?
- Are you sure that you have typed the correct hostname?
- Are you sure that the database server is running?
For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.
I did move this site between servers, and it still seems to be working fine on the first server. I did update the sites/default/settings.php to reflect the new database, and the site seems to work fine. Not sure why Drush is doing this.
Also, I call Drush from the drupal root with ./sites/all/modules/drush/drush.php.
Is there some where that Drush is caching the database URL?
Comments
Comment #1
mikey_p commentedThis is the full output of the script, without the PHP tags obviously....
Comment #2
Vidarls commentedI assume that you have no problems connecting to the database with the regular drupal installation.
I had a similar problem, the solution was that I had to point command line PHP to the php.ini file used by the apache php to enable database support.
This is done with the -c attribute
the path to the php.ini can be found allmost at the top running the command php -i, or alternatively making a simple php page that looks like this:
This might help solve this problem, but I did not get exactly the same error you have. My error was that php claimed I had not activated any database modules.
Comment #3
mikey_p commentedhttp://www.raincitystudios.com/blogs-and-pods/steve-krueger/how-use-drus...
Just saw this, and confirms the same thing Vidar mentioned. I haven't tried it yet, but I am sure this is what the problem is. Maybe I'll try to cook up something to add to the docs for this.
Comment #4
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #5
GothGargoyle commentedHaving the same issue with 6.x-1.x-dev
mysql is running fine for my site.
Comment #6
GothGargoyle commentedFigured out the solution to this problem - I needed to update my settings.php to set the host for mysql to 127.0.0.1 instead of localhost to be able to access mysql from command line.
Comment #7
blb commentedI had this same problem with version 5.
I am running MAMP on OS X so there was more than one version of php on my machine and the one I wanted was not in my path.
The solution was to specify the MAMP version of php in the alias. the line in .bash_profile looked like this:
alias drush='/Applications/MAMP/bin/php5/bin/php ~/Documents/websites/public_html/drupal-5.7/sites/all/modules/drush/drush.php -r ~/Documents/websites/public_html/drupal-5.7'and i used the -l switch to specify the site.
Comment #8
christefano commentedComment #9
moshe weitzman commentedMAMPIf using MMAP, your socket file is not where mysql expects so that could be an issue as well. This is basically a local config problem, not drush.
Comment #10
christefano commentedI think the issue now is about how to properly document this.
Comment #11
niklp commentedPotentially not... I have a very similar problem, but with a different MySQL error. NO idea what's causing this. This is on a *nix server in a shared hosting environment, and Drush works in this exact account with a D5 setup.
I get:
... stuff...
I have no idea where this is coming from, as I've never used, attempted to use, or even typed "mysqli" before in my life, afaik.
Comment #12
moshe weitzman commentedYou don't have to type mysqli - Drupal detects that for you and uses it if available. So, you need to go to the settings.php that is in effect for this drush request and edit the line starting with $db_url and use mysql instead of mysqli
This whole thread comes down to people not configuring their command line php properly which is really easy to do. Unfortunately, fixing this out of scope for this module. It does not help that MAMP and maybe others do some non-standard stuff.
Comment #13
niklp commentedWOW. I wouldn't have thought of that in a month. I'm confused why I don't have this same problem with D5 tho - is the default connection string not the same? I have run a D5 install in exactly the same account, without this problem. Also, the rest of the site works just fine with the string like that, which seems weird...
At the *very* least, I think a mention of this deserves an entry in the README? The command line php stuff might be "really easy", but there is not a centralised place for these weird caveats, so people don't know about them. I asked half a dozen times about this on IRC with no useful response (not for want of people trying). Perhaps this is something that needs to be addressed?
Many thanks.
Comment #14
HotDrupal.com commentedMany providers give you what is known as a "jail shell"
When in this shell you can't use localhost as you don't have direct access to the MySQL socket. This is why you need to use a hostname of 127.0.0.1 so MySQL is accessed via TCP instead of trying to access via the socket.
Steve
HotDrupal - Drupal Friendly Hosting
Comment #15
akahn commentedAnother issue for me that was PHP/MySQL/MAMP related was that in setting.php, I don't have to specify the port that MySQL is running on for Drupal to work. I do have to specify it for drush to work, however. So I had to specify :8889 in my $db_url.
Comment #16
socialnicheguru commentedThanks. the localhost change to 127... did it for me.
Could this be placed in the documentation under troubleshooting? This would have saved me hours of looking online.
Comment #17
simeFor the record, #2 didn't work for me, if I reference the same php.ini in my MAMP installation my CLI php fails.
I used phpinfo() on the command line to find out where the php.ini file was. I then configured the mysql and mysqli socket config as per my MAMP php.ini.
Comment #18
Leech commentedReplacing 'localhost' by '127.0.0.1' in my settings.php file worked.
Comment #19
shrop commentedReplacing 'localhost' by '127.0.0.1' in my settings.php file also worked for me.
I am on OS X running the local apache build. On my OS X Servers I do not have the same issue with drush and the web environment. If anyone has an idea on how to have the OS X environment use localhost, I would appreciate it. I think it may have something to do with mysql treating localhost as a socket connect vs. TCP. Not sure though.
Comment #20
shrop commentedok, I finally found my issue above. To fix the OS X environment (for me at least) and avoid changing db connection strings in settings.php to 127.0.0.1, set the default socket connections for mysql in the php.ini that your CLI php is reading. Mine was in /etc/php.ini.
I found the tip here: http://drupal.org/node/383848
Comment #21
tuongaz commentedPerfect!
Comment #22
AdrianB commented@ moshe weitzman #12
It not just about configuring their command line properly, as this thread shows. The explanation from HotDrupal.com in #14 is very good:
It's not Drush problem that you can't connect to localhost in a shared hosting environment with jail shell, but it would help tremendously if these kind of errors where documented somewhere under troubleshooting for Drush.
I think a lot of those who are new to Drush are not that experienced with this and would be helped by simple lines like if you get this error, try to replace 'localhost' with '127.0.0.1' in settings.php.
Even better would be if there was some way to configure Drush to use 127.0.0.1 instead of localhost, so you don't have to mess with settings.php (maybe there already is a way?).
Comment #23
MadtownLems commentedI was really excited to finally find a fix for this, but alas, changing localhost to 127.0.0.1 did not work for me. Here's what it DID do though:
Changed the error I got from that /sock stuff to:
Access denied for user '....'@'127.0.0.1' (using password; YES)
Before I made the change from localhost, the website worked fine but CLI didn't, and after I made the change, I got this same new error from both web and cli.
For reference, changed:
$db_url = 'mysqli://my_username:my_password@localhost/my_dbname';
to
$db_url = 'mysqli://my_username:my_password@127.0.0.1/my_dbname';
Any ideas? I reallllly wanna use Drush :)
Comment #24
kentr commented@MadtownLems
Try adding 127.0.0.1 as an allowed host for your database user.
Symptoms:
I couldn't even connect with
mysql -h 127.0.0.1 ..., even though I was certain the info was correct.At first, I was getting the "can't connect using socket ..." error.
Adding
-h 127.0.0.1eliminated that error, but then I got "access denied for user..." error.Specifying 127.0.0.1 as an allowed host in addition to changing the host in
settings.phpfixed everything.Comment #25
AdrianB commentedI still think it would be a great help for many people if the module instructions included this info about localhost vs 127.0.0.1 in settings.php.
Comment #26
sunnydeveloper commentedI am not connecting to localhost but rather another server on the network and I am getting this error.
Comment #27
kentr commentedI think it boils down to this (may or may not also apply to postgresql):
PHP has multiple "interfaces" (aka, SAPI's). When using drush, you're using the CLI SAPI (Command Line Interface). Sometimes the connection settings via the CLI SAPI need to be different than that for the SAPI used for http / apache requests (which can vary according to how the server is set up).
So you need to figure out the MySQL connection settings for the CLI SAPI, and somehow set up settings.php or environment variables to let you connect.
You can debug somewhat this by using 'php -r ...' to execute PHP commands directly from the command line. Type 'php -h' for the built-in help text.
The question to answer is: "What do the connection settings need to be for CLI PHP connection to my database?" The hosting provider can probably give insight.
Another somewhat related issue is that your CLI SAPI might not even have the MySQL extension enabled - but that's another can of worms.
More info: http://php.net/manual/en/features.commandline.php
Comment #28
geerlingguy commented#14 fixed this issue for me - thanks, Steve!
(I have two separate multisite installs on Hot Drupal, and when running them via the SSH jailshell, I was hitting my head against a wall trying to troubleshoot the MySQL problem. I changed all the 'settings.php' files to use 127.0.0.1 instead of localhost, and all was well!
Comment #29
decibel.places commentedI also need to change the db url to 127.0.0.1
I looked for a long time for a way to configure that in drush or mysql so it does not have to go into every settings.php
Drush offical docs site had nothing
Comment #30
bspellmeyer commentedIf you are using MAMP make sure that your php executable (e.g. /Applications/MAMP/bin/php5.2/bin/php) actually IS executable. Setting the execute flag on this file solved this issue for me.
Comment #31
neotohin commented+1 .. This worked for me .
Comment #32
estherswhite commentedI was having problems with Drush connecting to my database (although the site was running fine). #6 fixed it!
Thanks
Comment #33
xmacinfo@bspellmeyer: Thanks for the tip. I would never I found that solution.
Comment #34
bisonbleu commentedYESSSSS!
Many thanks for the tip! Works like a charm. I feel the blood drushing to my head... :-)
Comment #35
Larry Jones commented++1
Comment #30 fixed my problem!
In particular, setting /Applications/MAMP/bin/php5.3/bin/php to executable (774)
allowed this command to succeed:
drush -d -s up
Comment #36
dynamicdan commented+1.. changed to 127.0.0.1 was the best solution.
I strongly recommend adding this to a FAQ...
Why can't I query the database?
A: Have you checked using localhost vs 127.0.0.1 ? Are you using MAMP with sockets? (--socket=/...../mysql.sock)?
Comment #37
agileadam+1 - Worked great for me :)
Comment #38
agileadam+1, Thanks! (Sorry for the double-post)
The solution in #30 worked for me!
Comment #39
mewren commentedThanks #6 - yes changing my settings file db host to 127.0.0.1 did the trick
Comment #40
pratik60 commentedThis worked for me
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
Comment #41
Internet commented#40 worked for me (on OSX MAMP). Thank you.
Comment #42
protools commented1) 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
Comment #43
pribeh commented@kentr Thanks! For whatever reason the mysql php extension was not installed for CLI on my server and I didn't realize to look until I read your comment (#27).
Comment #44
Sarah10 commented$export PATH="$HOME/.composer/vendor/bin:$PATH"
This worked for me !!
Thanks.
Comment #45
kubrt commentedHad the same problem with a d6 site on a machine with both php5 and php7 cli, DRUSH_PHP environment variable didn't help, ended up running drush like this:
php5 /var/www/<myroot>/sites/default/modules/drush/drush.php -u admin script <myscript>Comment #46
drupalastic commentedFollowing steps installed drush in my windows 7 PC's & laptop seamlessly. Please ignore 1st two steps if you already have a web-server-stack running in your machine.
1. Install VC11
https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E...
2. Install XAMPP-5.6-VC11
https://www.apachefriends.org/xampp-files/5.6.32/xampp-win32-5.6.32-0-VC...
3. Install GIT
http://git-scm.com/download/win
4. Install composer
https://getcomposer.org/Composer-Setup.exe
5. Install drush using compser
in git-bash type: composer global require drush/drush
6. in bash - navigate to sites folder
7. check environment by typing following commands in bash one by one
php --version
mysql --version
composer --version
drush --version
Incase any of the above commands returns error, make sure to update environment variables accordingly.
Makesure that your environment variables have these entries (depending upon your install location & user name)
C:\Users\Admin\AppData\Roaming\Composer\vendor\bin;
C:\Users\Admin\AppData\Roaming\Composer\vendor\drush\drush\;
C:\Program Files\Git\cmd;C:\ProgramData\ComposerSetup\bin;
C:\xampp\mysql\bin;
C:\xampp\php;
8. Finally in sites\defalut\settings.php change the host from localhost to 127.0.0.1
Hope this helps