|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42977 localhost setting goes to socket instead of
Submitted: 2007-10-15 17:14 UTC Modified: 2014-04-16 18:17 UTC
From: marc at perkel dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.4 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: marc at perkel dot com
New email:
PHP Version: OS:


 [2007-10-15 17:14 UTC] marc at perkel dot com
When a PHP application tries to call MySQL and it is set to talk to localhost it talks only to the socket and not to TCP In my.cnt I set the client as follows:


However that is ignored.

I'm running a web server hosting some 100 applications installed by many different people. So changing all the localhost setting to isn't practical. What I want to do is not use the socket at /var/lib/mysql/mysql.sock at all. The reason is that I'm trying to mugrate all mysql to a dedicated mysql server and having the web server talk to and link them with an SSH tunnel. In this setup there will be no socket.

I left a bug report at and they blame the problem on PHP so i'm now here to let you know about it.

Reproduce code:
Set configuration to localhost and disable socket and PHP can't talk to TCP

Expected result:
I expect PHP to ignore the unix socket and talk to TCP

Actual result:
No access


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-17 09:29 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

If you want to read settings from the my.cnf you have to use MySQLi and provide the option MYSQLI_READ_DEFAULT_GROUP, ext/mysql is legacy code. (see also )
 [2014-04-16 16:58 UTC] rcmova at gmail dot com
I've been playing around with 'mod_ruid2' and found the same problem since socket is not reachable from the jail.

PHP module will try to connect to mysql (unreachable) socket even if the specified host is 'localhost' ignoring actual 'my.cnf' config (since it's not even tried to be read at this moment).

Problem will not be reproduced if you use '' as connection host since a TCP connection is used instead.

IMHO, that's a smart (socket should be faster) but misleading beahaviour.

I've been reading PHP source and found the logic here:

An easy PoC to make a 'localhost' TCP connection is to modify that line of code to 'strncasecmp()' against, for example, ''.
This way module will use a TCP connection since it won't consider 'localhost' as "use socket" mandatory.

I don't know if there are some other implications in this issue that justifies this behaviour in the module, but my ideal workflow for it would be something like this:

* If 'localhost' is defined as connection host:
  * Check wether 'my.cnf' defines a socket or not
  * Check wether defined socket is reacheable or not and use it if it is
  * Fallback to if socket is not reacheable since that's the EXPLICITLY defined connection host

I hope to read some thoughts about this issue at least, since it was reported back in 2007 and it's still a big issue for many people around.
 [2014-04-16 18:17 UTC]
This is something known for ages and documented well for the mysql commando. So PHP just reflects what one already expects, no misleading at all. Please read more about this here .
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Mar 24 03:03:41 2023 UTC