Bug #21138 mysql_real_connect() failing when connecting to remote hosts on Apache-2.2/PHP-5
Submitted: 19 Jul 2006 8:53 Modified: 28 Sep 2006 12:59
Reporter: Mo McRoberts Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.0.22 OS:Solaris (Solaris/sparc)
Assigned to: CPU Architecture:Any

[19 Jul 2006 8:53] Mo McRoberts
Description:
I'm running Apache 2.2.2 (worker thread MPM) with a thread-safe PHP 5.1.4 linked against a thread-safe shared MySQL 5.0.22 client library on Solaris 9/sparc.

Connections to remote hosts succeed from the command-line (both the MySQL command-line client and from PHP's command-line SAPI, the latter of which is built to be thread-safe).

In contrast, MySQL client connections initiated from PHP running on the web server consistently fail immediately with mysql_error() returning: Can't connect to MySQL server on 'SERVERNAME' (0)

On Mac OS X, the same versions of MySQL, PHP and Apache (configured identically) the problem does not occur.

The issue also manifested itself with MySQL 5.0.21, however I'm unsure if it has been around longer.

How to repeat:
Compile Apache, PHP and MySQL as I did on Solaris/sparc and attempt a mysql_connect() call from PHP to a remote host.

Suggested fix:
My only suggestion is that it may be related to the non-blocking connect implementation interacting badly with Solaris' POSIX threads implementation, though I'm not sure how to proceed with proving or disproving this.
[19 Jul 2006 8:54] Mo McRoberts
Configuration options used for PHP, Apache and MySQL

Attachment: configuration-options.txt (text/plain), 2.95 KiB.

[19 Jul 2006 8:55] Mo McRoberts
Output of ‘ldd’ on the MySQL client binary, the PHP module and the Apache httpd binary

Attachment: ldd-output.txt (text/plain), 4.92 KiB.

[19 Jul 2006 10:01] Valeriy Kravchuk
Thank you for a problem report. Please, send the exact code of demo PHP application/web page that demonstrates the behaviour described. Please, send also the results of:

SELECT user, host FROM mysql.user;
[19 Jul 2006 10:12] Mo McRoberts
Test case:

<?php
$link = mysql_connect('server.domainname.com', 'username', 'password', true);
echo mysql_error();
?>

Running this from the command-line succeeds. Running this from within Apache fails. Local database connections (hostname is empty or 'localhost') succeeds. It doesn't matter what authentication details are supplied in the failure case, or indeed which server (I have several to choose from, running various different versions of MySQL)—a connection is never established so no authentication ever happens.

Running ‘mysql -uusername -ppassword -hserver.domainname.com’ on the same server also succeeds.

If you really want the output from ‘mysql.user’, I can paste it, though it should be irrelevant (and I'd just be picking one server at random).
[28 Aug 2006 12:59] Valeriy Kravchuk
Please, try to repeat with a newer version, 5.0.24. If you'll be able to repeat the same behaviour described, plese, send a list of exact steps you had performed to configure and build PHP 5.1.4 with MySQL support.
[28 Sep 2006 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".