I finally got Drush up on my new MAC with MAMP running. After running drush status I got....


SQLSTATE[HY000] [2002] Can't connect to local MySQL server through [warning]
socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)
Drupal version : 6.16
Site URI : http://default
Database driver : mysqli
Database hostname : localhost
Database username : root
Database name : riku
Default theme : garland
Administration theme : garland
PHP configuration : /Applications/MAMP/conf/php5/php.ini
Drush version : All-versions-3.0
Drush configuration :
Drupal root : /Applications/MAMP/htdocs/riku
Site path : sites/default
Jeff-Williamss-iMac:riku HomeFolder$ dl views
-bash: dl: command not found
Jeff-Williamss-iMac:riku HomeFolder$ ccc

any help would be most apreciated

Comments

greg.1.anderson’s picture

Status: Active » Fixed

Use "drush dl views" and "drush cc all", for example. If you want to use just the drush command, first go into cli mode via "drush cli". From inside the drush shell you will be able to use just "dl views" and "cc all".

puregin’s picture

Status: Fixed » Active

I am seeing the same error message.

I believe this is due to a 'non-standard' location for our mysql socket, /var/lib/mysql/mysql.sock' rather than the one indicated in the error message.

From my brief reading of the PDO documentation, this should be picked up by PDO reading the mysql configuration, but this evidently is not happening.

There doesn't seem to be any 'nice' way to supply this information via configuration files, so I added it to the database connection string in includes/environment.inc as a work-around. After this, everything works fine.

I'm not sure where/how this should be fixed...

greg.1.anderson’s picture

What change did you make to environment.inc?

puregin’s picture

This is purely a hack, but it worked for me

949c949
<     $constr = sprintf("%s:dbname=%s;host=%s;unix_socket=/var/lib/mysql/mysql.sock;", $type, $creds['name'], $creds['host']);
---
>     $constr = sprintf("%s:dbname=%s;host=%s", $type, $creds['name'], $creds['host']);
vb’s picture

The issue is following.
If you have installed anorther development LAMP package like LAMPP or I see here MAMP.
The drush takes by default its parameters php.ini and unix_socket

For lampp

vb@vb:~/www/omegasocial.net$ drush status

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket [warning]
'/opt/lampp/var/mysql/mysql.sock' (2)
+----------------------+----+------------------------------+
| Drupal version | : | 6.16 |
| Site URI | : | http://default |
| Database driver | : | mysqli
| PHP configuration | : | /opt/lampp/etc/php.ini

For MAMP see the output above
PHP configuration : /Applications/MAMP/conf/php5/php.ini

The services for lampp mysql and apache2 are not started at all. lampp is simple untared and does not have installation procedure that changes something.
On the ubuntu 10.04 native apache2 with php 5.3.2 and mysql are started and running.
But drush does not take their parametres. it takes the first ones that it finds in paths.
And I want that drush is using them but not the lampp's php 5.2.9 and mysql when lampp is not started.

How these parameters can be configured in drush?

greg.1.anderson’s picture

I didn't really understand the question in #5. If the change in #4 is generally useful, we could add the ability to set $option['mysql-socket'] = '/var/lib/mysql/mysql.sock' in your drushrc.php file.

vb’s picture

I am using php 5.3.2 for development of 5.3 apps and lampp-1.7.1 with php 5.2.9 to develop 5.2 apps.
Drush is using the first php.ini it found /opt/lampp/etc/php.ini but should use /etc/php5/cli/php.ini or something configurable.
Download lampp http://www.apachefriends.org/en/xampp-linux.html, simple untar it in /opt, do not start it.
Run the drush to your last site as usual.
See the difference.
#6 would be nice to fix the mysql socket issue. Make so with php.ini $option['php-ini'] = /etc/php5/cli/php.ini

greg.1.anderson’s picture

Regarding your suggestion in #7, I really don't think that's possible; php loads php.ini before drush gains control, and I doubt that you can reload your own. That would be a security violation.

It would be possible in theory to add a --php-ini flag to the invocation of php at the end of the drush script, but I'm not really sure where you'd pull the right option from. To me, it seems much easier to just put the correct configuration options in the php.ini file that happens to be in use on your system (/opt/lampp/etc/php.ini).

vb’s picture

I have found the place where it happens.
Please look at drush script line 37
It would be convenient to have the options to skip this test.

greg.1.anderson’s picture

Status: Active » Fixed

How are we supposed to know which php to run on your system? That script does the special checking for *AMP because php is usually newer in the *AMP installations. Now my basic answer to your request is that the "parameter" you should set is $PATH. Unfortunately, that doesn't work because of line 37, which was put in as a service for users who do not set their $PATH. I'm not sure that this was necessarily the best decision, but I'm reluctant to change that now, because I think doing that would break a lot of people.

Adding a flag to the drush script doesn't seem like a good option.

I would recommend that you just make an alias:

alias drush='/path/to/my/php/php /path/to/my/drush/drush --php="/path/to/my/php/php"'

Untested, but you get the general idea.

greg.1.anderson’s picture

Title: Can't connect to local MySQL server through socket » Add an option in drushrc.php to select the mysql socket to use
Status: Fixed » Active

Wait, this is still active per #4.

moshe weitzman’s picture

Status: Active » Closed (duplicate)

