Search code examples
phpmysqldnslagremote-connection

Php - Connecting to remote database very slow


I have a new VPS server, and I'm trying to get it to connect to another server at the same ISP. When I connect via mysql's command line tool, the connection is very fast.

When I use PHP to connect to the remote DB, the connection time may take up to 5 seconds. Queries after this are executed quickly.

This is not limited to mysql, using file_get_contents() to download a file from nearly any other server gives the same lag. Using wget to get the file does not have this lag.

I timed DNS queries from within PHP using dns_get_record(), and these are fast (1-2 milliseconds).

Any thoughts on what in the php config may be causing this?

Thanks.


Solution

  • I ended up upgrading from PHP 5.1.6 to PHP 5.2.6, and the problem went away. It definitely was a DNS lookup issue within PHP, the following would take about 5 seconds to run:

    gethostbyname('example.com')
    

    I have a feeling IPV6 was an issue (mostly a hunch from reading about this online), but I don't have any proof.