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

file 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

mikey_p’s picture

This is the full output of the script, without the PHP tags obviously....

 <!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 settings.php file is incorrect or that we can't connect to the MySQL database server. This could mean your hosting provider's database server is down.</p>
<p>The MySQL error was: <em>Can&#039;t connect to local MySQL server through socket &#039;/var/mysql/mysql.sock&#039; (2)</em>.</p>
<p>Currently, the username is <em>root</em> and the database server is <em>localhost</em>.</p>
<ul>
  <li>Are you sure you have the correct username and password?</li>
  <li>Are you sure that you have typed the correct hostname?</li>
  <li>Are you sure that the database server is running?</li>
</ul>
<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>
<!-- end content -->
</body></html> 
Vidarls’s picture

I 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

php -c /path/to/php.ini /path/to/drush.php

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:

<?php 
phpinfo();
?>

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.

mikey_p’s picture

Status: Active » Fixed

http://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.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

GothGargoyle’s picture

Version: 5.x-1.0-rc3 »
Status: Closed (fixed) » Active

Having the same issue with 6.x-1.x-dev

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="" lang="" dir="ltr">
  <head>
    <title>Site off-line | Drupal</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="/misc/favicon.ico" type="image/x-icon" />
    <link type="text/css" rel="stylesheet" media="all" href="/modules/system/defaults.css?0" />
<link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.css?0" />
<link type="text/css" rel="stylesheet" media="all" href="/modules/system/system-menus.css?0" />
<link type="text/css" rel="stylesheet" media="all" href="/modules/system/maintenance.css?0" />
<link type="text/css" rel="stylesheet" media="all" href="/themes/garland/style.css?0" />
<link type="text/css" rel="stylesheet" media="all" href="/themes/garland/minnelli/minnelli.css?0" />
<link type="text/css" rel="stylesheet" media="print" href="/themes/garland/print.css?0" />
        <!--[if lt IE 7]>
      <link type="text/css" rel="stylesheet" media="all" href="/themes/garland/fix-ie.css" />    <![endif]-->
  </head>
  <body>

<!-- Layout -->
  <div id="header-region" class="clear-block"></div>

    <div id="wrapper">
    <div id="container" class="clear-block">

      <div id="header">
        <div id="logo-floater">
        <h1><a href="/" title="Drupal"><img src="/themes/garland/minnelli/logo.png" alt="Drupal" id="logo" /><span>Drupal</span></a></h1>        </div>

      </div> <!-- /header -->

      
      <div id="center"><div id="squeeze"><div class="right-corner"><div class="left-corner">
          <h2>Site off-line</h2>                              <div class="clear-block">
            <p>The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.</p><hr /><p><small>If you are the maintainer of this site, please check your database settings in the settings.php file and ensure that your hosting provider's database server is running. For more help, see the <a href="http://drupal.org/node/258">handbook</a>, or contact your hosting provider.</small></p><p><small>The <em>mysql</em> error was: <em>Can&#039;t connect to local MySQL server through socket &#039;/var/lib/mysql/mysql.sock&#039; (2)</em>.</small></p>          </div>
          <div id="footer"></div>
      </div></div></div></div> <!-- /.left-corner, /.right-corner, /#squeeze, /#center -->

      
    </div> <!-- /container -->
  </div>
<!-- /layout -->

  </body>
</html>

mysql is running fine for my site.

GothGargoyle’s picture

Status: Active » Closed (fixed)

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

blb’s picture

Version: » 5.x-1.3
Component: Code » Documentation

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

christefano’s picture

Status: Closed (fixed) » Active
moshe weitzman’s picture

Status: Active » Closed (fixed)

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

christefano’s picture

Status: Closed (fixed) » Active

I think the issue now is about how to properly document this.

niklp’s picture

Potentially 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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

... stuff...

      <div id="center"><div id="squeeze"><div class="right-corner"><div class="left-corner">
          <h2>Site off-line</h2>                              <div class="clear-block">
            <p>The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.</p><hr /><p><small>If you are the maintainer of this site, please check your database settings in the settings.php file and ensure that your hosting provider's database server is running. For more help, see the <a href="http://drupal.org/node/258">handbook</a>, or contact your hosting provider.</small></p><p><small>The <em>mysqli</em> error was: <em>Unable to use the MySQLi database because the MySQLi extension for PHP is not installed. Check your php.ini to see how you can enable it.</em>.</small></p>          </div>
          <div id="footer"></div>
      </div></div></div></div> <!-- /.left-corner, /.right-corner, /#squeeze, /#center -->


    </div> <!-- /container -->
  </div>
<!-- /layout -->

  </body>
</html>

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.

moshe weitzman’s picture

Status: Active » Closed (won't fix)

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

niklp’s picture

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

HotDrupal.com’s picture

Many 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

akahn’s picture

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

socialnicheguru’s picture

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

sime’s picture

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

Leech’s picture

Replacing 'localhost' by '127.0.0.1' in my settings.php file worked.

shrop’s picture

Replacing '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.

shrop’s picture

ok, 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

tuongaz’s picture

Perfect!

AdrianB’s picture

@ moshe weitzman #12

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.

It not just about configuring their command line properly, as this thread shows. The explanation from HotDrupal.com in #14 is very good:

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

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?).

MadtownLems’s picture

I 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 :)

kentr’s picture

@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.1 eliminated 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.php fixed everything.

AdrianB’s picture

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

sunnydeveloper’s picture

I am not connecting to localhost but rather another server on the network and I am getting this error.

kentr’s picture

I 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

geerlingguy’s picture

#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!

decibel.places’s picture

I 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

bspellmeyer’s picture

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

neotohin’s picture

+1 .. This worked for me .

estherswhite’s picture

I was having problems with Drush connecting to my database (although the site was running fine). #6 fixed it!
Thanks

xmacinfo’s picture

@bspellmeyer: Thanks for the tip. I would never I found that solution.

bisonbleu’s picture

YESSSSS!

Many thanks for the tip! Works like a charm. I feel the blood drushing to my head... :-)

Larry Jones’s picture

++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

dynamicdan’s picture

+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)?

agileadam’s picture

+1 - Worked great for me :)

agileadam’s picture

+1, Thanks! (Sorry for the double-post)
The solution in #30 worked for me!

mewren’s picture

Thanks #6 - yes changing my settings file db host to 127.0.0.1 did the trick

pratik60’s picture

This worked for me

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

Internet’s picture

Issue summary: View changes

#40 worked for me (on OSX MAMP). Thank you.

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

pribeh’s picture

@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).

Sarah10’s picture

$export PATH="$HOME/.composer/vendor/bin:$PATH"

This worked for me !!

Thanks.

kubrt’s picture

Had 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>

drupalastic’s picture

Following 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