An installation has been failing. It cant connect to the mysql socket.

My installation of mysql has a socket file in an unusual place:

/var/run/mysqld/mysqld.sock

while drupal wants to connect to a socket to

/tmp/mysql.sock

I could "fix" my mysql settings, but I fear I could break other client connections.

I would rather redirect drupal to the active socket

How do I do this?

Comments

sunil-1’s picture

The error message I get when I try to run drupal:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)in /usr/local/www/drupal-test/includes/database.mysql.inc on line 12
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)

This, despite the fact that real socket location is set in

/etc/my.cnf

As a mysql client, shouldn't drupal be able to retrieve the
socket file location value from my.conf?

sellam’s picture

I have the same problem. After a frustrating hop around the Drupal site fruitlessly trying to find a solution, I decided to fix it myself.

I added a symbolic link to my actual mysql.sock file in the path where Drupal was trying to find it. It seems the default location is in /tmp. So I put a symbolic link at /tmp/mysql.sock that points to my actual mysql.sock file:

lrwxrwxrwx 1 root root 25 Dec 27 10:42 mysql.sock -> /var/lib/mysql/mysql.sock

To create a symbolic link, issue the following command:

ln -s [your actual mysql.sock path and filename] /tmp/mysql.sock

In your case, you should issue (as user root) the command:

ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

This should fix your problem.

My first fix was to try to hack the Drupal database code but it was more involved than I liked and the changes would have had to be moved to any upgrades of the system. I wish/hope the Drupal guys will put in a feature to allow an alternate socket file to be specified for those unfortunate of us with "non-standard" installations.

jeoffw’s picture

I had the same problem. I'm working on a hosted box (otherwise I wouldn't be using PHP :) j/k), so I can't be running around making symlinks wherever I want, now can I? This change seems to work so far:

In the file includes/database.mysql.inc change the db_connect function as follows:

function db_connect($url) {
  $url = parse_url($url);

  // Allow for non-standard MySQL port.
  if (isset($url['port'])) {
     $url['host'] = $url['host'] .':'. $url['port'];
  }

  /** Added to allow socket file in path; last path element becomes DB name *****/
  $p = explode('/', $url['path'] );
  $plen = count($p);
  if ($plen > 1) {
    $url['path'] = '/' . array_pop($p);
    $url['host'] = $url['host'] .':'. join('/', $p);
  }
  /***********************************/

  $connection = mysql_connect($url['host'], $url['user'], $url['pass'], TRUE) or die(mysql_error());
  mysql_select_db(substr($url['path'], 1)) or die('unable to select database');

  return $connection;
}

Then in your /sites/default/settings.php file you use a DB URL string like mysql://username:password@localhost:/path/to/socket/dbname. (The last element of the path is the database name).

In my case the socket is in my user home folder, so I did this:

$mysql_socket = $_ENV["HOME"] . "/mysql/socket";
$db_url = 'mysql://username:password@localhost' . $mysql_socket . '/drupal';
$db_prefix = '';

Misc. Dept.:
My hosting provider places the socket info in ~/.my.cnf but for some reason PHP doesn't pick it up... I have no idea if this is a Drupal problem or a problem w/ the way the host configured PHP...

ksuquix’s picture

Don't know if this is still an issue, but I found this in a search when I was trying to do the same thing before I figured it out:

$db_url = 'mysql://user:password@localhost%3Aport%3A%2Fpath%2Fto%2Fsocket%2Ffile/database';

No code changes or system changes necessary.

Why it works: parse_url returns this string as the host part:
localhost%3Aport%3A%2Fpath%2Fto%2Fsocket%2Ffile

Then it's immediately run through urldecode to get:
localhost:port:/path/to/socket/file

Which is the proper format for mysql_connect to parse.

jrwjrw’s picture

Ksuquix - thanks for figuring this out bc it was exactly what I needed. Well... almost exactly. I didn't need the 'localhost:port' part in the mysql_connect call - it wouldn't work with it in there actually. So here is what I use to connect using the socket /tmp/mysql5.sock:

$db_url = 'mysql://username:password@%3A%2Ftmp%2Fmysql5.sock/database';

thanks

buglady’s picture

I solved the problem without changing code or doing anything else unusual by editing the php.ini file for my server and changing

mysql.defautl_socket =

to

mysql.default_socket = /full/path/tol/mysql.sock

Then restart the apache server and just use localhost as the host and nothing in the port number.

badzilla’s picture

That is definitely the easiest approach but you also need to change the mysqli socket filename too. Full instructions on my openSUSE 11.2 Downgrade PHP 5.3 blog
http://www.badzilla.co.uk/openSUSE-11.2-Downgrade-PHP-5.3-to-5.2