Centos – php pdo connection cannot be established, but thesql command line client can

centosconnectionMySQLpdoPHP

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();
}

Best Answer

So it turns out that this had been a routing issue after all.

We are currently using a statically routed network, and the mysql server a did not have a route defined back to server b.

the network setup is a bit messy, as the physical connection goes like this: server b (off-site) > vpn gw > internet > checkpoint fw > openwrt router > server a

Apparently the mysql command line client is more forgiving and was able to establish that connection anyway, but the pdo library was not.

After adding the route, the pdo library could successfully establish connections on its own and is happily running after since.

So, check your routes, even if ping/telnet/mysql connections are seemingly working.

By the way, would the error "Interrupted system call" cover this scenario?