dupe of #437642: support for different MYSQL socket. i really don't see that is is drush's responsibility to allow configuration of the db connection like socket. core drupal does not allow that to be configured afaik. you gotta use the config that php provides.

s4j4n’s picture

If drush should be using the php.config file, why is it not using the mysql socket provided in it? Can we have it take advantage of that information?

I think this would be useful for example when running: drush sql-dump

I definitely like the idea of using the config that PHP provides.

The php.ini file that is used by php in MAMP /Applications/MAMP/conf/php5.2/php.ini shows this on line 620:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock

I have MAMP installed. I'm using the symbolic link approach to get drush to work with my MAMP installation by using the php in MAMP.

If I run php --ini I receive this:

Configuration File (php.ini) Path: /Applications/MAMP/conf/php5.2
Loaded Configuration File:         /Applications/MAMP/conf/php5.2/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

All has been good (commands like drush cron, drush status, or drush cc work just fine for me) until recently I tried to run drush sql-dump and I'm getting a 2002 error:

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect

If I run sql-dump in debug mode one of the lines you see are:

Calling system(mysqldump -v --single-transaction --opt -Q -hlocalhost -umeetu -pmypassword meetu )

So looks like drush is using the mysqldump in my $PATH (i.e. /sw/bin/mysqldump) instead of MAMP's version at /Applications/MAMP/Library/bin/mysqldump

It's also not using the specified socket listed in php.ini.

Can we have drush use the socket listed in php.ini (assuming there are no compatibility problems with using different mysqldumps)?

craigmc’s picture

Sadly, hack in #4 is all that is working for me thus far in Ubuntu 10.04 headless server running on Zend CSE.

Subscribing.

hansrossel’s picture

I had the same issue when I started using a normal php/mysql setup on ubuntu instead of xampp: drush status kept on pointing to /opt/lampp/php/php.ini and socket could not connect.

Reason was that I had not installed php-cli:
sudo apt-get install php5-cli
This solved all problems.

So this issue might be a duplicate of http://drupal.org/node/738038#comment-2721536, that issue happened when I removed the xampp installation so drush had no more wrong php to point at.

Bill Choy’s picture

Component: Installation » PM (dl, en, up ...)

My work-around was to create a soft link from where Drush expected the socket, to where my MAMP's "Start page" said it was.

Here the step-by-step (sometimes I need everything spelled out):
1. start your MAMP

2. click on "Open start page" button.

3. copy the socket location (in my case: "/Applications/MAMP/tmp/mysql/mysql.sock")

4. open a UNIX terminal and confirm
"ls -al /Applications/MAMP/tmp/mysql/mysql.sock"
=> srwxrwxrwx 1 williamchoy admin 0 Oct 11 11:39 /Applications/MAMP/tmp/mysql/mysql.sock
(the "s" is for socket).

5. try a drush command (drush pm-list) and get the expected location from the error message and confirm that it does not exist (step 4).
=> In my case it was "/tmp/mysql.sock".

6. create the soft link
"ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock"

7. Test again... and buy a friend a beer, because it is time to party with DRUSH.

joehudson’s picture

I just commented out the lines:

for amp_php in /Applications/MAMP/bin/php5/bin/php /Applications/MAMP/bin/php5.2/bin/php /
Applications/MAMP/bin/php5.3/bin/php /opt/lampp/bin/php /Applications/xampp/xamppfiles/bin/php /
Applications/acquia-drupal/php/bin/php; do
  if [ -x $amp_php ]; then
    php=$amp_php
  fi
done

in the drush script and it all behaved as expected for my standard lamp setup.

joehudson’s picture

candy13’s picture

A quick solution, inspired by #4, is to open "advanced options" on the web page and set the host to (in my case) localhost;unix_socket=/tmp/mysql.sock

(ooh, maybe not for drush - sorry, new, & setting up drupal for the first time)

staze’s picture

This would be extremely useful, as this is still broken and requires hacking environment.inc to get things to work right. I'm not sure why this doesn't work. My setup has the mysql.default_socket set correctly (/var/mysql/mysql.sock), but drush continues to look for it at /tmp/mysql.sock.

lakshminp’s picture

point the DRUSH_PHP to the php you want to use.

$ export DRUSH_PHP=/path/to/your/php

jrabeemer’s picture

#21 worked for me!

crossfish’s picture

#21 did not work for me.

scotts-imac:mwfprod scott$ php --ini
Configuration File (php.ini) Path: /Applications/MAMP/conf/php5.3
Loaded Configuration File: /Applications/MAMP/conf/php5.3/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)

Tried this:

scotts-imac:mwfprod scott$ export DRUSH_PHP=/Applications/MAMP/bin/php5.3/bin/php
scotts-imac:mwfprod scott$ drush sql-dump --file=mwfprod.sql
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/opt/local/var/run/mysqld/mysqld.sock' (2) when trying to connect

drush commands on my system seem to work okay for the most part, but the sql-dump croaks for sure in using mysqldump. In #16 above, line 5. seems to indicate an error should occur, but my pm-list comes out nicely, so I didn't try to setup the soft link to '/tmp/mysql.sock'.

I did this:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /opt/local/var/run/mysqld/mysqld.sock

and got a good execution. However, I may have broken something else in the process and I'll have to report that later if/when I come across it.