I’ve created a simple PHP test page which connects to a MySQL database, selects one int value, and then frees the result. The page takes 5 seconds or more to load. I’ve seen a lot of posts about this or similar issues but not of the resolutions have worked. Here is the output I’m getting:
mysql_connect took 4.8948628902435 seconds
mysql_select_db 0.00073790550231934 seconds
mysql_query took 0.0013959407806396 seconds
mysql_free_result took 2.0980834960938E-5 seconds
As you can see, the connection takes far too long and everything else is fast.
What I’ve tried
- Disabled IPv6
- Used IP instead of FQDN for the MySQL host.
- Tweaked some config settings.
The Facts
- All other non-PHP sites respond instantly.
- Pinging the MySQL server give 1ms latency.
- Querying the database using MySQL Query Browser gives instant response times.
FYI – I don’t do PHP so it’s okay to treat me like a baby when suggesting fixes.
The Test Script
<?php
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
mysql_connect("the_ip||the_hostname", "the_username", "the_password");
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo '<h2> mysql_connect took ' .$totaltime. ' seconds</h2>';
?>
Trace route is instantaneous:
By the way, the application in question is MantisBT as well as WordPress.
1 2 ms <1 ms <1 ms 1.2.3.4
2 <1 ms <1 ms <1 ms MYSQL5 [5.6.7.8]
Another thing to look at is “mysql_connect” which might show you that PHP is having a hard time resolving (finding) the mysql host. Are you using a IP address, localhost, or hostname? (when connecting to the database in php) If it’s a hostname try and add the IP to c:\windows\system32\drivers\etc\host
if that doesn’t work.
If all your “The Facts” are correct then you might have a issue with your php script. Can you post it so I can see what you are running? Also post the mysql table info like “Indexes” etc.
🙂