php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45468 Using mysqlnd defaults to using unix socket instead of localhost:port
Submitted: 2008-07-09 13:51 UTC Modified: 2009-08-27 10:35 UTC
Votes:15
Avg. Score:4.5 ± 0.8
Reproduced:15 of 15 (100.0%)
Same Version:8 (53.3%)
Same OS:10 (66.7%)
From: michael dot kofler at gmx dot com Assigned: johannes (profile)
Status: Closed Package: MySQL related
PHP Version: 5.3CVS, 6CVS (2009-04-25) OS: * (not win32)
Private report: No CVE-ID: None
 [2008-07-09 13:51 UTC] michael dot kofler at gmx dot com
Description:
------------
neither mysql nor mysqli work at all

the problem is caused by mysqlnd (if I compile without mysqlnd, it works)

testing environment:

1) Linux with MySQL 5.1.25 and PHP 5.3 snapshot compiled with 
   --with-mysql=mysqlnd --with-mysqli=mysqlnd

2) Windows Vista with MySQL 5.1.25 and PHP 5.3 snapshot

in both cases, phpinfo() shows mysql, mysqli and mysqlnd information; mysql and mysqli use mysqlnd

Reproduce code:
---------------
$mysqli = new mysqli("localhost", "root", "***", "mydatabase");

Expected result:
----------------
a valid mysqli object (code works in Windows with PHP 5.2.6 and in Linux when PHP is compiled without mysqlnd)

Actual result:
--------------
Windows: blank page, no error at all

Linux: 

Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No such file or directory ...



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-14 18:27 UTC] uw@php.net
Please make use of http://de2.php.net/manual/en/mysqli.connect-errno.php and http://de2.php.net/manual/en/mysqli.connect-error.php

The linux message smells like you have to specify a socket or a port. Looks like an attempt is made to connect to a default socket but the unix domain socket file is not accessible.

 [2008-07-14 18:27 UTC] uw@php.net
... make use of the functions and report back what error you get.
 [2008-07-15 07:36 UTC] michael dot kofler at gmx dot com
Linux: you were right, it's a socket problem

--with-mysqli=mysqlnd looks for the socket file at /tmp/mysql.sock

is there a ./configure option or another way to set the default socket file? I set a link from /tmp/mysql.sock to /var/run/mysqld/mysqld.sock, but that's a hack and no solution

Windows: no error message is shown; the browser waits for ever for an answer but does not get one; the Apache log file also shows no errors

