How to force MySQL to connect by TCP instead of a Unix socket?

In Linux and other *nixes, MySQL will assume you want to use a socket if you connect to the host "localhost" (which would be the default hostname).

You can override this in 3 ways: 1) Specify a different hostname like 127.0.0.1 (mysql -h 127.0.0.1) or your server's real hostname 2) Specify that you want to use TCP and not a socket (mysql --protocol tcp)

You can also easily make that the default my editing your my.cnf so it has this ([client] means any client:

[client]
protocol=tcp

You can see the full description of how MySQL decides how to connect here:

http://dev.mysql.com/doc/refman/5.5/en/connecting.html


Use an IP-binding to 127.0.0.1. That should activate a listening port on localhost. On the client side do not use localhost - use 127.0.0.1 instead. Many clients have an internal alias that makes them connect to the socket if you specify localhost as target.

MySQL is strange.


Isn't this really a client issue ? If using the mysql program You can use the --protocol switch. From the man page

 --protocol={TCP|SOCKET|PIPE|MEMORY}

       The connection protocol to use for connecting to the server. It is
       useful when the other connection parameters normally would cause a
       protocol to be used other than the one you want. For details on the
       allowable values, see Section 4.2.2, “Connecting to the MySQL
       Server”.

I just tried

mysql --protocol=TCP -u root -p

whilst monitoring port 3306 with tcpdump -i lo tcp port 3306 and I can see traffic whereas if I just run

mysql  -u root -p

I (correctly) see no traffic on port 3306.

EDIT:

Now that you tell us you are using DRUPAL, the solution is relatively easy.

Go to sites/<sitename> or sites/default and edit the settings.php file

You will find a structure like this

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'databasename',
      'username' => 'databaseuser',
      'password' => 'databasepassword',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

Change the 'localhost' to '127.0.0.1' and save the file.


This may sound a little crazy

Try setting the socket file to an absolute path whose path resides on another machine

http://dev.mysql.com/doc/refman/5.0/en/server-options.html#option_mysqld_socket

Otherwise, you cannot bypass this default behavior because a socket file must exist for mysqld to communicate with.