Drush not connecting to database

mikey_p - February 16, 2008 - 19:48
Project:Drush
Version:5.x-1.3
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:won't fix
Description

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?

#1

mikey_p - February 16, 2008 - 19:51

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

<?php
<!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</code> 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>
?>

#2

Vidar Løvbrekke... - February 20, 2008 - 22:41

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.

#3

mikey_p - February 21, 2008 - 05:43
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.

#4

Anonymous (not verified) - March 6, 2008 - 05:52
Status:fixed» closed

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

#5

GothGargoyle - June 10, 2008 - 09:01
Version:5.x-1.0-rc3»
Status:closed» 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.

#6

GothGargoyle - June 10, 2008 - 16:01
Status:active» closed

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.

#7

blb - July 1, 2008 - 01:43
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.

#8

christefano - July 3, 2008 - 20:51
Status:closed» active

#9

moshe weitzman - July 24, 2008 - 14:12
Status:active» closed

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.

#10

christefano - July 28, 2008 - 22:06
Status:closed» active

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

#11

NikLP - July 31, 2008 - 15:10

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.

#12

moshe weitzman - July 31, 2008 - 16:01
Status:active» 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.

#13

NikLP - July 31, 2008 - 18:49

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.

#14

HotDrupal.com - August 5, 2008 - 02:38

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

#15

akahn - September 26, 2008 - 16:04

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.

#16

SocialNicheGuru - February 26, 2009 - 23:50

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.

#17

sime - April 22, 2009 - 23:32

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.

#18

Leech - August 14, 2009 - 22:05

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

#19

shrop - August 16, 2009 - 02:17

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.

#20

shrop - August 16, 2009 - 02:26

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

#21

tuongaz - August 24, 2009 - 00:01

Perfect!

#22

AdrianB - August 30, 2009 - 11:47

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

#23

MadtownLems - October 2, 2009 - 18:12

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

#24

kentr - November 11, 2009 - 20:04

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

#25

AdrianB - November 11, 2009 - 21:57

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.

 
 

Drupal is a registered trademark of Dries Buytaert.