PHP mysql_connect cannot resolve hostname when sessions are enabled

Crazy truth? Code example:

<?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); ?> 

Throws Unknown MySQL server host 'samplehost' (2) . If we delete session_start() or just do session_destroy() before mysql_connect() , it works correctly. Basically, if we have an open session, it as mysql_connect will not resolve the host name. The host name that we use for the server is correctly added to / etc / hosts.

This is a production server with PHP 5.3.2-1ubuntu4.7 - just started today. Does anyone come across this?

edit: should specify, with or without sessions, we can specify the IP address of the database server, and it works correctly.

+4
source share
2 answers

If someone encounters this, it turns out to be a problem with the number of virtual hosts that we had in Apache. Appears when you click on the file descriptor limit, you get some odd symptoms - this was finally diagnosed when we started getting the "Too many open files" error in PHP.

It was an outdated setting for how we automatically generated vhosts for new domains. It’s better to manage this process, reduce the number of vhosts and problems have disappeared.

+2
source

Why don't you just connect first and then start a session?

0
source

Source: https://habr.com/ru/post/1341900/


All Articles