Simon
Simon

Reputation: 23

MySQL connections fails unpredictably in PHP

We are seeing a MySQL connection problem on our server. The strange thing is that this problem only occurs once in a while, perhaps 1 out of 100 times, and it seems it happens more frequently when there is heavy load on our servers. Before, when we had much less users, we would never see this error.

The error comes from this line of code:

$myconnection = mysql_pconnect($hostname_myconnection, $username_myconnection, $password_myconnection) or trigger_error(mysql_error(),E_USER_ERROR);

and the error which is thrown is:

Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (11) in/home/john/web/htdocs/lib/myconnection.php on line 11

In the same file we also establish another MySQL connection, which is based on Zend Db Adapter:

$db = new Zend_Db_Adapter_Mysqli($data);

We use both the two connections.

My question is: where does this error come from? Can the error be triggered because too many people are trying to establish a connection at the same time? If this is the case, what is the setting in httpd.conf or my.cnf that we should change? Or is it a problem that we are establishing two connections per user?

Upvotes: 2

Views: 343

Answers (1)

vbence
vbence

Reputation: 20333

I see you're using persistent connections so this should not be a problem at all. I suspect your PHP was not compiled with persistent connections, and mysql_connect is called in the background instead of its persistent cousin.

All systems have a limit on open sockets and files, if you are instantiating many connections during page load and the persistent function is not working they each take up a new socket.

You should also consider closing the connections manually, as PHP scripts can run a few seconds even after the client hit the cancel button, and cleanup (closing the connection) could take a while this way.

Upvotes: 2

Related Questions