Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have been recently getting these errors on my server:

mysql_connect() [function.mysql-connect ]: 
   Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (4)

I don't think this is a credentials issue since the username, password, host IP, and database name are all read from a flat file config and stored in a PHP constant. Plus my error log tells me that the credentials being used are actually the ones in the config.

I tried to google this up and one points me to a max_connections issue. This site is still in beta and the number of concurrent users does not exceed 10. I looked up the value of max_connections and it is at 2048

The MySQL version is 5.0.91 and the server is Gentoo Linux (its what the version_comment says). Socket file is at /var/run/mysqld/mysqld.sock and port is 3306

What's really bugging me is that this error seems intermittent. I really can't catch it when I am trying to reproduce it. Somebody said from one site that the error index (4) means interrupted systems calls.

Does this mean that I can dismiss the error as a fault in my web host and not my PHP script?

share|improve this question
    
Can you connect from the command line with: mysql -u <user> -p -h <ip>? –  quanta Aug 25 '11 at 8:18
    
yes. weird huh? –  Rolando Cruz Aug 25 '11 at 9:57
add comment

2 Answers

Try to see if you're not exeeding the max open file limit typing dmesg and watching for the "Too many open files" error.

If you're exeeding it, you'll have to increase the limits fixed by the system by changing the /etc/security/limits.conf with this statement :


mysql           soft    nofile          2048
mysql           hard    nofile          4096

Where 2048 and 4096 are the maximum number of files the user "mysql" can open. (This includes all file descriptors such as sockets)

share|improve this answer
add comment

You may dig down in further by utilizing the mysql_error() and mysql_errno()-functions, but from what I can tell, your assumptions are right.

As a short bugfix (which I usually implement in my webapplications) is a reconnect with logging after two seconds:

$i = 0;
if ($i < 3 && ($db = mysql_connect(...)) == false) {
    trigger_error('Could not connect to ...', E_USER_WARNING);
    $i++;
    sleep(2);
}
if (!$db)
    die('Could not connect to ... for 3 times with a 2 second dely - giving up');

As the site is low-traffic, you might consider running a tcpdump on the server to a file and configure your php script to mail you a warning if a connection fails. Copy over the tcpdump-file to your local box and analyse the connection failure via wireshark.

You should also check the mysql-daemon logs and the system logs on your system around the time of failure for anomalies.

Be aware, that logging your traffic will log all contents of the traffic including passwords and other sensitive information and thus may be violating company policies or laws. You should wipe the tcpdump-file after deactivating the logging to not let any sensitive information left on the server and transfer the files via ssh or any other appropriate encrypted connection.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.