Problem with host lookup in PHP

patrik

Verified User
Joined
Sep 6, 2006
Messages
128
I'm experiencing this really strange problem which has something to do with hostname lookups in PHP. When Apache has been running for some time hostname lookups tends to stop working.

This is the error message given from a site:
Code:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '[b]mysql_hostname[/b]' (2) in /path/to/script.php on line 71

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /path/to/script.php on line 72

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /path/to/script.php on line 72

When this occurs it is possible to refresh the site and sometimes it works but mostly it doesn't. A restart of apache fixes the problem temporarily. It's also possible to fix this permanent by adding mysql_hostname to /etc/hosts Please help me figure out why this occurs. This have happend on two different servers (running PHP5 and PHP4 and both using an external dedicated MySQL server). I have also tried changing nameservers in /etc/resolv.conf but it didn't help.
 
I am not too sure, but in general I think that either you can provide IP address of the remote mysql server to the script. Otherwise by adding an entry in /etc/hosts file sounds like a good idea as otherwise the server wouldn't know where `mysql_hostname` host is. I think providing a FQDN might help. Say you have a domain xyz.com, create mysql.xyz.com and point it to the right IP. Give mysql.xyz.com to all your customers then.

The issue might be to do something with DNS Cache Expire (or something similar) too?

There might be someone else, who can put some more light on this topic.
 
I am not too sure, but in general I think that either you can provide IP address of the remote mysql server to the script. Otherwise by adding an entry in /etc/hosts file sounds like a good idea as otherwise the server wouldn't know where `mysql_hostname` host is. I think providing a FQDN might help. Say you have a domain xyz.com, create mysql.xyz.com and point it to the right IP. Give mysql.xyz.com to all your customers then.

The issue might be to do something with DNS Cache Expire (or something similar) too?

There might be someone else, who can put some more light on this topic.

Thanks for your reply. How do I flush the local DNS-cache? Is there even a local DNS-cache when I don't run Bind locally? Does Apache cache DNS replies somehow?
Adding the host to /etc/hosts isn't really a good solution because we're giving each domain a subdomain mysql.xyz.com pointing at the correct MySQL server. So, let's say a customer owns example.org, then we're automatically setting mysql.example.org as a CNAME to mysqlXX.ourdomain.com which is an A record to the correct server.
 
cant u then in this case create an entry for mysqlXX.ourdomain.com in /etc/hosts?

if u r not running DNS server on the server then everything must be getting resolved based on the DNS servers listed in /etc/resolv.conf
 
cant u then in this case create an entry for mysqlXX.ourdomain.com in /etc/hosts?
Well, yes it's possible but not really a solution. First of all it looks to me like an ugly hack. Second, domains are added/deleted every day, it would be to much work to make sure /etc/hosts is updated.

if u r not running DNS server on the server then everything must be getting resolved based on the DNS servers listed in /etc/resolv.conf
I have tried changing the nameservers in /etc/resolv.conf but that didn't help. Could this be a setting i Apache or PHP somewhere? The strange thing is that this occurs on two different servers. Both with DirectAdmin but one has Apache installed from ports (rather than DA doing the work), and they are both running different versions of PHP (4 and 5).
It doesn't make sense to me that the problem is temporarily solved by restarting Apache.
 
read these, might be of some help:

http://bugs.mysql.com/bug.php?id=4229
http://au3.php.net/fopen - search for `DNS` in this
http://dev.mysql.com/doc/refman/5.0/en/dns.html

Second, domains are added/deleted every day, it would be to much work to make sure /etc/hosts is updated.

Since you said that you use CNAMES (mysql.example.org as a CNAME to mysqlXX.ourdomain.com) then in this case, dont u just have to create one entry in hosts file and doesn't require you to maintain it even if you are adding deleting domain everyday.

Also, I think could be sometime it is taking along time for the DNS Server to reply back to the query that is causing mysql_connect to die.

Try writting a small script to loop say 50 times to see, if DNS resolving is happening correctly every time.
 
This does not only occur with CNAME but with A records too. I've also noticed that it is not MySQL specific, I have customers using libcurl which suddenly fails:
libcurl error(6): Couldn't resolve host 'www.failing-domain.xyz'
I also wrote a simple script that does lookup on a particular domain (that has failed before) a 10 000 times and echos whenever it cannot get the IP address but I can't reproduce the problem. I get the feeling it has something to do with how long Apache has been running.
 
Back
Top