XAMPP: Requests take far longer when connecting to a MySQL database

1

1

I'm developing in PHP and MySQL locally using XAMPP on Windows. The web site is fairly fast when not connecting to any database. However, when I connect to the MySQL database, a simple request now takes approximentaly a second.

Note: This is not the case on my remote Debian vServer. My vServer handles requests fast, no matter if using a database or not.

I use Windows 8 x64 and the latest version of XAMPP and I did not commit any changed to the configuration files.

What could be the cause of this bad performance?^

Edit: This is the connection code I use:

$sql = new SqlConnection($cfgDbHost, $cfgDbUser, $cfgDbPassword);
$sql->setCurrentDatabase($cfgDbDatabase);

[...]

    class SqlConnection
    {
        private $Link, $CurrentDatabase, $IsConnected;

        function SqlConnection($host = 'localhost', $user = 'root', $pass = '')
        {
            $this->Link = @mysql_connect($host, $user, $pass);
            $this->IsConnected = $this->Link != NULL;
        }
        function setCurrentDatabase($database)
        {
            if (@mysql_select_db($database, $this->Link))
            {
                $this->CurrentDatabase = $database;
                return true;
            }
            else
            {
                return false;
            }
        }
        [...]

bytecode77

Posted 2012-12-07T17:14:40.373

Reputation: 971

1Use a SQL Browser (i.e.: HeidiSQL) and run some test queries directly against the database... do it on the server and from a remote computer and compare the times. It could be network, it could be memory, it could be MySQLs settings. – Wes – 2012-12-07T17:30:02.510

Using HeidiSQL, a query takes almost zero time. phpMyAdmin is also fast. I'll post the connection code. – bytecode77 – 2012-12-07T17:37:55.217

I uploaded the relevant code. Also I'd like to note, that the connection alone is the part that takes all the time. Even when no queries are fired, it takes like a second longer. – bytecode77 – 2012-12-07T17:42:10.730

Answers

7

Check the Windows Hosts file here -

C:\Windows\system32\drivers\etc\hosts

Make sure that this line is in there...

127.0.0.1 localhost

And make sure that this line is commented out...

::1 localhost

Sometimes issues with IPv6 and/or localhost resolves can cause timeouts like this.

Also see if -

$host = '127.0.0.1'

makes any difference.

rightstuff

Posted 2012-12-07T17:14:40.373

Reputation: 221

Changing $host to 127.0.0.1 really helped. Seems to be a DNS problem. Thank you! – bytecode77 – 2012-12-07T21:38:13.313

yup that connect using 127.0.0.1 was it.. Thanks!! – dano – 2013-10-04T20:25:46.283