简体   繁体   中英

Remote Server SQLSTATE[HY000] [2002] Connection timed out

When using php to connect to a remote MySQL database (say 999.999.999.999 ) from a server running on a different IP (say 888.888.888.888 ), I get this error:

SQLSTATE[HY000] [2002] Connection timed out

Here is what I've tried or looked into:

  • On the remote server, I have allowed connections from the "local server" ( 888.888.888.88 ), but that hasn't helped.
  • Some answers suggest changing the connection details from localhost to 127.0.0.1 , but this is not relevant to me as we are connecting to a remote database.
  • I restarted the server (CentOS with cPanel ) several times with no luck.

Any idea?

Resurrecting this question because I ran into a similar problem on cPanel and none of the answers I found solved it.

There is a progression of three steps to resolve this issue. For me, the third was critical.

1. Credentials

We'll assume you already have the correct credentials to access the database. These can be tested:

  • locally on the database if you have direct access.
  • remotely, from an IP that is not the one giving you trouble (your CentOS server). For instance, you can use programs such as HeidiSQL (Win) or Sequel Pro (OSX) as a sanity check.

You could also run a php script like the following one both from the troublesome origin server and from another server. If only your server refuses to connect, you know you don't have a credentials issue.

<?php
try {
    $db = new PDO('mysql:host=999.999.999.999;dbname=Database_Name_Goes_Here;charset=utf8',
            'User_Goes_Here', 'Password_Goes_Here',
            array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
            );
    echo "We connected.";
} catch (PDOException $e) {
    print($e->getMessage());
    }
?>              

2. Remote Access: Allowing your IP

Most of the answers point out that in the remote server, you must allow connections from your IP. You have already done this.

For everyone else, if you are also using cPanel like the OP, you can add the IP in the Remote MySQL section of cPanel .

3. Firewall

This was the missing ingredient for me in the answers I found. After all, isn't there a chance that the error message might be correct, ie, that the connection simply timed out?

This points to outside connections being disabled from your server. I investigated if there were any parameters in php.ini that could conflict, then turned at the firewall, which provided the answer.

I don't know which firewall you're using. Here's what to do for those using the csf firewall, which is common on cPanel / WHM installations. Assuming you have SSH access, enter something like this, where the IP is that of the remote database server:

csf -a 999.999.999.999 'allow connection to XYZ database server'

  • If you have a different firewall, see the documentation.
  • If you have neither ssh nor WHM (or equivalent) access, ask your web host to unblock the remote IP for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM