php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52434 mysqlnd: host cannot be "localhost" when connecting to tcp port
Submitted: 2010-07-25 04:42 UTC Modified: 2010-09-17 04:15 UTC
From: anthon dot pang at gmail dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.3.3 OS: Ubuntu 10.04
Private report: No CVE-ID: None
 [2010-07-25 04:42 UTC] anthon dot pang at gmail dot com
Description:
------------
With PDO_MYSQL, if PHP is linked with mysql client libraries (instead of mysqlnd), the DSN can contain a host parameter with a hostname, e.g., host=localhost.

However, with mysqlnd, the host has to be an ip address, e.g., host=127.0.0.1.  This occurs for '--with-mysqli=mysqlnd' or '--with-pdo-mysql=mysqlnd'.  It appears mysqlnd wants to use a Unix socket even though the port is explictly specified,

This backward incompatibility surprises users migrating from php 5.2.x and find their apps suddenly can't connect.

MySQL 5.1.41

Test script:
---------------
From Zend Framework 1.10.6:

        $_isConnected = @mysqli_real_connect(
            $this->_connection,
            $this->_config['host'],
            $this->_config['username'],
            $this->_config['password'],
            $this->_config['dbname'],
            $port
        );

Expected result:
----------------
Expect it to connect.


Actual result:
--------------
Warning: PDO::__construct() [pdo.--construct]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /var/www/libs/Zend/Db/Adapter/Pdo/Abstract.php on line 129
SQLSTATE[HY000] [2002] No such file or directory

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-25 06:31 UTC] dtajchreber@php.net
-Status: Open +Status: Bogus
 [2010-07-25 06:31 UTC] dtajchreber@php.net
Tell PHP where your mysql.sock file is via the DSN or pdo_mysql.default_socket in 
the php.ini file and your error will go away. Your conclusion is way off. 

http://www.php.net/manual/en/ref.pdo-mysql.connection.php
 [2010-07-25 09:05 UTC] anthon dot pang at gmail dot com
-Summary: mysqlnd: host cannot be a hostname +Summary: mysqlnd: host cannot be "localhost" when connecting to tcp port
 [2010-07-25 09:05 UTC] anthon dot pang at gmail dot com
Seriously?

http://www.php.net/manual/en/ref.pdo-mysql.connection.php gives two examples:

mysql:host=localhost;port=3307;dbname=testdb
mysql:unix_socket=/tmp/mysql.sock;dbname=testdb

And my DSN (constructed by Zend Framework) follows the first example.  Where does it say I have to define the unix socket in php.ini if I intend to connect using the tcp port?

Here's the fix:

--- mysqlnd.c.bak       2010-07-25 02:47:29.000000000 -0400
+++ mysqlnd.c   2010-07-25 02:49:37.000000000 -0400
@@ -635,7 +635,7 @@
                char * transport = NULL;
                int transport_len;
 #ifndef PHP_WIN32
-               if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
+               if (!port && host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
                        DBG_INF_FMT("socket=%s", socket? socket:"n/a");
                        if (!socket) {
                                socket = "/tmp/mysql.sock";
 [2010-07-25 18:33 UTC] johannes@php.net
The patch won't be added as then "localhost" has two meanings - one time meaning "use the unixdomain socket in the default location" and one time actually "resolve localhost and use TCP" as it is taught in many places that "localhost" in MySQL has the first meaning we keep it there.
 [2010-07-25 19:00 UTC] anthon dot pang at gmail dot com
Ok.  I'll open a Documentation bug for this compat buster.
 [2010-09-16 23:01 UTC] neweracracker at gmail dot com
Hello.

I am able to reproduce this issue with:

Windows 7 build 7600 (Ultimate Edition) x86
PHP Version 5.3.3 MSVC6
mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $ 
MySQL 5.1.50

Steps to reproduce:
1. Download phpmyadmin
2. Extract on html/www/public_html (or similar)
3. Try to connect to mysql (example: login)
4. It won't work, stop php
5. Edit config.inc.php and change localhost to 127.0.0.1
6. No more errors. I guess this needs a fix
 [2010-09-16 23:30 UTC] pajoye@php.net
There are dozen of reports about that on Windows and it is unrelated to this problem.

A work around for the windows issue will be present in 5.3.4, in the meantime you can disable ::1 in your windows' host file.
 [2010-09-17 03:50 UTC] neweracracker at gmail dot com
# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

Its disabled :)
Still no result hmm.

I blame Microsoft for this. lol
 [2010-09-17 04:15 UTC] anthon dot pang at gmail dot com
This doesn't have anything to do with the hosts file or DNS resolution.

mysqlnd (rightly or wrongly) specially interprets "localhost" to mean a local 
socket, to mimic some of the behavior of MySQL command-line utilities.
 [2010-09-17 04:59 UTC] neweracracker at gmail dot com
I agree with anthon.

Someone has to fix this :)
 [2012-10-10 18:21 UTC] register at bazanov dot net
Athon!
Thank you very much for bringing this BUG out!
I killed 1-2 hours of my time trying to understand why I can't connect to the server.
Then I found this bug report and changed localhost to 127.0.0.1 and it worked!

This is one of the most annoying and frustrating bugs ever. It must be fixed!

For search engines, here is the error I got:
Warning: mysqli_real_connect() ... [2002] ... connection refused. (trying to connect via tcp://localhost:3306)
Warning: mysqli_real_connect() ... (HY000/2002) ... connection refused. (trying to connect via tcp://localhost:3306)
 [2013-03-14 12:34 UTC] 45net at rambler dot ru
Confirmed. PHP 5.3.22 compiled with mysqlnd enabled both on mysql and mysqli ( and even on pdo). 127.0.0.1 works, localhost doesn't.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 00:01:32 2024 UTC