PHP mysql_connect cannot resolve Host name when sessions are enabled
Crazy right? Sample code:
<?php
session_start();
$hostname="samplehost";
$username="sampleuser";
$password="samplepass";
$dbname="sampledb";
$link = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname, $link);
?>
Will throw Unknown MySQL server host 'samplehost' (2)
. If we remove the session_start()
or just do开发者_运维技巧 a session_destroy()
before the mysql_connect()
, it works correctly. Basically, if we have a session open, its like mysql_connect wont resolve the host name. The host name we use for the server is correctly added to /etc/hosts.
It's a production server running PHP 5.3.2-1ubuntu4.7 - just started happening today. Anyone run into this?
edit: should mention, with or without sessions, we can specify the IP of the DB server, and it works correctly.
If someone comes across this - it turns out to be a an issue with the amount of Virtual Hosts we had in Apache. Appears when you start hitting the limit on file descriptors, you get some odd symptoms - it was finally diagnosed when we started getting a 'Too many open files' error in PHP.
It was a legacy setup in how we were automatically generating vhosts for new domains. Better managed that process, reduce the amount of vhosts and the issues went away.
Why don't you just connect first, then start the session?
精彩评论