php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42977 localhost setting goes to socket instead of 127.0.0.1
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marc at perkel dot com
New email:
PHP Version: OS:

 

 [2007-10-15 17:14 UTC] marc at perkel dot com
Description:
------------
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 127.0.0.1. In my.cnt I set the client as follows:

[client]
protocol=TCP

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 127.0.0.1 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 127.0.0.1 and link them with an SSH tunnel. In this setup there will be no socket.

I left a bug report at mysql.com 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 127.0.0.1


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

Actual result:
--------------
No access

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

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 http://bugs.mysql.com/bug.php?id=31577 )
 [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 '127.0.0.1' 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:

https://github.com/php/php-src/blob/master/ext/mysqlnd/mysqlnd.c#L920

An easy PoC to make a 'localhost' TCP connection is to modify that line of code to 'strncasecmp()' against, for example, 'local.host'.
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 127.0.0.1 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] ab@php.net
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 http://dev.mysql.com/doc/refman/5.1/en/connecting.html .
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu May 19 19:04:05 2022 UTC