4

Possible Duplicate:
mysql_connect (localhost / 127.0.0.1) on Windows platform

I just updated my Apache and PHP versions on a development machine and PDO has totally crapped out.

This is the dead simple PDO class:

class PDO_DBH {
    public static function openSesame() {
        echo '<p>start openSesame: </p>'.microtime(true);
        $db_username = 'root';
        $db_password = 'pass';
        try {
            $dbh = new PDO('mysql:host=localhost;dbname=DB_NAME', $db_username, $db_password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            echo '<p>end successful openSesame: </p>'.microtime(true);
            return $dbh;
        } catch (PDOException $e) {
            echo '<p>end failed openSesame: </p>'.microtime(true);
            return 'PDO database access error!';
        }
    }
}

Prior to upgrading, and currently on other dev machines, a typical page will load in a second, two at most.

Now, via the wonders of microtime(true), I am seeing each connection opening requires about a full second.

A typical page may create a new $dbh and then $dbh = null; it 20 times as different objects and methods go about retrieving the data they need. This has never been a problem on other machines or on my webhost.

Where have I gone wrong here?

Community
  • 1
  • 1
Drew
  • 6,208
  • 10
  • 45
  • 68
  • 4
    What if you connect to `127.0.0.1` not to `localhost`? I bet it should help ;-) – zerkms Oct 30 '12 at 04:00
  • And you may which to disable MySQL's [reverse DNS checks](http://developers.sugarcrm.com/wordpress/2012/01/10/howto-turn-off-mysql-reverse-dns-lookup-to-speed-up-response-times/). – El Yobo Oct 30 '12 at 04:03
  • @zerkms On Windows, yes to avoid the lookup for `localhost` which may require polling the IPv6 and v4 stacks. On Linux, `localhost` will use a local socket which is *much* faster – Phil Oct 30 '12 at 04:03
  • @Phil: my development machine is ubuntu server and I'm sure I've solved the same problem once by changing `localhost` to ip (but it was postgresql) (but I'll keep your comment in mind since now) – zerkms Oct 30 '12 at 04:05
  • 1
    @AndrewHeath rather than returning an error string, I would recommend `throw $e;`. Never discard exceptions like that. Also, what OS is this on? – Phil Oct 30 '12 at 04:09
  • Hi everyone, this is on Win7 64. @zerkms - changing to `127.0.0.1` did the trick. Please post that as an answer, and also please explain why that mattered, and I will gladly award you the checkmark and +1. – Drew Oct 30 '12 at 04:39
  • 1
    @Phil - regarding the `$e`, I simplified the code when cutting and pasting because I figured that bit wasn't relevant, I do have a handler in place in the real script. Thank you for caring. :-) – Drew Oct 30 '12 at 04:41
  • 1
    @AndrewHeath Here's why it mattered - http://stackoverflow.com/questions/11663860/mysql-connect-localhost-127-0-0-1-on-windows-platform. Here's another solution - http://www.php.net/manual/en/function.mysql-connect.php#94316 – Phil Oct 30 '12 at 04:48

1 Answers1

4

It was just a guess - but on windows machines changing to 127.0.0.1 instead of localhost does the trick.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • The problem is trying to resolve `localhost`. Windows is dumb – Phil Oct 30 '12 at 23:37
  • @Phil: but localhost is in `hosts` which should be always cached. So shouldn't it be resolved immediately? – zerkms Oct 31 '12 at 01:09
  • Yes, but on Windows it's in there twice. Once for `127.0.0.1` and once for `::1`. The problem is it tries the IPv6 one first and tries to connect via that address (on which MySQL is not listening) – Phil Oct 31 '12 at 02:33
  • Tried hosts file but no luck. However, putting in **127.0.0.1** instead of **localhost** as parameter of function **mysql_connect()** in PHP code does the trick. Can't imagine DNS resolve still has performance issue on Microsoft platform along the years. It affects its projects on dot net framework as well. – Ken Pega Mar 31 '14 at 04:25