php pdo connection cannot be established, but mysql command line client can

Andreas Wagner asked:

situation as follows:

php script on server a, run by user ‘web’ (nginx + php-fpm) should access mysql on server b via pdo library, but gets
SQLSTATE[HY000] [2003] Can’t connect to MySQL server on ‘xxx.xxx.xxx.xxx’ (4)

server a centos 6.5, php 5.3; server b centos 6.2, mysql 5.1

however, mysql command line client – run as unprivileged user – can connect perfectly fine, reproducible and stable.

once mysql command line client established a successful connection between the servers, the php script runs successfully as well for about 5-7 minutes, presumably until the mysql command line client connection times out.

what am i missing?

here’s the test script:

   <?php
   try {
   $dbh = new PDO('mysql:host=111.111.111.111;dbname=myname;port=3306', 'myuser',
    'mypass');
  echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}

My answer:


$ perror 4
OS error code   4:  Interrupted system call

You should simply try again when receiving this particular error.

This may be an indication of a heavily loaded server/network. In this case you can try increasing the connect timeout as a short-term fix, and resolving the congestion issues long-term.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.