PS: if mysqlnd is to be used by default in PHP 5.3, it really should be *completely* backward compatible (as advertised); right now, this is not the case; while the socket file problem can probably fixed with the right configuration, mysqlnd apparently requires the new MySQL authentication (introduced in MySQL 4.1); some Linux distributions still ship my.conf with old_passwords=1 to get maximum compatibility to old client libraries (I know, that's a bad idea ...); for this reason, PHP 5.3 will not work out of the box in many, many cases ...

PPS: I am now offline till July 22nd, so I can give no more feedback till then
 [2008-07-21 18:23 UTC] andrey@php.net
The old authentication method is insecure and should not be used. Also MySQL servers before 4.1 are already antique. If you have an application that uses them you might don't want to change you PHP, and you still have the possibility to compile PHP with libmysql. mysqlnd will never support the old authentication. This saves hassles when checking what is possible and what not. 4.0 and earlier are too old.
I just committed a fix, so it will be possible to configure PHP with --with-mysql-sock to use another socket. However, the problem _wasn't_ in mysqlnd but in the extensions that sit on it. If you use libmysql it will look by default for a socket under /tmp/mysql.sock . That's the default. During server compilation you can set different default value which will appear then in mysql_version.h that comes with libmysql and what is used by the binary. mysqlnd uses /tmp/mysql.sock and there is no way to change it on it's level, but with the existing option it will be on the extension level.
 [2008-07-23 11:37 UTC] uw@php.net
From the begin on mysqlnd has been described as MySQL 4.1+. We won't make it work with any MySQL <4.1. See also the MySQL Lifecycle Policy.

Those distributions that use old_passwords=1 have two choices: use a different configure for PHP and use libmysql like ever since or take the challenge of an upgrade. PHP 4 will become history at some point in time and so will MySQL 4.1.

If you are using MySQL < 4.1 and it works with libmysql all is fine. mysqlnd is does not support MySQL <4.1 nor does it support old_passwords=1: no bug.
 [2008-08-07 08:01 UTC] michael dot kofler at gmx dot com
re-tested on Linux with alpha1, compiled with this configuration:

configure --with-mysqli=mysqlnd \
    --with-mysql=mysqlnd \
    --with-mysql-sock=/var/run/mysqld/mysqld.sock \
    --enable-pdo \
    --with-pdo-mysql=mysqlnd \
    --with-apxs2=/usr/bin/apxs2 \
    --with-zlib \
    --with-gd \
    --with-config-file-scan-dir=/etc/php5/apache2 \
    --with-jpeg-dir=/usr/lib \
    --enable-exif \
    --libdir=/usr/lib \
    --enable-mbstring  

also tried

    --with-mysql-sock=/var/run/mysqld \

result: mysql, mysqli and PDO/mysql, all using mysqlnd, still look for the socket file /tmp/mysql.sock and ignore --with-mysql-sock

if I compile without mysqlnd and without the --with-mysql-sock option, PHP automatically finds the right socket file, probably because libmysql reads the [client] settings in /etc/mysql/my.cnf

my solution for now: I changed all socket options in /etc/mysql/my.cnf and /etc/mysql/debian.cnf

it would be better either to provide a working configure option for PHP (--mysqlnd-sock=...) or to evaluate my.cnf within mysqlnd

------

PS: as to mysqlnd not supporting old MySQL authentication: perfectly fine for me, but *do document* it in some PHP 5.3 update advisory; otherwise I am pretty sure the update to PHP 5.3 will cause trouble for many MySQL users
 [2009-02-03 11:43 UTC] johannes@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

--with-mysql-sock should work now properly
 [2009-02-03 11:59 UTC] andrey@php.net
 Hi,
it was a problem, the Unix path, in the extensions, not mysqlnd. Yes, mysqlnd uses /tmp/mysql.sock, but actually there are no configure options for mysqlnd. --with-mysql-sock is actually an option of ext/mysql . It wasn't used in the past, as far as I recall, but current 5_3 and HEAD do use it to set default value for the socket path, which can be overwritten by the user. Both for ext/mysql and mysqli.
This if from ext/mysql/php_mysql.c :
#ifdef MYSQL_UNIX_ADDR
	STD_PHP_INI_ENTRY("mysql.default_socket",		MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty,	default_socket,	zend_mysql_globals,		mysql_globals)
#else
	STD_PHP_INI_ENTRY("mysql.default_socket",		NULL,	PHP_INI_ALL,		OnUpdateStringUnempty,	default_socket,	zend_mysql_globals,		mysql_globals)
#endif


This is from ext/mysqli/mysql.c :
#ifdef PHP_MYSQL_UNIX_SOCK_ADDR
	STD_PHP_INI_ENTRY("mysqli.default_socket",			MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty,	default_socket,	zend_mysqli_globals,		mysqli_globals)
#else
	STD_PHP_INI_ENTRY("mysqli.default_socket",			NULL,	PHP_INI_ALL,		OnUpdateStringUnempty,	default_socket,	zend_mysqli_globals,		mysqli_globals)
#endif

MYSQL_UNIX_ADDR is a macro, for PHP_MYSQL_UNIX_ADDR, which is defined by the configure script if --with-mysql-sock is used.

In this regard, PDO doesn't use --with-mysql-sock. PDO_MYSQL used mysql_config to find the socket, but for mysqln defaults to /tmp/mysql.sock , which seems like bug, because of inconsistency.
This is something for Johannes

Best,
Andrey
 [2009-04-26 15:27 UTC] jani@php.net
This is clearly a bug:

# sapi/cli/php -n -r 'mysql_connect("localhost:3306");'

Output with --with-mysql=mysqlnd:

Warning: mysql_connect(): [2002] No such file or directory (trying to 
connect via unix:///tmp/mysql.sock) in Command line code on line 1

# sapi/cli/php -n -r 'mysql_connect("localhost:3306");'

No output (error) --with-mysql (without mysqlnd) -> connection works.

The problem is with code in ext/mysqlnd/mysqlnd.c:537-543 which forces 
using the socket in this case.

Note: Same happens with mysqli. This bug also makes all mysql(i) tests  
fail unless one uses some environment variables while running them.

 [2009-08-27 10:26 UTC] andrey@php.net
Jani,
I think you are wrong :
andrey@winnie:/work/vanilla/php/php-src/branches/PHP_5_3$ ./php -r '$c=mysql_connect("localhost:3307");var_dump($c, $res=mysql_query("select 42", $c), mysql_fetch_assoc($res));'

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in Command line code on line 1

Warning: mysql_query() expects parameter 2 to be resource, boolean given in Command line code on line 1

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in Command line code on line 1
bool(false)
NULL
NULL
---------------------------
My MySQL runs on 3307 and the socket it uses is /tmp/mysql.sock. I have a /etc/my.cnf, that specifies /var/run/mysqld/mysqld.sock, and the client tries to use it. So you see, if you specify localhost this means - use unix socket, no matter if there is a port or not. mysqlnd does the same. Same is valid if you try mysql --host localhost --port=3307 . I suppose in your case you had mysql running with socket which you was where expected.

 [2009-08-27 10:35 UTC] andrey@php.net
Closing this, as not a bug anymore
 [2010-05-06 12:25 UTC] wolfgang_weber at gmx dot at
I don't know why this ticket has been closed as I am still affected from this bug.

PHP: 5.3.2 with mysqlnd and php-fpm patch from svn
MySQL: 5.1.46
OS: Centos 5.4 x86_64

Configure options:

--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql \

Tried to add a zone in ZoneAdmin, and this is a part from my error log:

[06-May-2010 12:08:39] PHP Warning:  mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /var/www/tools/zoneadmin/includes/config.php on line 28

So why do you think that this is not a bug anymore?

Kind regards
Wolfgang Weber
 [2010-05-06 12:33 UTC] wolfgang_weber at gmx dot at
Sorry for double posting, but I'd like to add a quote:

"The default location for the Unix socket file that the server uses for communication with local clients is /tmp/mysql.sock. (For some distribution formats, the directory might be different, such as /var/lib/mysql for RPMs.)"

From: http://dev.mysql.com/doc/refman/5.1/en/problems-with-mysql-sock.html

Regards
Wolfgang
 [2010-11-30 11:44 UTC] me dot deepa dot selvam at hotmail dot com
PHP Warning:  mysql_connect(): [2002] A connection attempt failed because the connected party did not  (trying to connect via tcp://localhost:3306) in C:\inetpub\wwwroot\test.php on line 3

I got this error on running my php file:test.php

<?php
echo "Got it............test.php";
$con=mysql_connect("localhost","root","pa@onicz");
if(!$con)
	die('Could not connect: '.mysql_error());
echo "Connection Success";
?>

I am working on windows 7 + IIS 7 + php 5.3.3 + MySQL 5.1.53

Someone please help me...
 [2011-09-19 22:30 UTC] php at mike2k dot com
Still an issue - even with mysqli.default_socket and mysql.default_socket set, 
it's still looking for /tmp/mysql.sock

It's not defined as that anywhere, phpinfo() shows that I am pointing to 
/var/lib/mysql/mysql.sock as well.

I don't want to do a symlink hack. I am running PHP 5.3.8 under FPM.

'./configure' '--prefix=/opt/php53' '--with-config-file-path=/opt/php53/etc' '--
with-config-file-scan-dir=/opt/php53/etc/conf.d' '--enable-fpm' '--with-
libdir=lib64' '--enable-cli' '--enable-inline-optimization' '--disable-rpath' '-
-disable-ipv6' '--with-gettext' '--with-readline' '--enable-mbstring' '--enable-
mbregex' '--enable-sqlite-utf8' '--with-gettext' '--enable-intl' '--with-
mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-curl' 
'--with-zlib' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-xpm-dir' '-
-with-freetype-dir' '--enable-gd-native-ttf' '--enable-exif=shared' '--enable-
shmop' '--with-xsl=shared' '--enable-soap=shared' '--with-mssql=shared' '--
enable-sockets' '--enable-pcntl=shared' '--with-mcrypt' '--with-bz2' '--with-
tidy=shared' '--with-pcre-dir' '--with-openssl' '--with-ldap=shared' '--with-
imap=shared' '--with-imap-ssl' '--with-kerberos' '--with-pear' '--with-gmp' '--
with-pdo-dblib' '--with-xmlrpc'
 [2011-09-19 23:18 UTC] php at mike2k dot com
okay - I just recompiled, and perhaps I didn't fully restart it last time. It 
*seems* to work now.

./configure \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
...

checking for MySQL support... yes
checking for specified location of the MySQL UNIX socket... 
/var/lib/mysql/mysql.sock
checking for MySQL UNIX socket location... /var/lib/mysql/mysql.sock
checking for MySQLi support... yes
...
checking for MySQL support for PDO... yes

it seems to work now. I think I forgot to restart FPM when I tried last. It was 
unclear in this bug report though what the fix seemed to be or when it happened 
:p
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Feb 17 17:01:25 2019 UTC