Hello,

I'm new to drupal and doing my first install (version 7.9).

During the database setup I get an error and cannot continue:

"Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)."

On the server I am using, the MySQL socket is not located in the default folder that /etc/php.ini and /etc/my.cnf point to.

Instead, each account creates their own indivdual socket that includes their account number in the file name. For example:

/tmp/account95643_mysql.sock
/tmp/account44798_mysql.sock
/tmp/account44542_mysql.sock
etc..

I need to find a way to direct the drupal installation files to the correct location. I don't have permission to edit php.ini, or my.cnf and it wouldn't matter anyway because as I explained above, each account has an individually named/located socket.

Most of the 'hacks' to bypass this involve editing php.ini, or creating a redirect in the folder where the mysql socket should be located. However, I need to be able to do it from the drupal installer itself, something like:

"localhost:/tmp/account44798_mysql.sock"

Hopefully someone can help me figure this out!

Comments

John_B’s picture

did you look at the comment text in settings.php?

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

Ken Ficara’s picture

I hope the original poster was able to solve this problem, but if so, it wasn't just by reading the comments in settings.php. Given the situation as described, the way to solve this is by adding pdo options to the database configuration. This is mentioned in settings.php, but you have to do a fair amount of work to figure out how to make the necessary change.

The settings file mentions the DatabaseConnection_mysql::__construct API call. If you read the code there, you'll see you need to change the unix_socket connection option. Based on the example shown in settings.php, you would therefore create your database configuration like so:

 $databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'DBNAME',
      'username' => 'DBUSER',
      'password' => 'DBPASS',
      'host' => '127.0.0.1',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
       'pdo' => array(
          'unix_socket' => '/PATH/TO/SOCKET',
          ),
    ),
  ),
);

NOTE: If you are not in the extremely restrictive situation of the original poster, you do NOT have to do this. PDO will use your default connection settings from my.cnf or from your php.ini settings if (and only if) you set your host to 127.0.01, not localhost. The latter will cause it to use whatever default value was hardcoded into PHP when it was compiled. This, of course, matters only if your DB is on your local host.

The same applies if you need to change other MySQL connection settings, such as port.

Anonymous’s picture

This solution worked for me on Drupal 7, with two small changes: 'unix_socket' is not nested in a 'pdo' array, & I used 'localhost' rather than '127.0.0.1' (since this is supposed to be using UNIX sockets rather than TCP/IP):

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'DBNAME',
      'username' => 'DBUSER',
      'password' => 'DBPASS',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
      'unix_socket' => '/PATH/TO/SOCKET',
    ),
  ),
);

But see my comment below for the more global solution I found.

seanr’s picture

Works the same in D8, FYI. Thanks for posting this. :-)

slydevil’s picture

This worked for me as well. Thanks for posting this.

gramie’s picture

I know that this doesn't address your problem, but it may help someone else who gets to this page looking for a Drupal 6 solution (like I did).

In D6 there is no array for the DB connection, just a single line of text.

$db_url = 'mysql://username:password@hostname/dbname';

There's a trick to this: the path to the socket replaces the hostname, but it has to be urlencoded, so that database.mysql.inc can parse it without getting confused by too many slashes.

$mysqlSocket = urlencode(':/tmp/account44798_mysql.sock');
$db_url = 'mysql://username:password@' . $mysqlSocket . '/dbname';

Maybe you just need to lose the "localhost" at the front of your string?

Anonymous’s picture

Drupal 7 is using PDO rather than mysql/mysqli, so changing the following in php.ini made it work for me:

pdo_mysql.default_socket = /path/to/mysql.sock

For Drupal 6, I believe it uses mysqli, so the following line in php.ini may work (untested):

mysqli.default_socket = /path/to/mysql.sock